Search in sources :

Example 51 with DeclaredType

use of javax.lang.model.type.DeclaredType in project epoxy by airbnb.

the class ProcessorUtils method getEpoxyObjectType.

/**
   * Returns the type of the Epoxy model.
   * <p>
   * Eg for "class MyModel extends EpoxyModel<TextView>" it would return TextView.
   */
static TypeMirror getEpoxyObjectType(TypeElement clazz, Types typeUtils) {
    if (clazz.asType().getKind() != TypeKind.DECLARED) {
        return null;
    }
    DeclaredType superclass = (DeclaredType) clazz.getSuperclass();
    List<? extends TypeMirror> superTypeArguments = superclass.getTypeArguments();
    if (superTypeArguments.isEmpty()) {
        return getEpoxyObjectType((TypeElement) typeUtils.asElement(superclass), typeUtils);
    }
    // should be rare, but it would be nice to handle.
    return superTypeArguments.get(0);
}
Also used : DeclaredType(javax.lang.model.type.DeclaredType)

Example 52 with DeclaredType

use of javax.lang.model.type.DeclaredType in project epoxy by airbnb.

the class ProcessorUtils method isSubtypeOfType.

static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
    if (otherType.equals(typeMirror.toString())) {
        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) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 53 with DeclaredType

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

the class StandaloneDeclaredTypeTest method testToStringWithGenerics.

@Test
public void testToStringWithGenerics() throws IOException {
    initCompiler();
    TypeElement mapElement = elements.getTypeElement("java.util.Map");
    TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
    TypeMirror integerType = elements.getTypeElement("java.lang.Integer").asType();
    DeclaredType mapStringIntType = types.getDeclaredType(mapElement, stringType, integerType);
    assertEquals("java.util.Map<java.lang.String,java.lang.Integer>", mapStringIntType.toString());
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 54 with DeclaredType

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

the class TypeResolverTest method testMultiDimArrayTypeResolves.

@Test
public void testMultiDimArrayTypeResolves() throws IOException {
    compile("abstract class Foo extends java.util.ArrayList<java.lang.String[][]> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement listElement = elements.getTypeElement("java.util.ArrayList");
    TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
    ArrayType stringArrayType = types.getArrayType(stringType);
    ArrayType stringArrayArrayType = types.getArrayType(stringArrayType);
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, stringArrayArrayType);
    assertSameType(expectedSuperclass, fooElement.getSuperclass());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 55 with DeclaredType

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

the class TreeBackedTypeElementTest method testGetSuperclassOtherSuperclass.

@Test
public void testGetSuperclassOtherSuperclass() throws IOException {
    compile(ImmutableMap.of("Foo.java", Joiner.on('\n').join("package com.facebook.foo;", "public class Foo extends com.facebook.bar.Bar { }"), "Bar.java", Joiner.on('\n').join("package com.facebook.bar;", "public class Bar { }")));
    TypeElement fooElement = elements.getTypeElement("com.facebook.foo.Foo");
    TypeElement barElement = elements.getTypeElement("com.facebook.bar.Bar");
    DeclaredType superclass = (DeclaredType) fooElement.getSuperclass();
    assertSame(barElement, superclass.asElement());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

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