Search in sources :

Example 6 with ExpressionStatementTree

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

the class OverwrittenKeyCheck method isMapPut.

@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
            MethodInvocationTree mapPut = (MethodInvocationTree) expression;
            Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
            ExpressionTree keyTree = mapPut.arguments().get(0);
            Object key = extractKey(keyTree);
            if (collection != null && key != null) {
                return new CollectionAndKey(collection, keyTree, key, false, null);
            }
        }
    }
    return null;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) 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) CheckForNull(javax.annotation.CheckForNull)

Example 7 with ExpressionStatementTree

use of org.sonar.plugins.java.api.tree.ExpressionStatementTree 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 8 with ExpressionStatementTree

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

the class JavaTreeModelTest method postfix_expression.

/**
 * 15.14. Postfix Expressions
 */
@Test
public void postfix_expression() {
    UnaryExpressionTree tree;
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i++; } }"))).expression();
    assertThat(tree.is(Tree.Kind.POSTFIX_INCREMENT)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("++");
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i--; } }"))).expression();
    assertThat(tree.is(Tree.Kind.POSTFIX_DECREMENT)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.operatorToken().text()).isEqualTo("--");
    assertThatChildrenIteratorHasSize(tree, 2);
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Example 9 with ExpressionStatementTree

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

the class JavaTreeModelTest method expression_statement.

/**
 * 14.8. Expression Statements
 */
@Test
public void expression_statement() {
    ExpressionStatementTree tree = (ExpressionStatementTree) firstMethodFirstStatement("class T { void m() { i++; } }");
    assertThat(tree.is(Tree.Kind.EXPRESSION_STATEMENT)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.semicolonToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 2);
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Example 10 with ExpressionStatementTree

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

the class JavaTreeModelTest method lambda_expressions.

@Test
public void lambda_expressions() {
    String code = "class T { public void meth(){IntStream.range(1,12).map(x->x*x).map((int a)-> {return a*a;});}}";
    ExpressionTree expressionTree = ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
    // parsing not broken by lambda
    assertThat(expressionTree).isNotNull();
    MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
    LambdaExpressionTree tree = (LambdaExpressionTree) mit.arguments().get(0);
    assertThat(tree.openParenToken()).isNotNull();
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.closeParenToken()).isNotNull();
    assertThat(tree.arrowToken()).isNotNull();
    assertThat(tree.body().is(Tree.Kind.BLOCK)).isTrue();
    tree = (LambdaExpressionTree) ((MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression()).arguments().get(0);
    assertThat(tree.openParenToken()).isNull();
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.closeParenToken()).isNull();
    assertThat(tree.arrowToken()).isNotNull();
    assertThat(tree.body().is(Tree.Kind.MULTIPLY)).isTrue();
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Aggregations

ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)21 Test (org.junit.Test)11 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)11 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)11 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)10 Symbol (org.sonar.plugins.java.api.semantic.Symbol)7 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)7 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)6 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)6 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)4 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)3 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 CheckForNull (javax.annotation.CheckForNull)2 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)2