Search in sources :

Example 56 with DeclaredType

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

the class TreeBackedTypeElementTest method testGetSuperclassNoSuperclassIsObject.

@Test
public void testGetSuperclassNoSuperclassIsObject() throws IOException {
    compile("class Foo { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    DeclaredType superclass = (DeclaredType) fooElement.getSuperclass();
    TypeElement objectElement = elements.getTypeElement("java.lang.Object");
    assertSame(objectElement, superclass.asElement());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 57 with DeclaredType

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

the class TypeResolverTest method testParameterizedTypeResolves.

@Test
public void testParameterizedTypeResolves() throws IOException {
    compile(Joiner.on('\n').join("abstract class Foo extends java.util.ArrayList<java.lang.String> { }", "abstract class Bar extends java.util.ArrayList<java.lang.String> { }"));
    TypeElement listElement = elements.getTypeElement("java.util.ArrayList");
    TypeElement stringElement = elements.getTypeElement("java.lang.String");
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, stringElement.asType());
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement barElement = elements.getTypeElement("Bar");
    DeclaredType fooSuperclass = (DeclaredType) fooElement.getSuperclass();
    DeclaredType barSuperclass = (DeclaredType) barElement.getSuperclass();
    assertNotSame(expectedSuperclass, fooSuperclass);
    assertSameType(expectedSuperclass, fooSuperclass);
    assertSameType(fooSuperclass, barSuperclass);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 58 with DeclaredType

use of javax.lang.model.type.DeclaredType in project RoboBinding by RoboBinding.

the class WrappedTypeElement method firstTypeArgument.

public WrappedTypeElement firstTypeArgument() {
    List<? extends TypeMirror> typeArguments = type.getTypeArguments();
    DeclaredType firstTypeArgument = (DeclaredType) typeArguments.get(0);
    return typeElementOf(firstTypeArgument);
}
Also used : DeclaredType(javax.lang.model.type.DeclaredType) WrappedDeclaredType(org.robobinding.codegen.apt.type.WrappedDeclaredType)

Example 59 with DeclaredType

use of javax.lang.model.type.DeclaredType in project butterknife by JakeWharton.

the class ButterKnifeProcessor method isSubtypeOfType.

static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
    if (isTypeEqual(typeMirror, otherType)) {
        return true;
    }
    if (typeMirror.getKind() != TypeKind.DECLARED) {
        return false;
    }
    DeclaredType declaredType = (DeclaredType) typeMirror;
    List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
    if (typeArguments.size() > 0) {
        StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
        typeString.append('<');
        for (int i = 0; i < typeArguments.size(); i++) {
            if (i > 0) {
                typeString.append(',');
            }
            typeString.append('?');
        }
        typeString.append('>');
        if (typeString.toString().equals(otherType)) {
            return true;
        }
    }
    Element element = declaredType.asElement();
    if (!(element instanceof TypeElement)) {
        return false;
    }
    TypeElement typeElement = (TypeElement) element;
    TypeMirror superType = typeElement.getSuperclass();
    if (isSubtypeOfType(superType, otherType)) {
        return true;
    }
    for (TypeMirror interfaceType : typeElement.getInterfaces()) {
        if (isSubtypeOfType(interfaceType, otherType)) {
            return true;
        }
    }
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 60 with DeclaredType

use of javax.lang.model.type.DeclaredType in project ig-json-parser by Instagram.

the class TypeUtils method getCollectionParameterizedType.

/**
   * If {@code typeMirror} represents a list type ({@link java.util.List}), attempt to divine the
   * type of the contents.
   *
   * Returns null if {@code typeMirror} does not represent a list type or if we cannot divine the
   * type of the contents.
   */
public TypeMirror getCollectionParameterizedType(TypeMirror typeMirror) {
    if (!(typeMirror instanceof DeclaredType)) {
        return null;
    }
    DeclaredType declaredType = (DeclaredType) typeMirror;
    Element element = declaredType.asElement();
    if (!(element instanceof TypeElement)) {
        return null;
    }
    TypeElement typeElement = (TypeElement) element;
    List<? extends TypeParameterElement> typeParameterElements = typeElement.getTypeParameters();
    List<TypeMirror> typeArguments = (List<TypeMirror>) declaredType.getTypeArguments();
    if (JAVA_UTIL_QUEUE.equals(getCanonicalTypeName(declaredType)) || JAVA_UTIL_LIST.equals(getCanonicalTypeName(declaredType)) || JAVA_UTIL_ARRAYLIST.equals(getCanonicalTypeName(declaredType)) || JAVA_UTIL_SET.equals(getCanonicalTypeName(declaredType))) {
        // sanity check.
        if (typeParameterElements.size() != 1) {
            throw new IllegalStateException(String.format("%s is not expected generic type", declaredType));
        }
        return typeArguments.get(0);
    } else if (JAVA_UTIL_HASHMAP.equals(getCanonicalTypeName(declaredType))) {
        // sanity check.
        if (typeParameterElements.size() != 2) {
            throw new IllegalStateException(String.format("%s is not expected generic type", declaredType));
        }
        TypeMirror keyType = typeArguments.get(0);
        TypeMirror valueType = typeArguments.get(1);
        if (!JAVA_LANG_STRING.equals(keyType.toString())) {
            throw new IllegalStateException("Only String keys are supported for map types");
        }
        return valueType;
    }
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) List(java.util.List) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)138 TypeElement (javax.lang.model.element.TypeElement)82 TypeMirror (javax.lang.model.type.TypeMirror)75 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayType (javax.lang.model.type.ArrayType)15 ArrayList (java.util.ArrayList)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 TypeParameterElement (javax.lang.model.element.TypeParameterElement)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5