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