Search in sources :

Example 66 with MethodInvocationTree

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;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 67 with MethodInvocationTree

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();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 68 with MethodInvocationTree

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();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Test(org.junit.Test)

Example 69 with MethodInvocationTree

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();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 70 with MethodInvocationTree

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();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7