Search in sources :

Example 1 with MethodReferenceTree

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

the class JavaTreeModelTest method method_reference_expression_should_not_break_AST.

@Test
public void method_reference_expression_should_not_break_AST() throws Exception {
    String code = "class T { public void meth(){IntStream.range(1,12).map(new MethodReferences()::<String>square).map(super::myMethod).map(int[]::new).forEach(System.out::println);}}";
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
    MethodReferenceTree mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.MEMBER_SELECT)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.ARRAY_TYPE)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThat(mrt.method().name()).isEqualTo("new");
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.IDENTIFIER)).isTrue();
    assertThat(((IdentifierTree) mrt.expression()).name()).isEqualTo("super");
    assertThat(mrt.doubleColon()).isNotNull();
    assertThatChildrenIteratorHasSize(mrt, 3);
    mit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression();
    mrt = (MethodReferenceTree) mit.arguments().get(0);
    assertThat(mrt.expression().is(Kind.NEW_CLASS)).isTrue();
    assertThat(mrt.doubleColon()).isNotNull();
    assertThat(mrt.typeArguments()).isNotNull();
    assertThat(mrt.method().name()).isEqualTo("square");
    assertThatChildrenIteratorHasSize(mrt, 4);
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) 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 2 with MethodReferenceTree

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

the class MethodReferenceResolutionTest method MethodReferenceUsingThis.

@Test
public void MethodReferenceUsingThis() throws Exception {
    Result result = Result.createFor("MethodReferencesThis");
    JavaSymbol bar1 = result.symbol("bar1");
    assertThat(bar1.usages()).hasSize(2);
    JavaSymbol bar = result.symbol("bar");
    assertThat(bar.usages()).hasSize(1);
    MethodReferenceTree methodRef = (MethodReferenceTree) bar.usages().get(0).parent();
    assertThat(methodRef.symbolType().is("java.util.function.Consumer")).isTrue();
    MethodInvocationTree foreach = (MethodInvocationTree) methodRef.parent().parent();
    assertThat(foreach.symbol().owner().type().is("java.lang.Iterable")).isTrue();
}
Also used : MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Example 3 with MethodReferenceTree

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

the class PreferStreamAnyMatchCheck method handleAnyMatch.

private void handleAnyMatch(MethodInvocationTree anyMatchMIT) {
    ExpressionTree predicate = anyMatchMIT.arguments().get(0);
    IdentifierTree reportTree = ExpressionUtils.methodName(anyMatchMIT);
    if (anyMatchMIT.parent().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
        if (predicate.is(Tree.Kind.LAMBDA_EXPRESSION) && ((LambdaExpressionTree) predicate).body().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
            // !stream.anyMatch(x -> !(...))
            context.reportIssue(this, reportTree, "Replace this double negation with \"allMatch()\" and positive predicate.");
        } else {
            context.reportIssue(this, reportTree, "Replace this negation and \"anyMatch()\" with \"noneMatch()\".");
        }
    }
    if (predicate.is(Tree.Kind.METHOD_REFERENCE) && isBooleanValueReference((MethodReferenceTree) predicate)) {
        previousMITInChain(anyMatchMIT).filter(MAP_METHODS::anyMatch).ifPresent(mapMIT -> context.reportIssue(this, reportTree, "Use mapper from \"map()\" directly as predicate in \"anyMatch()\"."));
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 4 with MethodReferenceTree

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

the class DeadStoreCheck method checkElement.

private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
    Set<Symbol> out = outVar;
    switch(element.kind()) {
        case PLUS_ASSIGNMENT:
        case DIVIDE_ASSIGNMENT:
        case MINUS_ASSIGNMENT:
        case MULTIPLY_ASSIGNMENT:
        case OR_ASSIGNMENT:
        case XOR_ASSIGNMENT:
        case AND_ASSIGNMENT:
        case LEFT_SHIFT_ASSIGNMENT:
        case RIGHT_SHIFT_ASSIGNMENT:
        case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
        case REMAINDER_ASSIGNMENT:
        case ASSIGNMENT:
            handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
            break;
        case IDENTIFIER:
            handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
            break;
        case VARIABLE:
            handleVariable(out, (VariableTree) element);
            break;
        case NEW_CLASS:
            handleNewClass(out, methodSymbol, (NewClassTree) element);
            break;
        case LAMBDA_EXPRESSION:
            LambdaExpressionTree lambda = (LambdaExpressionTree) element;
            out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
            break;
        case METHOD_REFERENCE:
            MethodReferenceTree methodRef = (MethodReferenceTree) element;
            out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
            break;
        case TRY_STATEMENT:
            handleTryStatement(out, methodSymbol, (TryStatementTree) element);
            break;
        case PREFIX_DECREMENT:
        case PREFIX_INCREMENT:
            handlePrefixExpression(out, (UnaryExpressionTree) element);
            break;
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            handlePostfixExpression(out, (UnaryExpressionTree) element);
            break;
        case CLASS:
        case ENUM:
        case ANNOTATION_TYPE:
        case INTERFACE:
            ClassTree classTree = (ClassTree) element;
            out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
            break;
        default:
    }
    return out;
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 5 with MethodReferenceTree

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

the class Resolve method findMethodReference.

Resolution findMethodReference(Env env, List<JavaType> samMethodArgs, MethodReferenceTree methodRefTree) {
    Tree expression = methodRefTree.expression();
    JavaType expressionType = (JavaType) ((AbstractTypedTree) expression).symbolType();
    String methodName = getMethodReferenceMethodName(methodRefTree.method().name());
    Resolution resolution = findMethod(env, expressionType, methodName, samMethodArgs);
    // JLS ยง15.13.1
    if (secondSearchRequired(expression, expressionType, resolution.symbol, samMethodArgs)) {
        resolution = findMethod(env, expressionType, methodName, samMethodArgs.stream().skip(1).collect(Collectors.toList()));
    }
    return resolution;
}
Also used : AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree)

Aggregations

MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)4 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 Test (org.junit.Test)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 HashSet (java.util.HashSet)1 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)1 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1