Search in sources :

Example 41 with TypeMirror

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

the class AbstractQuerydslProcessor method collectElements.

protected Set<TypeElement> collectElements() {
    Set<TypeElement> elements = new HashSet<TypeElement>();
    // from delegate methods
    elements.addAll(processDelegateMethods());
    // from class annotations
    for (Class<? extends Annotation> annotation : conf.getEntityAnnotations()) {
        for (Element element : getElements(annotation)) {
            if (element instanceof TypeElement) {
                elements.add((TypeElement) element);
            }
        }
    }
    // from package annotations
    if (conf.getEntitiesAnnotation() != null) {
        for (Element element : getElements(conf.getEntitiesAnnotation())) {
            AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, conf.getEntitiesAnnotation());
            elements.addAll(TypeUtils.getAnnotationValuesAsElements(mirror, "value"));
        }
    }
    // from embedded annotations
    if (conf.getEmbeddedAnnotation() != null) {
        elements.addAll(getEmbeddedTypes());
    }
    // from embedded
    if (conf.isUnknownAsEmbedded()) {
        elements.addAll(getTypeFromProperties(elements));
    }
    // from annotation less supertypes
    if (!conf.isStrictMode()) {
        elements.addAll(getAnnotationlessSupertypes(elements));
    }
    // register possible embedded types of non-tracked supertypes
    if (conf.getEmbeddedAnnotation() != null) {
        Class<? extends Annotation> embedded = conf.getEmbeddedAnnotation();
        Set<TypeElement> embeddedElements = new HashSet<TypeElement>();
        for (TypeElement element : elements) {
            TypeMirror superTypeMirror = element.getSuperclass();
            while (superTypeMirror != null) {
                TypeElement superTypeElement = (TypeElement) processingEnv.getTypeUtils().asElement(superTypeMirror);
                if (superTypeElement != null) {
                    List<? extends Element> enclosed = superTypeElement.getEnclosedElements();
                    for (Element child : enclosed) {
                        if (child.getAnnotation(embedded) != null) {
                            handleEmbeddedType(child, embeddedElements);
                        }
                    }
                    superTypeMirror = superTypeElement.getSuperclass();
                    if (superTypeMirror instanceof NoType) {
                        superTypeMirror = null;
                    }
                } else {
                    superTypeMirror = null;
                }
            }
        }
        // register found elements
        for (TypeElement element : embeddedElements) {
            if (!elements.contains(element)) {
                elementHandler.handleEntityType(element);
            }
        }
    }
    return elements;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) NoType(javax.lang.model.type.NoType)

Example 42 with TypeMirror

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

the class AbstractQuerydslProcessor method processDelegateMethods.

private Set<TypeElement> processDelegateMethods() {
    Set<? extends Element> delegateMethods = getElements(QueryDelegate.class);
    Set<TypeElement> typeElements = new HashSet<TypeElement>();
    for (Element delegateMethod : delegateMethods) {
        ExecutableElement method = (ExecutableElement) delegateMethod;
        Element element = delegateMethod.getEnclosingElement();
        String name = method.getSimpleName().toString();
        Type delegateType = typeFactory.getType(element.asType(), true);
        Type returnType = typeFactory.getType(method.getReturnType(), true);
        List<Parameter> parameters = elementHandler.transformParams(method.getParameters());
        // remove first element
        parameters = parameters.subList(1, parameters.size());
        EntityType entityType = null;
        for (AnnotationMirror annotation : delegateMethod.getAnnotationMirrors()) {
            if (TypeUtils.isAnnotationMirrorOfType(annotation, QueryDelegate.class)) {
                TypeMirror type = TypeUtils.getAnnotationValueAsTypeMirror(annotation, "value");
                if (type != null) {
                    entityType = typeFactory.getEntityType(type, true);
                }
            }
        }
        if (entityType != null) {
            registerTypeElement(entityType.getFullName(), (TypeElement) element);
            entityType.addDelegate(new Delegate(entityType, delegateType, name, parameters, returnType));
            TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(entityType.getFullName());
            boolean isAnnotated = false;
            for (Class<? extends Annotation> ann : conf.getEntityAnnotations()) {
                if (typeElement.getAnnotation(ann) != null) {
                    isAnnotated = true;
                }
            }
            if (isAnnotated) {
                // handle also properties of entity type
                typeElements.add(processingEnv.getElementUtils().getTypeElement(entityType.getFullName()));
            } else {
                // skip handling properties
                context.extensionTypes.put(entityType.getFullName(), entityType);
                context.allTypes.put(entityType.getFullName(), entityType);
            }
        }
    }
    return typeElements;
}
Also used : NoType(javax.lang.model.type.NoType) Type(com.mysema.codegen.model.Type) DeclaredType(javax.lang.model.type.DeclaredType) TypeMirror(javax.lang.model.type.TypeMirror) QueryDelegate(com.querydsl.core.annotations.QueryDelegate) Parameter(com.mysema.codegen.model.Parameter)

