Search in sources :

Example 1 with NOP

use of org.objectweb.asm.Opcodes.NOP in project sonar-java by SonarSource.

the class BytecodeCFGBuilderTest method all_opcodes_should_be_visited.

@Test
public void all_opcodes_should_be_visited() throws Exception {
    Instructions ins = new Instructions();
    Predicate<Integer> filterReturnAndThrow = opcode -> !((Opcodes.IRETURN <= opcode && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW);
    NO_OPERAND_INSN.stream().filter(filterReturnAndThrow).forEach(ins::visitInsn);
    INT_INSN.forEach(i -> ins.visitIntInsn(i, 0));
    VAR_INSN.forEach(i -> ins.visitVarInsn(i, 0));
    TYPE_INSN.forEach(i -> ins.visitTypeInsn(i, "java/lang/Object"));
    FIELD_INSN.forEach(i -> ins.visitFieldInsn(i, "java/lang/Object", "foo", "D(D)"));
    METHOD_INSN.forEach(i -> ins.visitMethodInsn(i, "java/lang/Object", "foo", "()V", i == INVOKEINTERFACE));
    JUMP_INSN.forEach(i -> {
        Label jumpLabel = new Label();
        ins.visitJumpInsn(i, jumpLabel);
        ins.visitLabel(jumpLabel);
    });
    ins.visitLdcInsn("a");
    ins.visitIincInsn(0, 1);
    Handle handle = new Handle(H_INVOKESTATIC, "", "", "()V", false);
    ins.visitInvokeDynamicInsn("sleep", "()V", handle);
    ins.visitLookupSwitchInsn(new Label(), new int[] {}, new Label[] {});
    ins.visitMultiANewArrayInsn("B", 1);
    Label l0 = new Label();
    Label dflt = new Label();
    Label case0 = new Label();
    ins.visitTableSwitchInsn(0, 1, dflt, case0);
    ins.visitLabel(dflt);
    ins.visitInsn(NOP);
    ins.visitLabel(l0);
    ins.visitInsn(NOP);
    BytecodeCFG cfg = ins.cfg();
    Multiset<String> cfgOpcodes = cfgOpcodes(cfg);
    List<String> collect = Instructions.ASM_OPCODES.stream().filter(filterReturnAndThrow).map(op -> Printer.OPCODES[op]).collect(Collectors.toList());
    assertThat(cfgOpcodes).containsAll(collect);
}
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) Label(org.objectweb.asm.Label) Handle(org.objectweb.asm.Handle) Test(org.junit.Test)

Aggregations

HashMultiset (com.google.common.collect.HashMultiset)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Multiset (com.google.common.collect.Multiset)1 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 ClassReader (org.objectweb.asm.ClassReader)1 Handle (org.objectweb.asm.Handle)1 Label (org.objectweb.asm.Label)1 Opcodes (org.objectweb.asm.Opcodes)1 H_INVOKESTATIC (org.objectweb.asm.Opcodes.H_INVOKESTATIC)1