Search in sources :

Example 41 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class GenericsTest method test_type_hierarchy_directly_between_wildcards_and_other_types.

@Test
public void test_type_hierarchy_directly_between_wildcards_and_other_types() {
    List<Type> elementTypes = declaredTypesUsingHierarchy("A<? extends Cat>", "A<? super Cat>", "A<?>", "Object", "Animal", "Cat", "Integer");
    Type wcExtendsCatType = ((ParametrizedTypeJavaType) elementTypes.get(0)).typeSubstitution.substitutedTypes().get(0);
    Type wcSuperCatType = ((ParametrizedTypeJavaType) elementTypes.get(1)).typeSubstitution.substitutedTypes().get(0);
    Type wcUnboundedType = ((ParametrizedTypeJavaType) elementTypes.get(2)).typeSubstitution.substitutedTypes().get(0);
    Type objectType = elementTypes.get(3);
    Type animalType = elementTypes.get(4);
    Type catType = elementTypes.get(5);
    Type integerType = elementTypes.get(6);
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf(objectType);
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf(catType);
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf(animalType);
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf("java.lang.Object");
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf("org.foo.Cat");
    SubtypeAssert.assertThat(wcExtendsCatType).isSubtypeOf("org.foo.Animal");
    SubtypeAssert.assertThat(wcSuperCatType).isSubtypeOf(objectType);
    SubtypeAssert.assertThat(wcSuperCatType).isNotSubtypeOf(catType);
    SubtypeAssert.assertThat(wcSuperCatType).isNotSubtypeOf(animalType);
    SubtypeAssert.assertThat(wcSuperCatType).isSubtypeOf("java.lang.Object");
    SubtypeAssert.assertThat(wcSuperCatType).isNotSubtypeOf("org.foo.Cat");
    SubtypeAssert.assertThat(wcSuperCatType).isNotSubtypeOf("org.foo.Animal");
    SubtypeAssert.assertThat(wcUnboundedType).isSubtypeOf(objectType);
    SubtypeAssert.assertThat(wcUnboundedType).isNotSubtypeOf(catType);
    SubtypeAssert.assertThat(wcUnboundedType).isNotSubtypeOf(animalType);
    SubtypeAssert.assertThat(wcUnboundedType).isSubtypeOf("java.lang.Object");
    SubtypeAssert.assertThat(wcUnboundedType).isNotSubtypeOf("org.foo.Cat");
    SubtypeAssert.assertThat(wcUnboundedType).isNotSubtypeOf("org.foo.Animal");
    SubtypeAssert.assertThat(wcExtendsCatType).isNotSubtypeOf(integerType);
    SubtypeAssert.assertThat(wcExtendsCatType).isNotSubtypeOf("java.lang.Integer");
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 42 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class GenericsTest method assertTypesAreNotTheSame.

private static void assertTypesAreNotTheSame(String type1, String type2) {
    List<Type> elementTypes = declaredTypesUsingHierarchy(type1, type2);
    Type t1 = elementTypes.get(0);
    Type t2 = elementTypes.get(1);
    SubtypeAssert.assertThat(t1).isNotSameAs(t2);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type)

Example 43 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class GenericsTest method test_method_resolution_for_parametrized_method_with_inference_from_call_site.

@Test
public void test_method_resolution_for_parametrized_method_with_inference_from_call_site() {
    List<Type> elementTypes = declaredTypes("class A<E> {" + "  static <T> A<T> foo() { return new A<T>(); }" + "  void tst() {" + "    A<String> a = A.foo();" + "  }" + "}");
    JavaType type = (JavaType) elementTypes.get(0);
    JavaSymbol.MethodJavaSymbol methodSymbol = getMethodSymbol(type, "foo");
    assertThat(methodSymbol.usages()).hasSize(1);
    Type methodInvocationType = getMethodInvocationType(methodSymbol, 0);
    assertThat(methodInvocationType.erasure().is("A")).isTrue();
    assertThat(methodInvocationType instanceof ParametrizedTypeJavaType).isTrue();
    assertThat(((ParametrizedTypeJavaType) methodInvocationType).typeSubstitution.substitutedTypes().get(0).is("java.lang.String")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Test(org.junit.Test)

Example 44 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class GenericsTest method methodHasUsagesWithSameTypeAs.

private static void methodHasUsagesWithSameTypeAs(JavaType type, String methodName, int methodIndex, String... variables) {
    JavaSymbol.MethodJavaSymbol method = getMethodSymbol(type, methodName, methodIndex);
    List<IdentifierTree> usages = method.usages();
    assertThat(usages).overridingErrorMessage("Method '" + methodName + "' should have " + variables.length + " reference(s) but has " + usages.size() + ".").hasSize(variables.length);
    for (int i = 0; i < variables.length; i++) {
        String variableName = variables[i];
        if (variableName != null) {
            Type methodInvocationType = getMethodInvocationType(method, i);
            Type variableType = getVariableType(type, variableName);
            assertThat(methodInvocationType).overridingErrorMessage("Type of expression should be the same as type of variable '" + variableName + "'.").isSameAs(variableType);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 45 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class GenericsTest method test_method_resolution_with_type_substitution.

@Test
public void test_method_resolution_with_type_substitution() {
    List<Type> elementTypes = declaredTypes("class A<X> {" + "  void foo(A<? extends X> a) {}" + "  A<String> qix() { return null; }" + "  void bar() {" + "    new A<String>().foo(qix());" + "  }" + "}");
    JavaSymbol.MethodJavaSymbol foo = getMethodSymbol((JavaType) elementTypes.get(0), "foo", 0);
    assertThat(foo.usages()).hasSize(1);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Test(org.junit.Test)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8