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);
}
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;
}
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());
}
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());
}
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());
}
Aggregations