use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class ModulusEqualityCheck method isMethodParameter.
private boolean isMethodParameter(ExpressionTree expressionTree) {
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) expressionTree;
Symbol symbol = identifier.symbol();
return methodParams.contains(symbol);
} else if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) expressionTree;
return isMethodParameter(memberSelectExpressionTree.expression());
} else if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
return isMethodParameter(methodInvocationTree.methodSelect());
}
return false;
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method infer_method_invocation_return_type.
@Test
public void infer_method_invocation_return_type() throws Exception {
Result result = Result.createFor("CollectorsToList");
List<IdentifierTree> usages = result.symbol("foo").usages();
assertThat(usages).hasSize(1);
ExpressionTree arg = ((MethodInvocationTree) usages.get(0).parent()).arguments().get(0);
assertThat(arg.symbolType().is("java.util.List")).isTrue();
assertThat(((JavaType) arg.symbolType()).isParameterized()).isTrue();
assertThat(((ParametrizedTypeJavaType) arg.symbolType()).typeSubstitution.substitutedTypes()).hasSize(1);
assertThat(((ParametrizedTypeJavaType) arg.symbolType()).typeSubstitution.substitutedTypes().get(0).is("java.lang.String")).isTrue();
usages = result.symbol("foo2").usages();
assertThat(usages).hasSize(1);
arg = ((MethodInvocationTree) usages.get(0).parent()).arguments().get(0);
assertThat(arg.symbolType().is("java.util.LinkedHashSet")).overridingErrorMessage("Expected java.util.Collection but got " + arg.symbolType().name()).isTrue();
assertThat(((JavaType) arg.symbolType()).isParameterized()).isTrue();
assertThat(((ParametrizedTypeJavaType) arg.symbolType()).typeSubstitution.substitutedTypes()).hasSize(1);
assertThat(((ParametrizedTypeJavaType) arg.symbolType()).typeSubstitution.substitutedTypes().get(0).is("java.lang.String")).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method infer_fully_lambda_types.
@Test
public void infer_fully_lambda_types() {
Result result = Result.createFor("InferLambdaType");
// Check lambda with a block return type.
assertThat(getRSubstitution(result, "line0").is("java.lang.String[]")).isTrue();
// Check lambda with a block with multiple return (using lub).
assertThat(getRSubstitution(result, "line1").is("java.lang.Number")).isTrue();
// Check lambda with a block only throwing.
assertThat(getRSubstitution(result, "line2").isTagged(JavaType.WILDCARD)).isTrue();
// Check lambda with nested returns
assertThat(getRSubstitution(result, "line3").is("java.lang.Integer")).isTrue();
// Check one liner lambdas
assertThat(getRSubstitution(result, "line").is("java.lang.String[]")).isTrue();
MethodInvocationTree mapMethod = (MethodInvocationTree) result.symbol("line").declaration().parent().parent().parent();
Type mapType = mapMethod.symbolType();
assertThat(mapType.is("java.util.stream.Stream")).as("Found " + mapType + " instead of Stream").isTrue();
assertThat(((JavaType) mapType).isParameterized()).isTrue();
assertThat(((ParametrizedTypeJavaType) mapType).typeSubstitution.substitutedTypes()).hasSize(1);
assertThat(((ParametrizedTypeJavaType) mapType).typeSubstitution.substitutedTypes().get(0).is("java.lang.String[]")).isTrue();
JavaSymbol sx = result.symbol("sx");
assertThat(sx.type.is("java.lang.String")).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method getClass_return_type.
@Test
public void getClass_return_type() {
Result result = Result.createFor("GetClassReturnType");
JavaSymbol maybeAddListener = result.symbol("maybeAddListener");
assertThat(maybeAddListener.isMethodSymbol()).isTrue();
List<IdentifierTree> usages = maybeAddListener.usages();
assertThat(usages).hasSize(1);
Type getClassType = ((MethodInvocationTree) usages.get(0).parent()).arguments().get(1).symbolType();
assertThat(getClassType).isInstanceOf(ParametrizedTypeJavaType.class);
Type param = ((ParametrizedTypeJavaType) getClassType).typeSubstitution.substitutedTypes().get(0);
assertThat(param).isInstanceOf(WildCardType.class);
assertThat(((WildCardType) param).boundType).isEqualTo(WildCardType.BoundType.EXTENDS);
assertThat(((WildCardType) param).bound.is("ISuiteListener")).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SymbolTableTest method conditional_expression_in_lambda.
@Test
public void conditional_expression_in_lambda() {
Result result = Result.createFor("ConditionalExpressionInLambda");
JavaSymbol foo = result.symbol("foo");
assertThat(foo.usages()).hasSize(1);
IdentifierTree map = result.referenceTree(8, 8);
JavaType mapResultType = ((MethodJavaType) map.symbolType()).resultType;
assertThat(mapResultType.isTagged(JavaType.DEFERRED)).isFalse();
assertThat(mapResultType.is("java.util.stream.Stream")).isTrue();
assertThat(mapResultType.isParameterized()).isTrue();
JavaType substitutionType = ((ParametrizedTypeJavaType) mapResultType).typeSubstitution.substitutedTypes().get(0);
assertThat(substitutionType.is("java.lang.Comparable")).isTrue();
JavaType lambdaType = (JavaType) ((MethodInvocationTree) map.parent().parent()).arguments().get(0).symbolType();
assertThat(lambdaType.isParameterized()).isTrue();
assertThat(lambdaType.is("java.util.function.Function")).isTrue();
// only interested in return type: LUB of Integer and String
JavaType returnType = ((ParametrizedTypeJavaType) lambdaType).typeSubstitution.substitutedTypes().get(1);
assertThat(returnType.isSubtypeOf("java.lang.Comparable")).isTrue();
JavaSymbol bar = result.symbol("bar");
assertThat(bar.usages()).hasSize(1);
IdentifierTree flatMap = result.referenceTree(13, 8);
JavaType flatMapResultType = ((MethodJavaType) flatMap.symbolType()).resultType;
assertThat(flatMapResultType.isTagged(JavaType.DEFERRED)).isFalse();
assertThat(flatMapResultType.is("java.util.stream.Stream")).isTrue();
assertThat(flatMapResultType.isParameterized()).isTrue();
JavaType flatMapSubstitutionType = ((ParametrizedTypeJavaType) flatMapResultType).typeSubstitution.substitutedTypes().get(0);
assertThat(flatMapSubstitutionType.is("java.lang.Integer")).isTrue();
lambdaType = (JavaType) ((MethodInvocationTree) flatMap.parent().parent()).arguments().get(0).symbolType();
assertThat(lambdaType.isParameterized()).isTrue();
assertThat(lambdaType.is("java.util.function.Function")).isTrue();
// only interested in return type: LUB of deferred type Stream.empty() and Stream<Integer>
returnType = ((ParametrizedTypeJavaType) lambdaType).typeSubstitution.substitutedTypes().get(1);
assertThat(returnType.isParameterized()).isTrue();
assertThat(returnType.is("java.util.stream.Stream")).isTrue();
JavaType substitution = ((ParametrizedTypeJavaType) returnType).typeSubstitution.substitutedTypes().get(0);
assertThat(substitution.is("java.lang.Integer")).isTrue();
}
Aggregations