Search in sources :

Example 1 with Type

use of com.mysema.codegen.model.Type in project querydsl by querydsl.

the class AbstractQuerydslProcessor method addExternalParents.

private void addExternalParents(EntityType entityType) {
    Deque<Type> superTypes = new ArrayDeque<Type>();
    if (entityType.getSuperType() != null) {
        superTypes.push(entityType.getSuperType().getType());
    }
    while (!superTypes.isEmpty()) {
        Type superType = superTypes.pop();
        if (!context.allTypes.containsKey(superType.getFullName())) {
            TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(superType.getFullName());
            if (typeElement == null) {
                throw new IllegalStateException("Found no type for " + superType.getFullName());
            }
            if (conf.isStrictMode() && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
                continue;
            }
            EntityType superEntityType = elementHandler.handleEntityType(typeElement);
            if (superEntityType.getSuperType() != null) {
                superTypes.push(superEntityType.getSuperType().getType());
            }
            context.allTypes.put(superType.getFullName(), superEntityType);
        }
    }
}
Also used : NoType(javax.lang.model.type.NoType) Type(com.mysema.codegen.model.Type) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with Type

use of com.mysema.codegen.model.Type in project querydsl by querydsl.

the class AbstractQuerydslProcessor method serialize.

private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
        try {
            Type type = conf.getTypeMappings().getPathType(model, model, true);
            String packageName = type.getPackageName();
            String className = !packageName.isEmpty() ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
            // skip if type is excluded class or in excluded package
            if (conf.isExcludedPackage(model.getPackageName()) || conf.isExcludedClass(model.getFullName())) {
                continue;
            }
            Set<TypeElement> elements = context.typeElements.get(model.getFullName());
            if (elements == null) {
                elements = new HashSet<TypeElement>();
            }
            for (Property property : model.getProperties()) {
                if (property.getType().getCategory() == TypeCategory.CUSTOM) {
                    Set<TypeElement> customElements = context.typeElements.get(property.getType().getFullName());
                    if (customElements != null) {
                        elements.addAll(customElements);
                    }
                }
            }
            processingEnv.getMessager().printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
            JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className, elements.toArray(new Element[elements.size()]));
            Writer writer = fileObject.openWriter();
            try {
                SerializerConfig serializerConfig = conf.getSerializerConfig(model);
                serializer.serialize(model, serializerConfig, new JavaWriter(writer));
            } finally {
                if (writer != null) {
                    writer.close();
                }
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
        }
    }
}
Also used : JavaWriter(com.mysema.codegen.JavaWriter) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) NoType(javax.lang.model.type.NoType) Type(com.mysema.codegen.model.Type) DeclaredType(javax.lang.model.type.DeclaredType) Writer(java.io.Writer) JavaWriter(com.mysema.codegen.JavaWriter)

Example 3 with Type

use of com.mysema.codegen.model.Type 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 4 with Type

use of com.mysema.codegen.model.Type in project querydsl by querydsl.

the class TypeElementHandler method getType.

private Type getType(VariableElement element) {
    Type rv = typeFactory.getType(element.asType(), true);
    if (element.getAnnotation(QueryType.class) != null) {
        QueryType qt = element.getAnnotation(QueryType.class);
        if (qt.value() != PropertyType.NONE) {
            TypeCategory typeCategory = TypeCategory.valueOf(qt.value().name());
            rv = rv.as(typeCategory);
        }
    }
    return rv;
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) Type(com.mysema.codegen.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) EntityType(com.querydsl.codegen.EntityType) TypeCategory(com.mysema.codegen.model.TypeCategory) QueryType(com.querydsl.core.annotations.QueryType)

Example 5 with Type

use of com.mysema.codegen.model.Type in project querydsl by querydsl.

the class TypeElementHandler method handleProjectionType.

public EntityType handleProjectionType(TypeElement e) {
    Type c = typeFactory.getType(e.asType(), true);
    EntityType entityType = new EntityType(c.as(TypeCategory.ENTITY), configuration.getVariableNameFunction());
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    List<? extends Element> elements = e.getEnclosedElements();
    handleConstructors(entityType, elements);
    return entityType;
}
Also used : EntityType(com.querydsl.codegen.EntityType) QueryType(com.querydsl.core.annotations.QueryType) Type(com.mysema.codegen.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) EntityType(com.querydsl.codegen.EntityType)

Aggregations

Type (com.mysema.codegen.model.Type)31 SimpleType (com.mysema.codegen.model.SimpleType)11 EntityType (com.querydsl.codegen.EntityType)8 Test (org.junit.Test)8 ClassType (com.mysema.codegen.model.ClassType)6 PropertyType (com.querydsl.core.annotations.PropertyType)5 QueryType (com.querydsl.core.annotations.QueryType)5 Parameter (com.mysema.codegen.model.Parameter)4 TypeCategory (com.mysema.codegen.model.TypeCategory)4 Property (com.querydsl.codegen.Property)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 DeclaredType (javax.lang.model.type.DeclaredType)3 NoType (javax.lang.model.type.NoType)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 MappingException (org.hibernate.MappingException)2 org.hibernate.mapping (org.hibernate.mapping)2 JavaWriter (com.mysema.codegen.JavaWriter)1 ScalaWriter (com.mysema.codegen.ScalaWriter)1 QueryDelegate (com.querydsl.core.annotations.QueryDelegate)1