use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class DebugMethodYieldsOnInvocationsCheck method reportYields.
private void reportYields(MethodInvocationTree mit, CheckerDispatcher checkerDispatcher) {
MethodBehavior mb = checkerDispatcher.peekMethodBehavior((Symbol.MethodSymbol) mit.symbol());
if (mb != null && mb.isComplete()) {
IdentifierTree methodName = getIdentifier(mit.methodSelect());
String message = String.format("Method '%s' has %d method yields.", methodName.name(), mb.yields().size());
Set<Flow> flow = flowFromYield(mb, methodName);
reportIssue(methodName, message, flow);
}
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method generateMethodBehavior.
@Test
public void generateMethodBehavior() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun(ZLjava/lang/Object;)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svsecondArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg, svsecondArg);
List<ObjectConstraint> collect = methodBehavior.yields().stream().map(my -> {
Collection<ProgramState> ps = my.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ProgramState next = ps.iterator().next();
return next.getConstraint(svResult, ObjectConstraint.class);
}).collect(Collectors.toList());
assertThat(collect).hasSize(2).containsOnly(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
List<HappyPathYield> nullConstraintOnResult = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(nullConstraintOnResult).hasSize(1);
HappyPathYield nullConstraintResult = nullConstraintOnResult.get(0);
Collection<ProgramState> ps = nullConstraintResult.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ObjectConstraint constraint = ps.iterator().next().getConstraint(svsecondArg, ObjectConstraint.class);
assertThat(constraint).isSameAs(ObjectConstraint.NULL);
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_enqueueing_of_catch_blocks.
@Test
public void test_enqueueing_of_catch_blocks() {
MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "testCatchBlockEnqueue(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Z");
List<HappyPathYield> happyPathYields = mb.happyPathYields().collect(Collectors.toList());
assertThat(happyPathYields).hasSize(1);
assertThat(happyPathYields.get(0).resultConstraint()).isNull();
List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
assertThat(exceptionalYields).hasSize(1);
assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.lang.RuntimeException")).isTrue();
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_enqueueing_of_catch_blocks2.
@Test
public void test_enqueueing_of_catch_blocks2() {
MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "testCatchBlockEnqueue2()Z");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(1);
// result should have TRUE constraint, but wrong yield with FALSE constraint is also created
// and two yields are reduced subsequently
assertThat(mb.happyPathYields().findFirst().get().resultConstraint()).isNull();
assertThat(mb.exceptionalPathYields().findFirst().isPresent()).isFalse();
}
use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block3.
@Test
public void test_enqueueing_of_exit_block3() {
MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock3()Z");
assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
assertThat(exceptionalYields).hasSize(1);
assertThat(exceptionalYields.get(0).exceptionType(semanticModel).is("java.io.FileNotFoundException")).isTrue();
}
Aggregations