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);
}
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();
}
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()\"."));
}
}
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;
}
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;
}
Aggregations