Example 43 with TypeMirror

use of javax.lang.model.type.TypeMirror 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 44 with TypeMirror

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

the class TypeElementHandler method handleEntityType.

public EntityType handleEntityType(TypeElement element) {
    EntityType entityType = typeFactory.getEntityType(element.asType(), true);
    List<? extends Element> elements = element.getEnclosedElements();
    VisitorConfig config = configuration.getConfig(element, elements);
    Set<String> blockedProperties = new HashSet<String>();
    Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
    Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
    Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
    // constructors
    if (config.visitConstructors()) {
        handleConstructors(entityType, elements);
    }
    // fields
    if (config.visitFieldProperties()) {
        for (VariableElement field : ElementFilter.fieldsIn(elements)) {
            String name = field.getSimpleName().toString();
            if (configuration.isBlockedField(field)) {
                blockedProperties.add(name);
            } else if (configuration.isValidField(field)) {
                Annotations annotations = new Annotations();
                configuration.inspect(field, annotations);
                annotations.addAnnotation(field.getAnnotation(QueryType.class));
                annotations.addAnnotation(field.getAnnotation(QueryInit.class));
                propertyAnnotations.put(name, annotations);
                propertyTypes.put(name, field.asType());
                TypeMirror fixedType = configuration.getRealType(field);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // methods
    if (config.visitMethodProperties()) {
        for (ExecutableElement method : ElementFilter.methodsIn(elements)) {
            String name = method.getSimpleName().toString();
            if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(3));
            } else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(2));
            } else {
                continue;
            }
            if (configuration.isBlockedGetter(method)) {
                blockedProperties.add(name);
            } else if (configuration.isValidGetter(method) && !blockedProperties.contains(name)) {
                Annotations annotations = propertyAnnotations.get(name);
                if (annotations == null) {
                    annotations = new Annotations();
                    propertyAnnotations.put(name, annotations);
                }
                configuration.inspect(method, annotations);
                annotations.addAnnotation(method.getAnnotation(QueryType.class));
                annotations.addAnnotation(method.getAnnotation(QueryInit.class));
                propertyTypes.put(name, method.getReturnType());
                TypeMirror fixedType = configuration.getRealType(method);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // fixed types override property types
    propertyTypes.putAll(fixedTypes);
    for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
        Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
        if (property != null) {
            entityType.addProperty(property);
        }
    }
    return entityType;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) EntityType(com.querydsl.codegen.EntityType) Annotations(com.querydsl.core.util.Annotations) TypeMirror(javax.lang.model.type.TypeMirror) Property(com.querydsl.codegen.Property)

Example 45 with TypeMirror

use of javax.lang.model.type.TypeMirror 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)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)475 TypeElement (javax.lang.model.element.TypeElement)197 ExecutableElement (javax.lang.model.element.ExecutableElement)105 Test (org.junit.Test)90 VariableElement (javax.lang.model.element.VariableElement)88 DeclaredType (javax.lang.model.type.DeclaredType)76 Element (javax.lang.model.element.Element)61 ArrayList (java.util.ArrayList)36 Expression (com.google.devtools.j2objc.ast.Expression)32 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)28 Elements (javax.lang.model.util.Elements)28 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)24 AbstractJClass (com.helger.jcodemodel.AbstractJClass)24 List (java.util.List)23 Types (javax.lang.model.util.Types)22 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)21 TypeParameterElement (javax.lang.model.element.TypeParameterElement)21 ArrayType (javax.lang.model.type.ArrayType)21 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)20 IJExpression (com.helger.jcodemodel.IJExpression)20