Search in sources :

Example 6 with ObjectConstraint

use of org.sonar.java.se.constraint.ObjectConstraint in project sonar-java by SonarSource.

the class ParameterNullnessCheck method checkParameters.

private void checkParameters(Tree syntaxNode, Symbol symbol, Arguments arguments, ProgramState state) {
    if (!symbol.isMethodSymbol() || arguments.isEmpty()) {
        return;
    }
    JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) symbol;
    if (nonNullAnnotationOnParameters(methodSymbol) == null) {
        // method is not annotated (locally or globally)
        return;
    }
    int nbArguments = arguments.size();
    List<SymbolicValue> argumentSVs = getArgumentSVs(state, syntaxNode, nbArguments);
    List<JavaSymbol> argumentSymbols = methodSymbol.getParameters().scopeSymbols();
    int nbArgumentToCheck = Math.min(nbArguments, argumentSymbols.size() - (methodSymbol.isVarArgs() ? 1 : 0));
    for (int i = 0; i < nbArgumentToCheck; i++) {
        ObjectConstraint constraint = state.getConstraint(argumentSVs.get(i), ObjectConstraint.class);
        if (constraint != null && constraint.isNull() && !parameterIsNullable(methodSymbol, argumentSymbols.get(i))) {
            reportIssue(syntaxNode, arguments.get(i), methodSymbol);
        }
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint)

Aggregations

ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)6 ProgramState (org.sonar.java.se.ProgramState)4 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)4 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 SemanticModel (org.sonar.java.resolve.SemanticModel)2 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)2 Constraint (org.sonar.java.se.constraint.Constraint)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 ByteStreams (com.google.common.io.ByteStreams)1 File (java.io.File)1 Collection (java.util.Collection)1 Nullable (javax.annotation.Nullable)1 FileUtils (org.apache.commons.io.FileUtils)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThat (org.fest.assertions.Assertions.assertThat)1 BeforeClass (org.junit.BeforeClass)1