Search in sources :

Example 41 with MethodInvocationTree

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

the class JavaTreeModelTest method explicit_constructor_invocation.

/**
 * 8.8.7.1. Explicit Constructor Invocations
 */
@Test
public void explicit_constructor_invocation() {
    // TODO test NonWildTypeArguments
    MethodInvocationTree tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { this(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
    assertThat(tree.arguments()).hasSize(2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>this(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
    assertThat(tree.arguments()).hasSize(2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { super(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
    assertThat(tree.arguments()).hasSize(2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>super(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
    assertThat(tree.arguments()).hasSize(2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.super(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    MemberSelectExpressionTree methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(methodSelect, 3);
    assertThat(methodSelect.identifier().name()).isEqualTo("super");
    assertThat(methodSelect.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
    assertThat(tree.arguments()).hasSize(2);
    tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.<T>super(true, false); } }");
    assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
    methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
    assertThatChildrenIteratorHasSize(methodSelect, 3);
    assertThat(methodSelect.identifier().name()).isEqualTo("super");
    assertThat(methodSelect.operatorToken()).isNotNull();
    assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
    assertThat(tree.arguments()).hasSize(2);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 42 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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)

Example 43 with MethodInvocationTree

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

the class DataStoredInSessionCheck method checkArgument.

private void checkArgument(ExpressionTree argument, ExpressionTree startPoint, MethodInvocationTree reportTree) {
    ExpressionTree expressionToEvaluate = argument;
    if (expressionToEvaluate.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) expressionToEvaluate;
        identifiersUsedToSetAttribute.add(identifier);
        Symbol variable = identifier.symbol();
        ExpressionTree lastAssignmentOrDeclaration = ReassignmentFinder.getClosestReassignmentOrDeclarationExpression(startPoint, variable);
        if (lastAssignmentOrDeclaration != null && !usedBetween(variable, lastAssignmentOrDeclaration, startPoint)) {
            expressionToEvaluate = lastAssignmentOrDeclaration;
        }
    }
    if (isRequestOrCookieDataRetrieval(expressionToEvaluate)) {
        reportIssue(reportTree.methodSelect(), "Make sure the user is authenticated before this data is stored in the session.");
    } else if (expressionToEvaluate.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionToEvaluate;
        if (NO_EFFECT_OPERATION.anyMatch(mit)) {
            checkArgument(mit.arguments().get(0), mit, reportTree);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 44 with MethodInvocationTree

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

the class CollectionCallingItselfCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
        Symbol symbolReference = null;
        Symbol method = null;
        String reportedName = "";
        if (methodInvocationTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) methodInvocationTree.methodSelect();
            IdentifierTree identifier = mse.identifier();
            reportedName = identifier.name();
            method = identifier.symbol();
            if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
                symbolReference = ((IdentifierTree) mse.expression()).symbol();
            }
        }
        if (symbolReference != null && isMethodFromCollection(method)) {
            reportIssueForParameters(methodInvocationTree, symbolReference, reportedName);
        }
    }
}
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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 45 with MethodInvocationTree

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

the class JavaPropertiesHelperTest method null_if_variable_used_more_than_once.

@Test
public void null_if_variable_used_more_than_once() throws Exception {
    ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue); myValue = 0; } " + "java.util.Properties props = new java.util.Properties();" + "String myValue = props.getProperty(\"myKey\", \"defaultValue\");");
    ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
    assertThat(defaultValue).isNull();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Test(org.junit.Test)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7