Search in sources :

Example 26 with MethodNode

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);
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Test(org.junit.Test)

Example 27 with MethodNode

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);
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Test(org.junit.Test)

Example 28 with MethodNode

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);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Source(org.jacoco.core.test.validation.Source) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with MethodNode

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);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) JavaSymbol(org.sonar.java.resolve.JavaSymbol) H_INVOKESTATIC(org.objectweb.asm.Opcodes.H_INVOKESTATIC) Multiset(com.google.common.collect.Multiset) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) NO_OPERAND_INSN(org.sonar.java.bytecode.cfg.Instructions.NO_OPERAND_INSN) Label(org.objectweb.asm.Label) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VAR_INSN(org.sonar.java.bytecode.cfg.Instructions.VAR_INSN) MethodNode(org.objectweb.asm.tree.MethodNode) INVOKEINTERFACE(org.objectweb.asm.Opcodes.INVOKEINTERFACE) Lists(com.google.common.collect.Lists) CFGTestData(org.sonar.java.bytecode.cfg.testdata.CFGTestData) HashMultiset(com.google.common.collect.HashMultiset) FIELD_INSN(org.sonar.java.bytecode.cfg.Instructions.FIELD_INSN) JUMP_INSN(org.sonar.java.bytecode.cfg.Instructions.JUMP_INSN) METHOD_INSN(org.sonar.java.bytecode.cfg.Instructions.METHOD_INSN) JavaParser(org.sonar.java.ast.parser.JavaParser) NOP(org.objectweb.asm.Opcodes.NOP) Opcodes(org.objectweb.asm.Opcodes) Predicate(java.util.function.Predicate) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) Test(org.junit.Test) TYPE_INSN(org.sonar.java.bytecode.cfg.Instructions.TYPE_INSN) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Handle(org.objectweb.asm.Handle) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) ClassReader(org.objectweb.asm.ClassReader) Printer(org.objectweb.asm.util.Printer) SemanticModel(org.sonar.java.resolve.SemanticModel) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JSR(org.objectweb.asm.Opcodes.JSR) ClassNode(org.objectweb.asm.tree.ClassNode) Convert(org.sonar.java.resolve.Convert) INT_INSN(org.sonar.java.bytecode.cfg.Instructions.INT_INSN) Symbol(org.sonar.plugins.java.api.semantic.Symbol) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassNode(org.objectweb.asm.tree.ClassNode) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodNode(org.objectweb.asm.tree.MethodNode) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) Test(org.junit.Test)

Example 30 with MethodNode

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");
    }
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) Test(org.junit.Test)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17