use of org.objectweb.asm.tree.MethodNode in project jacoco by jacoco.
the class LombokGeneratedFilterTest method testNoAnnotations.
@Test
public void testNoAnnotations() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);
filter.filter("Foo", "java/lang/Object", m, this);
assertNull(fromInclusive);
assertNull(toInclusive);
}
use of org.objectweb.asm.tree.MethodNode in project jacoco by jacoco.
the class LombokGeneratedFilterTest method testOtherAnnotation.
@Test
public void testOtherAnnotation() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
m.visitAnnotation("Lother/Annotation;", false);
m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);
filter.filter("Foo", "java/lang/Object", m, this);
assertNull(fromInclusive);
assertNull(toInclusive);
}
use of org.objectweb.asm.tree.MethodNode in project jacoco by jacoco.
the class FinallyTest method gotos.
/**
* This test studies placement of GOTO instructions.
*/
@Test
public void gotos() throws IOException {
final Source source = Source.getSourceFor("src", Finally.class);
byte[] b = TargetLoader.getClassDataAsBytes(Finally.class);
b = BytecodeVersion.downgradeIfNeeded(BytecodeVersion.get(b), b);
final ClassNode classNode = new ClassNode();
new ClassReader(b).accept(classNode, 0);
final Set<String> tags = new HashSet<String>();
for (final MethodNode m : classNode.methods) {
if ("main".equals(m.name)) {
// skip it
continue;
}
int lineNumber = -1;
for (AbstractInsnNode i = m.instructions.getFirst(); i != null; i = i.getNext()) {
if (AbstractInsnNode.LINE == i.getType()) {
lineNumber = ((LineNumberNode) i).line;
}
if (Opcodes.GOTO == i.getOpcode()) {
final String line = source.getLine(lineNumber);
if (line.indexOf('$') < 0) {
throw new AssertionError("No tag at line " + lineNumber);
}
final String tag = line.substring(line.indexOf('$') + "$line-".length(), line.lastIndexOf('$'));
tags.add(tag);
}
}
}
final Set<String> expected = new HashSet<String>();
if (isJDKCompiler) {
expected.add("example.2");
} else {
expected.add("example.0");
}
expected.add("breakStatement.for");
if (isJDKCompiler) {
if (JAVA_VERSION.isBefore("10")) {
// https://bugs.openjdk.java.net/browse/JDK-8180141
expected.add("breakStatement.1");
} else {
expected.add("breakStatement");
}
expected.add("breakStatement.2");
} else {
expected.add("breakStatement");
}
if (isJDKCompiler) {
expected.add("emptyCatch.2");
} else {
expected.add("emptyCatch");
expected.add("emptyCatch.1");
}
if (isJDKCompiler) {
expected.add("catchNotExecuted.2");
} else {
expected.add("catchNotExecuted");
expected.add("catchNotExecuted.1");
}
if (isJDKCompiler) {
expected.add("nested.5");
expected.add("nested.6");
} else {
expected.add("nested.0");
expected.add("nested.3");
}
if (isJDKCompiler && JAVA_VERSION.isBefore("1.8")) {
expected.add("emptyTry.2");
}
if (!isJDKCompiler) {
expected.add("alwaysCompletesAbruptly.0");
}
assertEquals(expected, tags);
}
use of org.objectweb.asm.tree.MethodNode in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method test_all_instructions_are_part_of_CFG.
@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
ClassReader cr = new ClassReader(squidClassLoader.getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
ClassNode classNode = new ClassNode(Opcodes.ASM5);
cr.accept(classNode, 0);
for (MethodNode method : classNode.methods) {
Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode).filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode]).collect(Collectors.toCollection(HashMultiset::create));
Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) methodSymbol).completeSignature(), squidClassLoader);
Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
assertThat(cfgOpcodes).isEqualTo(opcodes);
}
}
use of org.objectweb.asm.tree.MethodNode in project closure-templates by google.
the class BytecodeProducerTest method testGenDoesntOverlapWithCompile.
@Test
public void testGenDoesntOverlapWithCompile() {
BytecodeProducer producer = new BytecodeProducer() {
@Override
protected void doGen(CodeBuilder adapter) {
BytecodeUtils.constant('c').gen(adapter);
}
};
try {
CodeBuilder adaterAdapter = new CodeBuilder(Opcodes.ACC_PUBLIC, BytecodeUtils.NULLARY_INIT, new MethodNode());
producer.gen(adaterAdapter);
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("All bytecode producers should be constructed prior to code generation");
}
}
Aggregations