Search in sources :

Example 21 with AssignmentExpressionTree

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

the class StringConcatenationInLoopCheck method isNotLoopLocalVar.

private boolean isNotLoopLocalVar(AssignmentExpressionTree tree) {
    IdentifierTree idTree = getIdentifierTree(tree.variable());
    Tree envTree = semanticModel.getTree(semanticModel.getEnv(idTree.symbol()));
    Tree loopTree = loopLevel.peek();
    return envTree == null || !(envTree.equals(loopTree) || envTree.equals(loopStatement(loopTree)));
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 22 with AssignmentExpressionTree

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

the class StaticFieldInitializationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    switch(tree.kind()) {
        case CLASS:
            classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
            break;
        case STATIC_INITIALIZER:
            withinStaticInitializer.push(true);
            break;
        case METHOD:
            methodUsesLocks.push(false);
            break;
        case METHOD_INVOCATION:
            if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
                methodUsesLocks.pop();
                methodUsesLocks.push(true);
            }
            break;
        case ASSIGNMENT:
            AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
            if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
                IdentifierTree variable = (IdentifierTree) aet.variable();
                if (isStaticNotVolatileObject(variable)) {
                    reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
                }
            }
            break;
        default:
    }
    super.visitNode(tree);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 23 with AssignmentExpressionTree

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

the class SillyBitOperationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expression;
    SyntaxToken operatorToken;
    if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
        BinaryExpressionTree binary = (BinaryExpressionTree) tree;
        expression = binary.rightOperand();
        operatorToken = binary.operatorToken();
    } else {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
        expression = assignment.expression();
        operatorToken = assignment.operatorToken();
    }
    Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
    if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
        reportIssue(operatorToken, "Remove this silly bit operation.");
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 24 with AssignmentExpressionTree

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

the class ShiftOnIntOrLongCheck method checkShiftTree.

private void checkShiftTree(Tree tree, int treeIndex) {
    String identifier;
    ExpressionTree shift;
    SyntaxToken operatorToken;
    if (tree.is(Kind.LEFT_SHIFT, Kind.RIGHT_SHIFT)) {
        BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
        if (isZeroMaskShift(binaryExpressionTree)) {
            // No issue should be reported for "1 << 0" or "1 >> 0"
            return;
        }
        identifier = getIdentifierName(binaryExpressionTree.leftOperand());
        shift = binaryExpressionTree.rightOperand();
        operatorToken = binaryExpressionTree.operatorToken();
    } else {
        AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
        identifier = getIdentifierName(assignmentExpressionTree.variable());
        shift = assignmentExpressionTree.expression();
        operatorToken = assignmentExpressionTree.operatorToken();
    }
    checkShift((ExpressionTree) tree, shift, identifier, operatorToken, treeIndex);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 25 with AssignmentExpressionTree

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

the class JavaTreeModelTest method assignment_expression.

/**
 * 15.26. Assignment Operators
 */
@Test
public void assignment_expression() {
    String code = "class T { void m() { a += 42; } }";
    AssignmentExpressionTree tree = (AssignmentExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
    assertThat(tree.is(Tree.Kind.PLUS_ASSIGNMENT)).isTrue();
    assertThat(tree.variable()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("+=");
    assertThat(tree.expression()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

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