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");
}
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);
}
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();
}
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);
}
}
}
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);
}
Aggregations