Search in sources :

Example 16 with DeclaredType

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

the class JPAConfiguration method getRealElementType.

private TypeMirror getRealElementType(Element element) {
    AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToOne.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToOne.class);
    }
    if (mirror != null) {
        return TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
    }
    mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToMany.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToMany.class);
    }
    if (mirror != null) {
        TypeMirror typeArg = TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
        TypeMirror erasure = types.erasure(element.asType());
        TypeElement typeElement = (TypeElement) types.asElement(erasure);
        if (typeElement != null && typeArg != null) {
            if (typeElement.getTypeParameters().size() == 1) {
                return types.getDeclaredType(typeElement, typeArg);
            } else if (typeElement.getTypeParameters().size() == 2) {
                if (element.asType() instanceof DeclaredType) {
                    TypeMirror first = ((DeclaredType) element.asType()).getTypeArguments().get(0);
                    return types.getDeclaredType(typeElement, first, typeArg);
                }
            }
        }
    }
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 17 with DeclaredType

use of javax.lang.model.type.DeclaredType in project realm-java by realm.

the class Utils method getGenericTypeQualifiedName.

/**
     * @return the generic type for Lists of the form {@code List<type>}
     */
public static String getGenericTypeQualifiedName(VariableElement field) {
    TypeMirror fieldType = field.asType();
    List<? extends TypeMirror> typeArguments = ((DeclaredType) fieldType).getTypeArguments();
    if (typeArguments.size() == 0) {
        return null;
    }
    return typeArguments.get(0).toString();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 18 with DeclaredType

use of javax.lang.model.type.DeclaredType in project realm-java by realm.

the class ClassMetaData method checkListTypes.

private boolean checkListTypes() {
    for (VariableElement field : fields) {
        if (Utils.isRealmList(field) || Utils.isRealmResults(field)) {
            // Check for missing generic (default back to Object)
            if (Utils.getGenericTypeQualifiedName(field) == null) {
                Utils.error("No generic type supplied for field", field);
                return false;
            }
            // Check that the referenced type is a concrete class and not an interface
            TypeMirror fieldType = field.asType();
            List<? extends TypeMirror> typeArguments = ((DeclaredType) fieldType).getTypeArguments();
            String genericCanonicalType = typeArguments.get(0).toString();
            TypeElement typeElement = elements.getTypeElement(genericCanonicalType);
            if (typeElement.getSuperclass().getKind() == TypeKind.NONE) {
                Utils.error("Only concrete Realm classes are allowed in RealmLists. " + "Neither interfaces nor abstract classes are allowed.", field);
                return false;
            }
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 19 with DeclaredType

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

the class GuiceAnnotationProcessor method addMethodOrConstructorToAnnotationDatabase.

private void addMethodOrConstructorToAnnotationDatabase(String annotationClassName, Element injectionPoint) {
    String injectionPointName = injectionPoint.getSimpleName().toString();
    for (VariableElement variable : ((ExecutableElement) injectionPoint).getParameters()) {
        String parameterTypeName = getTypeName((TypeElement) ((DeclaredType) variable.asType()).asElement());
        bindableClasses.add(parameterTypeName);
        injectionPointName += ":" + parameterTypeName;
    }
    TypeElement typeElementRequiringScanning = (TypeElement) injectionPoint.getEnclosingElement();
    String typeElementName = getTypeName(typeElementRequiringScanning);
    //System.out.printf("Type: %s, injection: %s \n",typeElementName, injectionPointName);
    if (injectionPointName.startsWith("<init>")) {
        addToInjectedConstructors(annotationClassName, typeElementName, injectionPointName);
    } else {
        addToInjectedMethods(annotationClassName, typeElementName, injectionPointName);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) String(java.lang.String) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 20 with DeclaredType

use of javax.lang.model.type.DeclaredType in project spring-framework by spring-projects.

the class TypeHelper method getType.

public String getType(TypeMirror type) {
    if (type == null) {
        return null;
    }
    if (type instanceof DeclaredType) {
        DeclaredType declaredType = (DeclaredType) type;
        Element enclosingElement = declaredType.asElement().getEnclosingElement();
        if (enclosingElement != null && enclosingElement instanceof TypeElement) {
            return getQualifiedName(enclosingElement) + "$" + declaredType.asElement().getSimpleName().toString();
        } else {
            return getQualifiedName(declaredType.asElement());
        }
    }
    return type.toString();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) 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