Search in sources :

Example 91 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method ignore_assignation_after_starting_point.

@Test
public void ignore_assignation_after_starting_point() throws Exception {
    String code = newCode("int foo() {", "  int b = 0;", "  doSomething(b);", "  b = 1;", "}");
    List<StatementTree> statements = methodBody(code);
    Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
    MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
    Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
    assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 92 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class ReassignmentFinderTest method ignore_assignation_after_starting_point_same_line.

@Test
public void ignore_assignation_after_starting_point_same_line() throws Exception {
    String code = newCode("int foo() {", "  int b = 0;", "  doSomething(b); b = 1;", "}");
    List<StatementTree> statements = methodBody(code);
    Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
    MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
    Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
    assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 93 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class ExplodedGraphWalker method learnIdentifierConstraints.

private void learnIdentifierConstraints(IdentifierTree tree, SymbolicValue sv) {
    if (THIS_SUPER.contains(tree.name())) {
        programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
        return;
    }
    Tree declaration = tree.symbol().declaration();
    if (!isFinalField(tree.symbol()) || declaration == null) {
        return;
    }
    VariableTree variableTree = (VariableTree) declaration;
    ExpressionTree initializer = variableTree.initializer();
    if (initializer == null) {
        return;
    }
    // only check final field with an initializer
    initializer = ExpressionUtils.skipParentheses(initializer);
    if (initializer.is(Tree.Kind.NULL_LITERAL)) {
        programState = programState.addConstraint(sv, ObjectConstraint.NULL);
    } else if (initializer.is(Tree.Kind.NEW_CLASS, Tree.Kind.NEW_ARRAY, Tree.Kind.STRING_LITERAL) || isNonNullMethodInvocation(initializer) || variableTree.symbol().type().isPrimitive() || initializer.symbolType().isPrimitive()) {
        programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)

Example 94 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class FlowComputation method flowsForArgumentsChangingName.

public static Flow flowsForArgumentsChangingName(List<Integer> argumentIndices, MethodInvocationTree mit) {
    JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
    MethodTree declaration = methodSymbol.declaration();
    if (declaration == null) {
        return Flow.empty();
    }
    Flow.Builder flowBuilder = Flow.builder();
    List<VariableTree> methodParameters = declaration.parameters();
    for (Integer argumentIndex : argumentIndices) {
        // do not consider varargs part
        if (methodSymbol.isVarArgs() && argumentIndex >= methodParameters.size() - 1) {
            break;
        }
        IdentifierTree argumentName = getArgumentIdentifier(mit, argumentIndex);
        if (argumentName != null) {
            IdentifierTree parameterIdentifier = methodParameters.get(argumentIndex).simpleName();
            String identifierName = parameterIdentifier.name();
            if (!argumentName.name().equals(identifierName)) {
                flowBuilder.add(new JavaFileScannerContext.Location(String.format(IMPLIES_SAME_VALUE, identifierName, argumentName.name()), parameterIdentifier));
            }
        }
    }
    return flowBuilder.build().reverse();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext)

Example 95 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method resolveMethodSymbol.

@CheckForNull
private Resolve.Resolution resolveMethodSymbol(Tree methodSelect, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes) {
    Resolve.Resolution resolution;
    IdentifierTree identifier;
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
        resolveAs(mset.expression(), JavaSymbol.TYP | JavaSymbol.VAR);
        JavaType type = getType(mset.expression());
        if (type.isTagged(JavaType.DEFERRED)) {
            return null;
        }
        identifier = mset.identifier();
        resolution = resolve.findMethod(methodEnv, type, identifier.name(), argTypes, typeParamTypes);
    } else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) methodSelect;
        resolution = resolve.findMethod(methodEnv, identifier.name(), argTypes, typeParamTypes);
    } else {
        throw new IllegalStateException("Method select in method invocation is not of the expected type " + methodSelect);
    }
    registerType(identifier, resolution.type());
    associateReference(identifier, resolution.symbol());
    return resolution;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) CheckForNull(javax.annotation.CheckForNull)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7