Search in sources :

Example 16 with AssignmentExpressionTree

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

the class MutableMembersUsageCheck method assignementsOfMutableType.

private static boolean assignementsOfMutableType(List<IdentifierTree> usages) {
    for (IdentifierTree usage : usages) {
        Tree current = usage;
        Tree parent = usage.parent();
        do {
            if (parent.is(Tree.Kind.ASSIGNMENT)) {
                break;
            }
            current = parent;
            parent = current.parent();
        } while (parent != null);
        if (parent != null) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
            if (assignment.variable().equals(current) && isMutableType(assignment.expression())) {
                return true;
            }
        }
    }
    return false;
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 17 with AssignmentExpressionTree

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

the class OverwrittenKeyCheck method isArrayAssignment.

@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
            ExpressionTree variable = assignment.variable();
            if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
                Symbol collection = symbolFromIdentifier(aaet.expression());
                ExpressionTree keyTree = aaet.dimension().expression();
                Object key = extractKey(keyTree);
                if (collection != null && key != null) {
                    return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
                }
            }
        }
    }
    return null;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 18 with AssignmentExpressionTree

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

the class ParameterReassignedToCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) variable;
        Symbol reference = identifier.symbol();
        if (reference.isVariableSymbol() && variables.contains(reference)) {
            context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 19 with AssignmentExpressionTree

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

the class AnnotationDefaultArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
    for (ExpressionTree argument : annotationTree.arguments()) {
        Tree valueSet = argument;
        // Single element annotation : JLS8 9.7.3 : one param must be named value.
        String paramName = "value";
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            paramName = nameTree.name();
            valueSet = assignmentTree.expression();
        }
        if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
            reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
        }
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LiteralUtils(org.sonar.java.model.LiteralUtils) Map(java.util.Map) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 20 with AssignmentExpressionTree

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

the class DeadStoreCheck method handleAssignment.

private void handleAssignment(Set<Symbol> out, Set<Tree> assignmentLHS, AssignmentExpressionTree element) {
    ExpressionTree lhs = ExpressionUtils.skipParentheses(element.variable());
    if (lhs.is(Tree.Kind.IDENTIFIER)) {
        Symbol symbol = ((IdentifierTree) lhs).symbol();
        if (isLocalVariable(symbol) && !out.contains(symbol) && (element.is(Tree.Kind.ASSIGNMENT) || isParentExpressionStatement(element))) {
            createIssue(element.operatorToken(), element.expression(), symbol);
        }
        assignmentLHS.add(lhs);
        if (element.is(Tree.Kind.ASSIGNMENT)) {
            out.remove(symbol);
        } else {
            out.add(symbol);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)34 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)28 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)13 Symbol (org.sonar.plugins.java.api.semantic.Symbol)11 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 Tree (org.sonar.plugins.java.api.tree.Tree)9 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)9 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)5 Test (org.junit.Test)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 CheckForNull (javax.annotation.CheckForNull)3 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)3 File (java.io.File)2