use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class IndentationCheck method visitLambdaExpression.
@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
// doesn't scan lambda parameters because there's no indentation check on types and identifiers
Tree body = lambdaExpressionTree.body();
if (body.is(Kind.BLOCK)) {
BlockTree block = (BlockTree) body;
excludeIssueAtLine = block.openBraceToken().line();
int previousLevel = expectedLevel;
expectedLevel = block.closeBraceToken().column();
scan(block);
expectedLevel = previousLevel;
} else {
scan(body);
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class LambdaOptionalParenthesisCheck method visitNode.
@Override
public void visitNode(Tree tree) {
LambdaExpressionTree let = (LambdaExpressionTree) tree;
SyntaxToken openParenToken = let.openParenToken();
if (openParenToken != null && let.parameters().size() == 1) {
VariableTree param = let.parameters().get(0);
String identifier = param.simpleName().name();
if (param.type().is(Tree.Kind.INFERED_TYPE)) {
reportIssue(openParenToken, "Remove the parentheses around the \"" + identifier + "\" parameter" + context.getJavaVersion().java8CompatibilityMessage());
}
}
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class SymbolTableTest method Lambdas.
@Test
public void Lambdas() throws Exception {
Result result = Result.createFor("Lambdas");
JavaSymbol barMethod = result.symbol("bar");
assertThat(barMethod.usages()).hasSize(1);
MethodTree methodTree = (MethodTree) barMethod.declaration();
Type Ftype = result.symbol("F").type();
assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
JavaSymbol qixMethod = result.symbol("qix");
LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
JavaSymbol fieldSymbol = result.symbol("field");
assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
JavaSymbol bSymbol = result.symbol("b");
assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
JavaSymbol condMethod = result.symbol("cond");
ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
JavaSymbol parenthMethod = result.symbol("parenth");
ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
JavaSymbol sym = result.symbol("o");
assertThat(sym.type.is("java.lang.Object")).isTrue();
assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
JavaSymbol operations = result.symbol("operations");
MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
JavaSymbol myStringParam = result.symbol("myStringParam");
Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
assertThat(stringParamMethod.usages()).hasSize(1);
assertThat(myStringParam.type.is("java.lang.String")).isTrue();
assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
assertThat(result.symbol("foo", 95).usages()).hasSize(1);
assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class SymbolTableTest method getRSubstitution.
private JavaType getRSubstitution(Result result, String symbolName) {
LambdaExpressionTree lambda = ((LambdaExpressionTree) result.symbol(symbolName).declaration().parent());
JavaType lambdaType = (JavaType) lambda.symbolType();
assertThat(lambdaType.isParameterized()).isTrue();
assertThat(lambdaType.is("java.util.function.Function")).isTrue();
TypeSubstitution typeSubstitution = ((ParametrizedTypeJavaType) lambdaType).typeSubstitution;
assertThat(typeSubstitution.size()).isEqualTo(2);
JavaType Tsubstitution = typeSubstitution.substitutedTypes().get(0);
// check that T -> ? super String
assertThat(Tsubstitution.isTagged(JavaType.WILDCARD)).isTrue();
assertThat(((WildCardType) Tsubstitution).boundType).isEqualTo(WildCardType.BoundType.SUPER);
assertThat(((WildCardType) Tsubstitution).bound.is("java.lang.String")).isTrue();
return typeSubstitution.substitutedTypes().get(1);
}
use of org.sonar.plugins.java.api.tree.LambdaExpressionTree in project sonar-java by SonarSource.
the class LambdaBlockReturnVisitorTest method test.
@Test
public void test() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + " java.util.function.Consumer<String> c = s -> {\n" + " if(s.length()>0) {\n" + " return;\n" + " }\n" + " System.out.println(s);\n" + " };\n" + "\n" + " java.util.function.Function<String, String> f = s -> {\n" + " if(s.length() > 0) {\n" + " return s.replace('a', 'b');\n" + " }\n" + " return unknownSymbol;\n" + " };\n" + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
assertThat(visitor.types).isEmpty();
visitor = new LambdaBlockReturnVisitor();
((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
assertThat(visitor.types).hasSize(1);
}
Aggregations