use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class NullableAnnotationUtilsTest method testEclipseIsGloballyAnnotatedNonNull.
@Test
public void testEclipseIsGloballyAnnotatedNonNull() {
List<File> classPath = new ArrayList<>(FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true));
classPath.add(new File("target/test-classes"));
// adding the class corresponding to package-info having @NonNullByDefault annotation
classPath.add(new File("src/test/files/se/annotations/eclipse"));
SemanticModel semanticModel = getSemanticModel("src/test/files/se/annotations/eclipse/org/foo/bar/Eclipse.java", classPath);
getMethods(semanticModel, "org.foo.bar.A").forEach(NullableAnnotationUtilsTest::testMethods);
getMethods(semanticModel, "org.foo.bar.B").forEach(NullableAnnotationUtilsTest::testMethods);
// fields not handled
assertThat(isAnnotatedNonNull(getSymbol(semanticModel, "org.foo.bar.B", "field"))).isFalse();
semanticModel = getSemanticModel("src/test/files/se/annotations/eclipse/org/foo/foo/Eclipse.java", classPath);
getMethods(semanticModel, "org.foo.foo.A").forEach(NullableAnnotationUtilsTest::testMethods);
semanticModel = getSemanticModel("src/test/files/se/annotations/eclipse/org/foo/qix/Eclipse.java", classPath);
getMethods(semanticModel, "org.foo.qix.A").forEach(NullableAnnotationUtilsTest::testMethods);
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class SETestUtils method createSymbolicExecutionVisitorAndSemantic.
public static Pair<SymbolicExecutionVisitor, SemanticModel> createSymbolicExecutionVisitorAndSemantic(String fileName, boolean crossFileEnabled, SECheck... checks) {
File file = new File(fileName);
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse(file);
SemanticModel semanticModel = SemanticModel.createFor(cut, CLASSLOADER);
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Arrays.asList(checks), new BehaviorCache(CLASSLOADER, crossFileEnabled));
sev.scanFile(new DefaultJavaFileScannerContext(cut, file, semanticModel, null, new JavaVersionImpl(8), true));
return new Pair<>(sev, semanticModel);
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class BytecodeEGWalker method handleMethodInvocation.
private boolean handleMethodInvocation(Instruction instruction) {
boolean isStatic = instruction.opcode == Opcodes.INVOKESTATIC;
int arity = isStatic ? instruction.arity() : (instruction.arity() + 1);
ProgramState.Pop pop = programState.unstackValue(arity);
Preconditions.checkState(pop.values.size() == arity, "Arguments mismatch for INVOKE");
// TODO use constraintManager.createMethodSymbolicValue to create relational SV for equals
programState = pop.state;
SymbolicValue returnSV = instruction.hasReturnValue() ? constraintManager.createSymbolicValue(instruction) : null;
String signature = instruction.fieldOrMethod.completeSignature();
MethodBehavior methodInvokedBehavior = behaviorCache.get(signature);
enqueueUncheckedExceptions();
// FIXME : empty yields here should not happen, for now act as if behavior was not resolved.
if (methodInvokedBehavior != null && methodInvokedBehavior.isComplete() && !methodInvokedBehavior.yields().isEmpty()) {
List<SymbolicValue> stack = Lists.reverse(pop.values);
if (!isStatic) {
// remove "thisSV" from stack before trying to apply any yield, as it should not match with arguments
stack = stack.subList(1, stack.size());
}
List<SymbolicValue> arguments = stack;
methodInvokedBehavior.happyPathYields().forEach(yield -> yield.statesAfterInvocation(arguments, Collections.emptyList(), programState, () -> returnSV).forEach(ps -> {
checkerDispatcher.methodYield = yield;
if (ps.peekValue() != null) {
ps = setDoubleOrLong(ps, ps.peekValue(), instruction.isLongOrDoubleValue());
}
checkerDispatcher.addTransition(ps);
checkerDispatcher.methodYield = null;
}));
methodInvokedBehavior.exceptionalPathYields().forEach(yield -> {
Type exceptionType = yield.exceptionType(semanticModel);
yield.statesAfterInvocation(arguments, Collections.emptyList(), programState, () -> constraintManager.createExceptionalSymbolicValue(exceptionType)).forEach(ps -> {
ps.storeExitValue();
enqueueExceptionHandlers(exceptionType, ps);
});
});
return true;
}
if (methodInvokedBehavior != null) {
methodInvokedBehavior.getDeclaredExceptions().forEach(exception -> {
Type exceptionType = semanticModel.getClassType(exception);
ProgramState ps = programState.stackValue(constraintManager.createExceptionalSymbolicValue(exceptionType));
enqueueExceptionHandlers(exceptionType, ps);
});
}
if (instruction.hasReturnValue()) {
programState = programState.stackValue(returnSV);
programState = setDoubleOrLong(returnSV, instruction.isLongOrDoubleValue());
}
return false;
}
use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.
the class ExceptionalYieldTest method exceptional_yields_void_method.
@Test
public void exceptional_yields_void_method() {
Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/ExceptionalYieldsVoidMethod.java");
SymbolicExecutionVisitor sev = sevAndSemantic.a;
SemanticModel semanticModel = sevAndSemantic.b;
MethodBehavior mb = getMethodBehavior(sev, "myVoidMethod");
assertThat(mb.yields()).hasSize(4);
List<ExceptionalYield> exceptionalYields = mb.exceptionalPathYields().collect(Collectors.toList());
assertThat(exceptionalYields).hasSize(3);
assertThat(exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown()).count()).isEqualTo(1);
MethodYield explicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException1")).findAny().get();
assertThat(explicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NULL);
MethodYield implicitExceptionYield = exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).is("org.foo.MyException2")).findAny().get();
assertThat(implicitExceptionYield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
}
Aggregations