Search in sources :

Example 11 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 12 with DeclaredType

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

the class TypeHierarchyCollector method collectUnresolvedInterface.

private void collectUnresolvedInterface(TypeMirror typeMirror, TypevarContext context) {
    if (typeMirror.getKind() == TypeKind.ERROR) {
        DeclaredType declaredType = toDeclaredType(typeMirror);
        String stringified = stringify(declaredType, context);
        implementedInterfaceNames.add(stringified);
    }
}
Also used : DeclaredType(javax.lang.model.type.DeclaredType)

Example 13 with DeclaredType

use of javax.lang.model.type.DeclaredType in project hibernate-orm by hibernate.

the class XmlMetaEntity method getCollectionTypes.

private String[] getCollectionTypes(String propertyName, String explicitTargetEntity, String explicitMapKeyClass, ElementKind expectedElementKind) {
    for (Element elem : element.getEnclosedElements()) {
        if (!expectedElementKind.equals(elem.getKind())) {
            continue;
        }
        String elementPropertyName = elem.getSimpleName().toString();
        if (elem.getKind().equals(ElementKind.METHOD)) {
            elementPropertyName = StringUtil.getPropertyName(elementPropertyName);
        }
        if (!propertyName.equals(elementPropertyName)) {
            continue;
        }
        DeclaredType type = determineDeclaredType(elem);
        if (type == null) {
            continue;
        }
        return determineTypes(propertyName, explicitTargetEntity, explicitMapKeyClass, type);
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 14 with DeclaredType

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

the class AbstractQuerydslProcessor method handleEmbeddedType.

private void handleEmbeddedType(Element element, Set<TypeElement> elements) {
    TypeMirror type = element.asType();
    if (element.getKind() == ElementKind.METHOD) {
        type = ((ExecutableElement) element).getReturnType();
    }
    String typeName = type.toString();
    if (typeName.startsWith(Collection.class.getName()) || typeName.startsWith(List.class.getName()) || typeName.startsWith(Set.class.getName())) {
        type = ((DeclaredType) type).getTypeArguments().get(0);
    } else if (typeName.startsWith(Map.class.getName())) {
        type = ((DeclaredType) type).getTypeArguments().get(1);
    }
    TypeElement typeElement = typeExtractor.visit(type);
    if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
        if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
            elements.add(typeElement);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 15 with DeclaredType

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

the class TypeUtils method getAnnotationValuesAsElements.

@SuppressWarnings("unchecked")
public static Set<TypeElement> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) {
    Set<TypeElement> elements = new HashSet<TypeElement>();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
        if (entry.getKey().getSimpleName().toString().equals(method)) {
            List<AnnotationValue> values = ((List) entry.getValue().getValue());
            for (AnnotationValue value : values) {
                DeclaredType type = (DeclaredType) value.getValue();
                elements.add((TypeElement) type.asElement());
            }
        }
    }
    return elements;
}
Also used : Map(java.util.Map) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)136 TypeElement (javax.lang.model.element.TypeElement)81 TypeMirror (javax.lang.model.type.TypeMirror)74 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 ArrayList (java.util.ArrayList)14 ArrayType (javax.lang.model.type.ArrayType)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 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5 TypeParameterElement (javax.lang.model.element.TypeParameterElement)5