Search in sources :

Example 1 with AssignmentExpressionTree

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

the class AnnotationArgumentOrderCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    List<String> declarationNames = new ArrayList<>();
    for (Symbol symbol : annotationSymbol.memberSymbols()) {
        declarationNames.add(symbol.name());
    }
    List<String> annotationArguments = new ArrayList<>();
    for (ExpressionTree argument : annotationTree.arguments()) {
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            annotationArguments.add(nameTree.name());
        }
    }
    declarationNames.retainAll(annotationArguments);
    if (!declarationNames.equals(annotationArguments)) {
        reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) 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) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 2 with AssignmentExpressionTree

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

the class CastArithmeticOperandCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree aet) {
    if (aet.is(Tree.Kind.ASSIGNMENT)) {
        Type varType = aet.symbolType();
        ExpressionTree expr = aet.expression();
        checkExpression(varType, expr);
    }
    super.visitAssignmentExpression(aet);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 3 with AssignmentExpressionTree

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

the class AssignmentInSubExpressionCheck method skipChainedAssignments.

private ExpressionTree skipChainedAssignments(ExpressionTree expressionTree) {
    ExpressionTree tree = ExpressionUtils.skipParentheses(expressionTree);
    while (tree instanceof AssignmentExpressionTree) {
        AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
        scan(assignmentExpressionTree.variable());
        tree = ExpressionUtils.skipParentheses(assignmentExpressionTree.expression());
    }
    return tree;
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) 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 4 with AssignmentExpressionTree

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

the class ExpressionUtilsTest method test_simple_assignments.

@Test
public void test_simple_assignments() throws Exception {
    File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(1);
    List<AssignmentExpressionTree> assignments = findAssignmentExpressionTrees(methodTree);
    assertThat(assignments).hasSize(4);
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(3))).isFalse();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) File(java.io.File) Test(org.junit.Test)

Example 5 with AssignmentExpressionTree

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

the class ReassignmentFinder method getClosestReassignmentOrDeclarationExpression.

@CheckForNull
public static ExpressionTree getClosestReassignmentOrDeclarationExpression(Tree startingPoint, Symbol referenceSymbol) {
    Tree result = referenceSymbol.declaration();
    List<IdentifierTree> usages = referenceSymbol.usages();
    if (usages.size() != 1) {
        List<AssignmentExpressionTree> reassignments = getReassignments(referenceSymbol.owner().declaration(), usages);
        SyntaxToken startPointToken = startingPoint.firstToken();
        Tree lastReassignment = getClosestReassignment(startPointToken, reassignments);
        if (lastReassignment != null) {
            result = lastReassignment;
        }
    }
    ExpressionTree initializerOrExpression = getInitializerOrExpression(result);
    if (initializerOrExpression == startingPoint) {
        return getClosestReassignmentOrDeclarationExpression(result, referenceSymbol);
    }
    return initializerOrExpression;
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) 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) 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) CheckForNull(javax.annotation.CheckForNull)

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