Search in sources :

Example 1 with JavaSymbol

use of org.sonar.java.resolve.JavaSymbol 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)

Example 2 with JavaSymbol

use of org.sonar.java.resolve.JavaSymbol in project sonar-java by SonarSource.

the class ProgramStateTest method testToString.

@Test
public void testToString() {
    SymbolicValue sv3 = new SymbolicValue() {

        @Override
        public String toString() {
            return "SV_3";
        }
    };
    ProgramState state = ProgramState.EMPTY_STATE.stackValue(sv3);
    Symbol variable = new JavaSymbol.VariableJavaSymbol(0, "x", new JavaSymbol(JavaSymbol.TYP, 0, "A", Symbols.unknownSymbol));
    SymbolicValue sv4 = new SymbolicValue() {

        @Override
        public String toString() {
            return "SV_4";
        }
    };
    state = state.put(variable, sv4);
    SymbolicValue sv5 = new SymbolicValue() {

        @Override
        public String toString() {
            return "SV_5";
        }
    };
    state = state.stackValue(sv5, variable);
    // FIXME to string is not really nice by displaying classes and order is not guaranteed.
    assertThat(state.toString()).contains("A#x->SV_4", "SV_NULL", "SV_TRUE", "SV_FALSE", "A#x->SV_5", "SV_3");
// .isEqualTo("{ A#x->SV_4}  { SV_0_NULL-> class org.sonar.java.se.constraint.ObjectConstraint->NULL SV_1_TRUE-> class org.sonar.java.se.constraint.BooleanConstraint->TRUE class org.sonar.java.se.constraint.ObjectConstraint->NOT_NULL SV_2_FALSE-> class org.sonar.java.se.constraint.BooleanConstraint->FALSE class org.sonar.java.se.constraint.ObjectConstraint->NOT_NULL} { [SV_5, SV_3] } { A#x } ");
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 3 with JavaSymbol

use of org.sonar.java.resolve.JavaSymbol in project sonar-java by SonarSource.

the class RelationalSymbolicValueTest method test_to_string.

@Test
public void test_to_string() throws Exception {
    RelationalSymbolicValue rsv = new RelationalSymbolicValue(EQUAL);
    Symbol var = new JavaSymbol.VariableJavaSymbol(0, "x", new JavaSymbol(JavaSymbol.TYP, 0, "A", Symbols.unknownSymbol));
    SymbolicValue left = new SymbolicValue() {

        @Override
        public String toString() {
            return "left";
        }
    };
    SymbolicValue right = new SymbolicValue() {

        @Override
        public String toString() {
            return "right";
        }
    };
    rsv.computedFrom(ImmutableList.of(new ProgramState.SymbolicValueSymbol(right, null), new ProgramState.SymbolicValueSymbol(left, var)));
    assertThat(rsv).hasToString("left(A#x)==right");
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Aggregations

JavaSymbol (org.sonar.java.resolve.JavaSymbol)3 Test (org.junit.Test)2 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)2 Symbol (org.sonar.plugins.java.api.semantic.Symbol)2 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)1 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)1