Search in sources :

Example 6 with AssignmentExpressionTree

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

the class ReassignmentFinder method checkAssignment.

private static Optional<AssignmentExpressionTree> checkAssignment(IdentifierTree usage) {
    Tree previousTree = usage;
    Tree nonParenthesisParent = previousTree.parent();
    while (nonParenthesisParent.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
        previousTree = nonParenthesisParent;
        nonParenthesisParent = previousTree.parent();
    }
    if (nonParenthesisParent instanceof AssignmentExpressionTree) {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) nonParenthesisParent;
        if (assignment.variable().equals(previousTree)) {
            return Optional.of(assignment);
        }
    }
    return Optional.empty();
}
Also used : Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 7 with AssignmentExpressionTree

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

the class ReassignmentFinder method getClosestReassignment.

@CheckForNull
private static Tree getClosestReassignment(SyntaxToken startToken, List<AssignmentExpressionTree> reassignments) {
    Tree result = null;
    List<Tree> assignmentsBeforeStartToken = reassignments.stream().sorted(ReassignmentFinder::isBefore).filter(a -> isBefore(startToken, a) > 0).collect(Collectors.toList());
    if (!assignmentsBeforeStartToken.isEmpty()) {
        return assignmentsBeforeStartToken.get(assignmentsBeforeStartToken.size() - 1);
    }
    return result;
}
Also used : Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Optional(java.util.Optional) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull)

Example 8 with AssignmentExpressionTree

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

the class UselessIncrementCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
        ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
        if (returnExpression != null && isPostfix(returnExpression)) {
            UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
            ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
            if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
                reportIssue(unaryExpression);
            }
        }
    } else {
        AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
        if (isPostfix(aet.expression())) {
            UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
            if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
                reportIssue(postfix);
            }
        }
    }
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 9 with AssignmentExpressionTree

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

the class SerializableFieldInSerializableClassCheck method checkCollectionAssignments.

private void checkCollectionAssignments(List<IdentifierTree> usages) {
    for (IdentifierTree usage : usages) {
        Tree parentTree = usage.parent();
        if (parentTree.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) parentTree;
            ExpressionTree expression = assignment.expression();
            if (usage.equals(assignment.variable()) && !expression.is(Tree.Kind.NULL_LITERAL) && isUnserializableCollection(expression.symbolType())) {
                reportIssue(usage);
            }
        }
    }
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 10 with AssignmentExpressionTree

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

the class WrongAssignmentOperatorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AssignmentExpressionTree aeTree = (AssignmentExpressionTree) tree;
    SyntaxToken operatorToken = aeTree.operatorToken();
    SyntaxToken expressionFirstToken = aeTree.expression().firstToken();
    SyntaxToken variableLastToken = aeTree.variable().lastToken();
    if (isSuspiciousToken(expressionFirstToken) && noSpacingBetween(operatorToken, expressionFirstToken) && !noSpacingBetween(variableLastToken, operatorToken)) {
        reportIssue(operatorToken, expressionFirstToken, getMessage(expressionFirstToken, aeTree));
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

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