Search in sources :

Example 1 with WildcardType

use of javax.lang.model.type.WildcardType in project buck by facebook.

the class StandaloneWildcardTypeTest method testSuperToString.

@Test
public void testSuperToString() throws IOException {
    initCompiler();
    WildcardType wildcard = types.getWildcardType(null, elements.getTypeElement("java.lang.String").asType());
    assertEquals("? super java.lang.String", wildcard.toString());
}
Also used : WildcardType(javax.lang.model.type.WildcardType) Test(org.junit.Test)

Example 2 with WildcardType

use of javax.lang.model.type.WildcardType in project buck by facebook.

the class StandaloneWildcardTypeTest method testUnboundedToString.

@Test
public void testUnboundedToString() throws IOException {
    initCompiler();
    WildcardType wildcard = types.getWildcardType(null, null);
    assertEquals("?", wildcard.toString());
}
Also used : WildcardType(javax.lang.model.type.WildcardType) Test(org.junit.Test)

Example 3 with WildcardType

use of javax.lang.model.type.WildcardType in project buck by facebook.

the class StandaloneWildcardTypeTest method testExtendsToString.

@Test
public void testExtendsToString() throws IOException {
    initCompiler();
    WildcardType wildcard = types.getWildcardType(elements.getTypeElement("java.lang.CharSequence").asType(), null);
    assertEquals("? extends java.lang.CharSequence", wildcard.toString());
}
Also used : WildcardType(javax.lang.model.type.WildcardType) Test(org.junit.Test)

Example 4 with WildcardType

use of javax.lang.model.type.WildcardType in project buck by facebook.

the class TreeBackedTypesTest method testIsSameTypeWildcardType.

@Test
public void testIsSameTypeWildcardType() throws IOException {
    compile("class Foo { }");
    WildcardType wildcardType = types.getWildcardType(null, null);
    WildcardType wildcardType2 = types.getWildcardType(null, null);
    // According to the docs, wildcard types are never equal to one another, even if they're
    // the exact same instance.
    assertNotSameType(wildcardType, wildcardType);
    assertNotSameType(wildcardType, wildcardType2);
}
Also used : WildcardType(javax.lang.model.type.WildcardType) Test(org.junit.Test)

Example 5 with WildcardType

use of javax.lang.model.type.WildcardType in project bazel by bazelbuild.

the class InternalUtils method leastUpperBound.

/**
     * Returns the least upper bound of two {@link TypeMirror}s.
     *
     * @param processingEnv The {@link ProcessingEnvironment} to use.
     * @param tm1 A {@link TypeMirror}.
     * @param tm2 A {@link TypeMirror}.
     * @return The least upper bound of {@code tm1} and {@code tm2}.
     */
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
    Type t1 = (Type) tm1;
    Type t2 = (Type) tm2;
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
    Types types = Types.instance(javacEnv.getContext());
    if (types.isSameType(t1, t2)) {
        // Special case if the two types are equal.
        return t1;
    }
    // Handle the 'null' type manually (not done by types.lub).
    if (t1.getKind() == TypeKind.NULL) {
        return t2;
    }
    if (t2.getKind() == TypeKind.NULL) {
        return t1;
    }
    // Special case for primitives.
    if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
        if (types.isAssignable(t1, t2)) {
            return t2;
        } else if (types.isAssignable(t2, t1)) {
            return t1;
        } else {
            return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
        }
    }
    if (t1.getKind() == TypeKind.WILDCARD) {
        WildcardType wc1 = (WildcardType) t1;
        Type bound = (Type) wc1.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t1 = bound;
    }
    if (t2.getKind() == TypeKind.WILDCARD) {
        WildcardType wc2 = (WildcardType) t2;
        Type bound = (Type) wc2.getExtendsBound();
        if (bound == null) {
            // Implicit upper bound of java.lang.Object
            Elements elements = processingEnv.getElementUtils();
            return elements.getTypeElement("java.lang.Object").asType();
        }
        t2 = bound;
    }
    return types.lub(t1, t2);
}
Also used : Types(com.sun.tools.javac.code.Types) JCAnnotatedType(com.sun.tools.javac.tree.JCTree.JCAnnotatedType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Elements(javax.lang.model.util.Elements)

Aggregations

WildcardType (javax.lang.model.type.WildcardType)30 TypeMirror (javax.lang.model.type.TypeMirror)14 DeclaredType (javax.lang.model.type.DeclaredType)10 Test (org.junit.Test)9 TypeVariable (javax.lang.model.type.TypeVariable)8 ArrayType (javax.lang.model.type.ArrayType)7 Elements (javax.lang.model.util.Elements)7 Types (javax.lang.model.util.Types)5 Type (com.sun.tools.javac.code.Type)4 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 TypeElement (javax.lang.model.element.TypeElement)3 PrimitiveType (javax.lang.model.type.PrimitiveType)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Tree (com.sun.source.tree.Tree)2 Types (com.sun.tools.javac.code.Types)2 JCTree (com.sun.tools.javac.tree.JCTree)2 JCAnnotatedType (com.sun.tools.javac.tree.JCTree.JCAnnotatedType)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 IntersectionType (javax.lang.model.type.IntersectionType)2