use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class MethodYieldTest method constraints_on_varargs.
@Test
public void constraints_on_varargs() throws Exception {
ActionParser<Tree> p = JavaParser.createParser();
CompilationUnitTree cut = (CompilationUnitTree) p.parse(new File("src/test/files/se/VarArgsYields.java"));
SemanticModel semanticModel = SemanticModel.createFor(cut, new SquidClassLoader(new ArrayList<>()));
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Lists.newArrayList(new SECheck[] {}), new BehaviorCache(new SquidClassLoader(new ArrayList<>())));
JavaFileScannerContext context = mock(JavaFileScannerContext.class);
when(context.getTree()).thenReturn(cut);
when(context.getSemanticModel()).thenReturn(semanticModel);
sev.scanFile(context);
MethodSymbol methodSymbol = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(0)).symbol();
MethodBehavior mb = getMethodBehavior(sev, "varArgMethod");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(5);
assertThat(mb.exceptionalPathYields()).hasSize(4);
MethodYield yield = mb.happyPathYields().findFirst().get();
// check that we have NOT_NULL constraint on the first argument
assertThat(yield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// check that we have NOT_NULL constraint on the variadic argument
assertThat(yield.parametersConstraints.get(1).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
List<IdentifierTree> usages = methodSymbol.usages();
assertThat(usages).hasSize(6);
List<List<Type>> arguments = usages.stream().map(MethodYieldTest::getMethodIncoationArgumentsTypes).collect(Collectors.toList());
ProgramState ps = ProgramState.EMPTY_STATE;
ProgramState psResult;
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svVarArg1 = new SymbolicValue();
SymbolicValue svVarArg2 = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
// apply constraint NotNull to parameter
Collection<ProgramState> arrayOfA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(0), ps, () -> svResult).collect(Collectors.toList());
assertThat(arrayOfA).hasSize(1);
psResult = arrayOfA.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// apply constraint NotNull to parameter (B[] is a subtype of A[])
Collection<ProgramState> arrayOfB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(1), ps, () -> svResult).collect(Collectors.toList());
assertThat(arrayOfB).hasSize(1);
psResult = arrayOfB.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// no constraint, as 'a' is not an array
Collection<ProgramState> objectA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(2), ps, () -> svResult).collect(Collectors.toList());
assertThat(objectA).hasSize(1);
psResult = objectA.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
// no constraint, as 'a' and 'b' can not receive the constraint of the array
Collection<ProgramState> objectsAandB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1, svVarArg2), arguments.get(3), ps, () -> svResult).collect(Collectors.toList());
assertThat(objectsAandB).hasSize(1);
psResult = objectsAandB.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
assertThat(psResult.getConstraint(svVarArg2, ObjectConstraint.class)).isNull();
// no param, we only learn something about the argument which is not variadic
Collection<ProgramState> noParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg), arguments.get(4), ps, () -> svResult).collect(Collectors.toList());
assertThat(noParam).hasSize(1);
psResult = noParam.iterator().next();
assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
// null param, contradiction, no resulting program state
ps = ProgramState.EMPTY_STATE.addConstraint(svFirstArg, ObjectConstraint.NULL);
Collection<ProgramState> nullParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(5), ps, () -> svResult).collect(Collectors.toList());
assertThat(nullParam).isEmpty();
}
use of org.sonar.java.resolve.SemanticModel 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);
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class ExceptionalYieldTest method exceptional_yields.
@Test
public void exceptional_yields() {
Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/ExceptionalYields.java");
SymbolicExecutionVisitor sev = sevAndSemantic.a;
SemanticModel semanticModel = sevAndSemantic.b;
MethodBehavior mb = getMethodBehavior(sev, "myMethod");
assertThat(mb.yields()).hasSize(4);
List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
assertThat(exceptionalYields).hasSize(3);
// runtime exception
Optional<ExceptionalYield> runtimeException = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown()).findFirst();
assertThat(runtimeException.isPresent()).isTrue();
MethodYield runtimeExceptionYield = runtimeException.get();
assertThat(runtimeExceptionYield.parametersConstraints.get(0).get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
// exception from other method call
Optional<ExceptionalYield> implicitException = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException2")).findFirst();
assertThat(implicitException.isPresent()).isTrue();
MethodYield implicitExceptionYield = implicitException.get();
assertThat(implicitExceptionYield.parametersConstraints.get(0).get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
// explicitly thrown exception
Optional<ExceptionalYield> explicitException = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException1")).findFirst();
assertThat(explicitException.isPresent()).isTrue();
MethodYield explicitExceptionYield = explicitException.get();
assertThat(explicitExceptionYield.parametersConstraints.get(0).get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.TRUE);
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class ExceptionalYieldTest method test_equals.
@Test
public void test_equals() {
MethodBehavior methodBehavior = mockMethodBehavior();
ExceptionalYield yield = new ExceptionalYield(methodBehavior);
ExceptionalYield otherYield = new ExceptionalYield(methodBehavior);
assertThat(yield).isNotEqualTo(null);
assertThat(yield).isEqualTo(yield);
assertThat(yield).isEqualTo(otherYield);
otherYield.setExceptionType("java.lang.Exception");
assertThat(yield).isNotEqualTo(otherYield);
yield.setExceptionType("java.lang.Exception");
assertThat(yield).isEqualTo(otherYield);
SemanticModel semanticModel = SemanticModel.createFor((CompilationUnitTree) JavaParser.createParser().parse("class A{}"), new SquidClassLoader(new ArrayList<>()));
assertThat(yield.exceptionType(semanticModel)).isEqualTo(otherYield.exceptionType(semanticModel));
// same arity and parameters but happy yield
assertThat(yield).isNotEqualTo(new HappyPathYield(methodBehavior));
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class MethodBehaviorTest method method_behavior_handling_finally.
@Test
public void method_behavior_handling_finally() {
Pair<SymbolicExecutionVisitor, SemanticModel> visitorAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/resources/se/ReturnAndFinally.java");
SymbolicExecutionVisitor sev = visitorAndSemantic.a;
SemanticModel semanticModel = visitorAndSemantic.b;
assertThat(sev.behaviorCache.behaviors.entrySet()).hasSize(5);
MethodBehavior foo = getMethodBehavior(sev, "foo");
assertThat(foo.yields()).hasSize(4);
assertThat(foo.happyPathYields()).hasSize(2);
assertThat(foo.exceptionalPathYields()).hasSize(2);
MethodBehavior qix = getMethodBehavior(sev, "qix");
List<MethodYield> qixYield = qix.yields();
assertThat(qixYield.stream().filter(y -> y.parametersConstraints.get(0).get(ObjectConstraint.class) != ObjectConstraint.NULL).allMatch(y -> y instanceof ExceptionalYield)).isTrue();
assertThat(qixYield.stream().filter(y -> y.parametersConstraints.get(0).get(ObjectConstraint.class) == ObjectConstraint.NULL && y instanceof ExceptionalYield).count()).isEqualTo(2);
assertThat(qixYield.stream().filter(y -> y instanceof HappyPathYield).allMatch(y -> y.parametersConstraints.get(0).get(ObjectConstraint.class) == ObjectConstraint.NULL)).isTrue();
MethodBehavior returnInFinally = getMethodBehavior(sev, "returnInFinally");
assertThat(returnInFinally.yields()).hasSize(1);
assertThat(returnInFinally.happyPathYields()).hasSize(1);
MethodBehavior returningException = getMethodBehavior(sev, "returningException");
assertThat(returningException.yields()).hasSize(3);
assertThat(returningException.happyPathYields()).hasSize(2);
assertThat(returningException.exceptionalPathYields()).hasSize(1);
MethodBehavior rethrowingException = getMethodBehavior(sev, "rethrowingException");
assertThat(rethrowingException.yields()).hasSize(4);
assertThat(rethrowingException.happyPathYields()).hasSize(1);
assertThat(rethrowingException.exceptionalPathYields()).hasSize(3);
assertThat(rethrowingException.exceptionalPathYields().filter(y -> y.exceptionType(semanticModel).isUnknown())).hasSize(1);
assertThat(rethrowingException.exceptionalPathYields().filter(y -> y.exceptionType(semanticModel).is("java.lang.Exception"))).hasSize(1);
assertThat(rethrowingException.exceptionalPathYields().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException"))).hasSize(1);
}
Aggregations