use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitReturnStatement.
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
super.visitReturnStatement(tree);
ExpressionTree expression = tree.expression();
if (expression != null && ((JavaType) expression.symbolType()).isTagged(JavaType.DEFERRED)) {
// get owner of return (method or lambda)
Tree parent = tree.parent();
while (!parent.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
parent = parent.parent();
if (parent == null) {
throw new IllegalStateException("Return statement was unexpected here");
}
}
Type infered;
if (parent.is(Tree.Kind.METHOD)) {
infered = ((MethodTree) parent).returnType().symbolType();
} else {
infered = ((LambdaExpressionTree) parent).symbolType();
}
setInferedType(infered, (DeferredType) expression.symbolType());
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class LambdaSingleExpressionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
Tree lambdaBody = lambdaExpressionTree.body();
if (isBlockWithOneStatement(lambdaBody)) {
String message = "Remove useless curly braces around statement";
if (singleStatementIsReturn(lambdaExpressionTree)) {
message += " and then remove useless return keyword";
}
reportIssue(((BlockTree) lambdaBody).openBraceToken(), message + context.getJavaVersion().java8CompatibilityMessage());
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class LambdaTypeParameterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
List<VariableTree> parameters = lambdaExpressionTree.parameters();
if (parameters.size() <= 2 && !lambdaExpressionTree.body().is(Tree.Kind.BLOCK)) {
// ignore lambdas with one or two params and a non-block body
return;
}
String missingTypeParameters = parameters.stream().filter(variable -> variable.type().is(Tree.Kind.INFERED_TYPE)).map(VariableTree::simpleName).map(IdentifierTree::name).map(parameterName -> "'" + parameterName + "'").collect(Collectors.joining(", "));
if (!missingTypeParameters.isEmpty()) {
reportIssue(parameters.get(0), Iterables.getLast(parameters), String.format("Specify a type for: %s", missingTypeParameters));
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class LiveVariables method processBlockElements.
private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
// process elements from bottom to top
Set<Tree> assignmentLHS = new HashSet<>();
for (Tree element : Lists.reverse(block.elements())) {
switch(element.kind()) {
case ASSIGNMENT:
processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
break;
case IDENTIFIER:
processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
break;
case MEMBER_SELECT:
processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
break;
case VARIABLE:
blockKill.add(((VariableTree) element).symbol());
blockGen.remove(((VariableTree) element).symbol());
break;
case LAMBDA_EXPRESSION:
blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
break;
case METHOD_REFERENCE:
blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
break;
case NEW_CLASS:
blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
break;
default:
}
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class SymbolTableTest method double_lambda_type_propagation.
@Test
public void double_lambda_type_propagation() {
Result result = Result.createFor("DoubleLambda");
JavaSymbol my = result.symbol("my");
assertThat(my.usages()).hasSize(2);
LambdaExpressionTree outerLambda = ((LambdaExpressionTree) result.symbol("l").declaration().parent());
Type nestedLambdaType = ((ExpressionTree) outerLambda.body()).symbolType();
assertThat(nestedLambdaType.is("Issue$Flow")).as("Issue$Flow was expected but got : " + nestedLambdaType.fullyQualifiedName()).isTrue();
}
Aggregations