use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_of_generics_without_loop2.
@Test
public void lub_of_generics_without_loop2() {
List<Type> typesFromInput = declaredTypes("class Parent<X> {}", "class Child<Y> extends Parent<Y> {}", "class Other<Z> {}", "class A {}", "class ChildP extends Parent<Other<? extends A>> {}", "class ChildC extends Child<Other<? extends A>> {}");
Type ChildP = typesFromInput.get(4);
Type childC = typesFromInput.get(5);
JavaType lub = leastUpperBound(ChildP, childC);
assertThat(lub.isTagged(JavaType.PARAMETERIZED)).isTrue();
ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) lub;
assertThat(ptt.rawType.is("Parent")).isTrue();
JavaType substitution = ptt.substitution(ptt.typeParameters().get(0));
assertThat(substitution.isTagged(JavaType.PARAMETERIZED)).isTrue();
ptt = (ParametrizedTypeJavaType) substitution;
assertThat(ptt.rawType.is("Other")).isTrue();
substitution = ptt.substitution(ptt.typeParameters().get(0));
assertThat(substitution.isTagged(JavaType.WILDCARD)).isTrue();
assertThat(((WildCardType) substitution).boundType).isEqualTo(WildCardType.BoundType.EXTENDS);
assertThat(((WildCardType) substitution).bound.is("A")).isTrue();
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_of_generics.
@Test
public void lub_of_generics() {
List<Type> typesFromInput = declaredTypes("class A extends Exception {}", "class B extends Exception implements I1<Exception> {}", "interface I1<T> {}");
Type a = typesFromInput.get(0);
Type b = typesFromInput.get(1);
Type lub = leastUpperBound(a, b);
assertThat(lub).isSameAs(a.symbol().superClass());
typesFromInput = declaredTypes("class A<T> extends java.util.List<T> {}", "class B extends A<String> {}");
a = typesFromInput.get(0);
b = typesFromInput.get(1);
lub = leastUpperBound(a, b);
assertThat(lub).isSameAs(a);
// FIXME : should be the other way around but we don't care about type parameter in lub for now.
assertThat(lub).isSameAs(b.symbol().superClass().erasure());
assertThat(lub).isNotSameAs(b.symbol().superClass());
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_with_unknown_inheritance.
@Test
public void lub_with_unknown_inheritance() {
List<Type> typesFromInput = declaredTypes("class A extends Exception {}", "class B extends UnknownException {}");
Type a = typesFromInput.get(0);
Type b = typesFromInput.get(1);
Type lub = leastUpperBound(a, b);
assertThat(lub.isUnknown()).isTrue();
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_with_hierarchy_of_supertypes1.
@Test
public void lub_with_hierarchy_of_supertypes1() {
List<Type> typesFromInput = declaredTypes("class A extends Exception {}", "class B extends A {}");
Type a = typesFromInput.get(0);
Type b = typesFromInput.get(1);
Type lub = leastUpperBound(a, b);
assertThat(lub).isSameAs(a);
lub = leastUpperBound(b, a);
assertThat(lub).isSameAs(a);
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBoundTest method lub_with_shared_supertypes1.
@Test
public void lub_with_shared_supertypes1() {
List<Type> typesFromInput = declaredTypes("class A extends Exception {}", "class B extends Exception {}");
Type a = typesFromInput.get(0);
Type b = typesFromInput.get(1);
Type lub = leastUpperBound(a, b);
assertThat(lub.is("java.lang.Exception")).isTrue();
}
Aggregations