Search in sources :

Example 31 with AssignmentExpressionTree

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

the class ExpressionUtilsTest method test_extract_identifier_mixed_access.

@Test
public void test_extract_identifier_mixed_access() throws Exception {
    File file = new File("src/test/files/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);
    // This should reflect method 'mixedReference'.
    assertThat(assignments).hasSize(4);
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
    // Contains method invocation.
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
    // Compound assignment
    assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
    // The returned identifier should have the same symbol regardless of the explicit usage of this.
    assertThat(ExpressionUtils.extractIdentifier(assignments.get(0)).symbol()).isEqualTo(ExpressionUtils.extractIdentifier(assignments.get(1)).symbol());
}
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 32 with AssignmentExpressionTree

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

the class ImmediateReverseBoxingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        visitMethodInvocationTree((MethodInvocationTree) tree);
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        ExpressionTree initializer = variableTree.initializer();
        if (initializer != null) {
            checkExpression(initializer, variableTree.type().symbolType());
        }
    } else if (tree.is(Tree.Kind.ASSIGNMENT)) {
        AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
        checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
    } else {
        NewClassTree newClassTree = (NewClassTree) tree;
        Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
        if (classSymbol != null) {
            ExpressionTree arg0 = newClassTree.arguments().get(0);
            checkForUnboxing(arg0);
            checkForUselessUnboxing(newClassTree.symbolType(), newClassTree.identifier(), arg0);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 33 with AssignmentExpressionTree

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

the class UCFGJavaVisitor method buildCall.

private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
    if (isStringVarDecl(element)) {
        VariableTree variableTree = (VariableTree) element;
        String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
        if (!idGenerator.isConst(lhs)) {
            ExpressionTree initializer = variableTree.initializer();
            String source = idGenerator.lookupIdFor(initializer);
            blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
        }
        return;
    }
    if (element.is(METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
        buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
    } else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
        if (element.is(PLUS)) {
            BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
            Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
            Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
            Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
            blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
        } else if (element.is(PLUS_ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                idGenerator.varForExpression(element, ((Expression.Variable) var).id());
                blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
            }
        } else if (element.is(ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
            }
        }
    }
}
Also used : Expression(org.sonar.ucfg.Expression) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) 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) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 34 with AssignmentExpressionTree

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

the class SyntaxTreeNameFinderTest method testFieldAccess.

@Test
public void testFieldAccess() {
    MethodTree tree = buildSyntaxTree("public void test() {this.field = value;} Object field;");
    BlockTree block = tree.block();
    AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
    tree = buildSyntaxTree("public void test() {super.field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
    tree = buildSyntaxTree("public void test() {A.field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("A");
    tree = buildSyntaxTree("public void test() {foo().field = value;}");
    block = tree.block();
    assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
    assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("foo");
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) 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