use of org.sonar.plugins.java.api.tree.LambdaExpressionTree 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.LambdaExpressionTree in project sonar-java by SonarSource.
the class MethodWithExcessiveReturnsCheck method leaveNode.
@Override
public void leaveNode(Tree tree) {
Tree reportTree = null;
if (tree.is(Tree.Kind.METHOD)) {
reportTree = ((MethodTree) tree).simpleName();
} else if (tree.is(Tree.Kind.LAMBDA_EXPRESSION)) {
reportTree = ((LambdaExpressionTree) tree).arrowToken();
}
if (reportTree != null) {
int count = returnStatementCounter.count(tree);
if (count > max) {
reportIssue(reportTree, "Reduce the number of returns of this method " + count + ", down to the maximum allowed " + max + ".");
}
methods.pop();
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree 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.LambdaExpressionTree in project sonar-java by SonarSource.
the class ReplaceLambdaByMethodRefCheck method getNullCheck.
private static Optional<String> getNullCheck(@Nullable Tree statement, LambdaExpressionTree tree) {
if (statement == null) {
return Optional.empty();
}
Tree expr = statement;
if (expr.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
expr = ExpressionUtils.skipParentheses((ParenthesizedTree) statement);
}
if (expr.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
BinaryExpressionTree bet = (BinaryExpressionTree) expr;
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
if (nullAgainstParam(leftOperand, rightOperand, tree) || nullAgainstParam(rightOperand, leftOperand, tree)) {
return Optional.of(expr.is(Tree.Kind.EQUAL_TO) ? "isNull" : "nonNull");
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree 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();
}
Aggregations