Search in sources :

Example 11 with MethodBehavior

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

the class BytecodeEGWalkerTest method test_starting_states.

@Test
public void test_starting_states() throws Exception {
    BytecodeEGWalker walker = new BytecodeEGWalker(null, semanticModel);
    String signature = "type#foo()V";
    walker.methodBehavior = new MethodBehavior(signature);
    ProgramState startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, false));
    SymbolicValue thisSv = startingState.getValue(0);
    assertThat(thisSv).isNotNull();
    assertThat(startingState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
    assertThat(startingState).isEqualTo(ProgramState.EMPTY_STATE);
    signature = "type#foo(DIJ)V";
    walker.methodBehavior = new MethodBehavior(signature);
    startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
    assertThat(startingState.getValue(0)).isNotNull();
    SymbolicValue doubleArg = startingState.getValue(0);
    assertThat(startingState.getConstraint(doubleArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
    assertThat(startingState.getValue(1)).isNull();
    assertThat(startingState.getValue(2)).isNotNull();
    SymbolicValue longArg = startingState.getValue(3);
    assertThat(longArg).isNotNull();
    assertThat(startingState.getConstraint(longArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 12 with MethodBehavior

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

the class BytecodeEGWalkerTest method goto_terminator.

@Test
public void goto_terminator() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("gotoTerminator(Ljava/lang/Object;)Z");
    assertThat(methodBehavior.yields()).hasSize(2);
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 13 with MethodBehavior

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

the class BytecodeEGWalkerTest method propagation_of_bytecode_analysis_exception.

@Test
public void propagation_of_bytecode_analysis_exception() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior(MaxRelationBytecode.class, "isXMLLetter(C)Z");
    assertThat(methodBehavior.isComplete()).isFalse();
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 14 with MethodBehavior

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

the class BytecodeEGWalkerTest method test_method_is_complete.

@Test
public void test_method_is_complete() {
    String desc = "(Ljava/lang/String;)Z";
    MethodBehavior nativeMethod = getMethodBehavior("nativeMethod" + desc);
    assertThat(nativeMethod.isComplete()).isFalse();
    MethodBehavior abstractMethod = getMethodBehavior("abstractMethod" + desc);
    assertThat(abstractMethod.isComplete()).isFalse();
    MethodBehavior finalMethod = getMethodBehavior("finalMethod" + desc);
    assertThat(finalMethod.isComplete()).isFalse();
    MethodBehavior staticMethod = getMethodBehavior("staticMethod" + desc);
    assertThat(staticMethod.isComplete()).isTrue();
    MethodBehavior privateMethod = getMethodBehavior("privateMethod" + desc);
    assertThat(privateMethod.isComplete()).isFalse();
    MethodBehavior publicMethodInFinalClass = getMethodBehavior(FinalBytecodeTestClass.class, "publicMethod" + desc);
    assertThat(publicMethodInFinalClass.isComplete()).isFalse();
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 15 with MethodBehavior

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

the class BytecodeEGWalkerTest method test_method_throwing_exception.

@Test
public void test_method_throwing_exception() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("throw_exception()V");
    assertThat(methodBehavior.yields()).hasSize(1);
    MethodYield methodYield = methodBehavior.yields().get(0);
    assertThat(methodYield).isInstanceOf(ExceptionalYield.class);
}
Also used : MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) 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