Search in sources :

Example 36 with Type

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

Example 37 with Type

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

Example 38 with Type

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;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Type(org.sonar.plugins.java.api.semantic.Type) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) File(java.io.File)

Example 39 with Type

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);
}
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 40 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_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");
}
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