use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBound method leastUpperBound.
/**
* Compute the "Least Upper Bound" ("lub", jls8 §4.10.4) of a list of type. The "lub" is a shared supertype that is more specific than any
* other shared supertype (that is, no other shared supertype is a subtype of the least upper bound)
*
* Parameterized types are currently ignored, as the method is used only to handle Union Types Trees, themselves being used only
* in catch trees. Note that Exceptions (any subclass of Throwable) cannot be generic (jls8 §8.1.2, §11.1.1: "compile-time error if a generic
* class is a direct or indirect subclass of Throwable")
*
* @param types
* @return the least upper bound of the types
*/
public Type leastUpperBound(Set<Type> types) {
Type lub = cachedLeastUpperBound(types);
lubCache.clear();
return lub;
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block.
@Test
public void test_enqueueing_of_exit_block() {
MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock()Z");
List<MethodYield> yields = mb.yields();
assertThat(yields).hasSize(1);
assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
ExceptionalYield exceptionalYield = mb.exceptionalPathYields().findFirst().get();
Type exceptionType = exceptionalYield.exceptionType(semanticModel);
assertThat(exceptionType.is("java.io.FileNotFoundException")).isTrue();
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class GenericsTest method declaredTypesFromFile.
private static List<Type> declaredTypesFromFile(String path) {
CompilationUnitTree tree = treeOf(new File(path));
List<Type> results = Lists.newLinkedList();
for (Tree classTree : tree.types()) {
Type type = ((ClassTree) classTree).symbol().type();
results.add(type);
}
return results;
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class GenericsTest method test_method_resolution_based_on_wildcards.
@Test
public void test_method_resolution_based_on_wildcards() {
List<Type> elementTypes = declaredTypes("class Animal {}", "class Cat extends Animal {}", "class Lion extends Cat {}", "class A<T> {", " void foo(A<? extends Animal> a) {", // call to foo with wildcard
" foo(new A<Animal>());", " foo(new A<Cat>());", " foo(new A<Lion>());", // call to foo with object
" foo(new A<Object>());", " }", " void foo(Object o) {}", " void bar(A<? super Cat> a) {", // call to bar with wildcard
" bar(new A<Object>());", " bar(new A<Animal>());", " bar(new A<Cat>());", // call to bar with object
" bar(new A<Lion>());", " }", " void bar(Object o) {}", " void qix(A<?> a) {", " qix(new A<Object>());", " qix(new A<Animal>());", " qix(new A<Cat>());", " qix(new A<Lion>());", " }", " void gul(A<Cat> a) {", " gul(new A<Animal>());", " gul(new A<Cat>());", " gul(new A<Object>());", " }", " void gul(Object o) {}", "}");
JavaType aType = (JavaType) elementTypes.get(3);
JavaSymbol.MethodJavaSymbol fooWildCard = getMethodSymbol(aType, "foo", 0);
JavaSymbol.MethodJavaSymbol fooObject = getMethodSymbol(aType, "foo", 1);
assertThat(fooWildCard.usages()).hasSize(3);
assertThat(fooObject.usages()).hasSize(1);
JavaSymbol.MethodJavaSymbol barWildCard = getMethodSymbol(aType, "bar", 0);
JavaSymbol.MethodJavaSymbol barObject = getMethodSymbol(aType, "bar", 1);
assertThat(barWildCard.usages()).hasSize(3);
assertThat(barObject.usages()).hasSize(1);
JavaSymbol.MethodJavaSymbol qix = getMethodSymbol(aType, "qix");
assertThat(qix.usages()).hasSize(4);
JavaSymbol.MethodJavaSymbol gulGenerics = getMethodSymbol(aType, "gul", 0);
JavaSymbol.MethodJavaSymbol gulObject = getMethodSymbol(aType, "gul", 1);
assertThat(gulGenerics.usages()).hasSize(1);
assertThat(gulObject.usages()).hasSize(2);
}
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_provided_substitution.
@Test
public void test_method_resolution_for_parametrized_method_with_provided_substitution() {
JavaType type = (JavaType) declaredTypesFromFile("src/test/files/resolve/ParametrizedMethodsWithProvidedSubstitution.java").get(0);
methodHasUsagesWithSameTypeAs(type, "f1", 0, "bString", "bb");
methodHasUsagesWithSameTypeAs(type, "f1", 1, "aType");
methodHasUsagesWithSameTypeAs(type, "f2", 0, "integer", "string", "aType");
methodHasUsagesWithSameTypeAs(type, "f2", 1, "aType");
methodHasUsagesWithSameTypeAs(type, "f3", "integer");
methodHasUsagesWithSameTypeAs(type, "f4", (String) null);
Type stringArray = getMethodInvocationType(getMethodSymbol(type, "f4", 0), 0);
assertThat(stringArray.isArray()).isTrue();
assertThat(((ArrayJavaType) stringArray).elementType.is("java.lang.String")).isTrue();
stringArray = getMethodInvocationType(getMethodSymbol(type, "f4", 1), 0);
assertThat(stringArray.isArray()).isTrue();
assertThat(((ArrayJavaType) stringArray).elementType.isArray()).isTrue();
assertThat(((ArrayJavaType) ((ArrayJavaType) stringArray).elementType).elementType.is("java.lang.String")).isTrue();
methodHasUsagesWithSameTypeAs(type, "f5", "cStringInteger", "cStringInteger", "cAB");
methodHasUsagesWithSameTypeAs(type, "f6", "wcSuperA");
methodHasUsagesWithSameTypeAs(type, "f7", "integer");
methodHasUsagesWithSameTypeAs(type, "f8", 0, "object");
methodHasUsagesWithSameTypeAs(type, "f8", 1, "bType", "dType");
methodHasUsagesWithSameTypeAs(type, "f9", 0, "object", "object");
methodHasUsagesWithSameTypeAs(type, "f9", 1, "dType");
methodHasUsagesWithSameTypeAs(type, "f10", "integer");
}
Aggregations