Search in sources :

Example 16 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block.

@Test
public void test_enqueueing_of_exit_block() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock()Z");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
    ExceptionalYield exceptionalYield = mb.exceptionalPathYields().findFirst().get();
    Type exceptionType = exceptionalYield.exceptionType(semanticModel);
    assertThat(exceptionType.is("java.io.FileNotFoundException")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodYield(org.sonar.java.se.xproc.MethodYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 17 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method athrow_should_not_be_linked_to_next_label.

@Test
public void athrow_should_not_be_linked_to_next_label() throws Exception {
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {int field;}");
    SquidClassLoader classLoader = new SquidClassLoader(Collections.singletonList(new File("src/test/JsrRet")));
    SemanticModel semanticModel = SemanticModel.createFor(tree, classLoader);
    BehaviorCache behaviorCache = new BehaviorCache(classLoader);
    behaviorCache.setFileContext(null, semanticModel);
    BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
    MethodBehavior methodBehavior = walker.getMethodBehavior("org.apache.commons.io.FileUtils#readFileToString(Ljava/io/File;)Ljava/lang/String;", classLoader);
    assertThat(methodBehavior.happyPathYields().collect(Collectors.toList())).hasSize(1);
    assertThat(methodBehavior.exceptionalPathYields().collect(Collectors.toList())).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SemanticModel(org.sonar.java.resolve.SemanticModel) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 18 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method behavior_should_have_declared_exceptions.

@Test
public void behavior_should_have_declared_exceptions() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#throwing()V", squidClassLoader);
    assertThat(mb.isComplete()).isFalse();
    assertThat(mb.getDeclaredExceptions()).containsExactly("java.io.IOException");
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 19 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.

@Test
public void method_returning_new_should_have_not_null_result() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    MethodYield yield = yields.get(0);
    assertThat(yield).isInstanceOf(HappyPathYield.class);
    ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
    assertThat(resultConstraint).isNotNull();
    assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
    assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Test(org.junit.Test)

Example 20 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerTest method max_step_exception_should_log_warning_and_generate_behavior.

@Test
public void max_step_exception_should_log_warning_and_generate_behavior() {
    BytecodeEGWalker bytecodeEGWalker = new BytecodeEGWalker(new BehaviorCache(squidClassLoader), semanticModel) {

        @Override
        int maxSteps() {
            return 2;
        }
    };
    MethodBehavior methodBehavior = getMethodBehavior(BytecodeTestClass.class, "fun(ZLjava/lang/Object;)Ljava/lang/Object;", bytecodeEGWalker);
    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Dataflow analysis is incomplete for method org.sonar.java.bytecode.se.testdata.BytecodeTestClass#fun(ZLjava/lang/Object;)Ljava/lang/Object;" + " : Too many steps resolving org.sonar.java.bytecode.se.testdata.BytecodeTestClass#fun(ZLjava/lang/Object;)Ljava/lang/Object;");
    assertThat(methodBehavior.isComplete()).isFalse();
    assertThat(methodBehavior.isVisited()).isTrue();
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Test(org.junit.Test)

Aggregations

MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)36 Test (org.junit.Test)30 SemanticModel (org.sonar.java.resolve.SemanticModel)10 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)10 HappyPathYield (org.sonar.java.se.xproc.HappyPathYield)10 MethodYield (org.sonar.java.se.xproc.MethodYield)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 Type (org.sonar.plugins.java.api.semantic.Type)8 Lists (com.google.common.collect.Lists)6 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)6 ProgramState (org.sonar.java.se.ProgramState)6 DivisionByZeroCheck (org.sonar.java.se.checks.DivisionByZeroCheck)6 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)6 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)6 ExceptionalYield (org.sonar.java.se.xproc.ExceptionalYield)6 File (java.io.File)5 Collection (java.util.Collection)5 Set (java.util.Set)5