Search in sources :

Example 36 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_enqueuing_exceptional_yields2.

@Test
public void test_enqueuing_exceptional_yields2() {
    BytecodeCFG cfg = SETestUtils.bytecodeCFG(TRY_WRONG_CATCH_SIGNATURE, squidClassLoader);
    BytecodeCFG.Block b2 = cfg.blocks().get(2);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue());
    walker.programPosition = new ProgramPoint(b2).next().next();
    walker.executeInstruction(b2.elements().get(3));
    assertThat(walker.workList).hasSize(3);
    assertThat(walker.workList.pop().programState.exitValue()).isNotNull().isInstanceOf(SymbolicValue.ExceptionalSymbolicValue.class).extracting(sv -> ((SymbolicValue.ExceptionalSymbolicValue) sv).exceptionType().fullyQualifiedName()).containsExactly("java.lang.IllegalStateException");
    assertThat(walker.workList.pop().programState.exitValue()).isNull();
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Example 37 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method exceptional_paths_should_be_enqueued.

@Test
public void exceptional_paths_should_be_enqueued() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#enqueue_exceptional_paths(Lorg/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest;)Ljava/lang/Object;", squidClassLoader);
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint()).map(c -> c.get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 38 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method initializeClassLoaderAndSemanticModel.

@BeforeClass
public static void initializeClassLoaderAndSemanticModel() {
    List<File> files = new ArrayList<>(FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true));
    files.add(new File("target/classes"));
    files.add(new File("target/test-classes"));
    squidClassLoader = new SquidClassLoader(files);
    File file = new File("src/test/java/org/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    semanticModel = SemanticModel.createFor(tree, squidClassLoader);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) BeforeClass(org.junit.BeforeClass)

Example 39 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeSECheckTest method initializeWalker.

@BeforeClass
public static void initializeWalker() {
    List<File> files = new ArrayList<>();
    files.add(new File("target/test-classes"));
    squidClassLoader = new SquidClassLoader(files);
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {}");
    semanticModel = SemanticModel.createFor(tree, squidClassLoader);
    walker = getEGWalker();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) BeforeClass(org.junit.BeforeClass)

Example 40 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class CFGTest method buildCFGFromCUT.

private static CFG buildCFGFromCUT(CompilationUnitTree cut) {
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    final MethodTree tree = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(0));
    return CFG.build(tree);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)53 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)37 File (java.io.File)24 Test (org.junit.Test)18 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 ArrayList (java.util.ArrayList)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 SemanticModel (org.sonar.java.resolve.SemanticModel)9 Tree (org.sonar.plugins.java.api.tree.Tree)9 List (java.util.List)7 Collectors (java.util.stream.Collectors)6 BeforeClass (org.junit.BeforeClass)6 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Opcodes (org.objectweb.asm.Opcodes)5 JavaParser (org.sonar.java.ast.parser.JavaParser)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Before (org.junit.Before)4 Label (org.objectweb.asm.Label)4