Search in sources :

Example 16 with Type

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

Example 17 with Type

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

Example 18 with Type

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

Example 19 with Type

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

Example 20 with Type

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