Search in sources :

Example 1 with EntityType

use of com.querydsl.codegen.EntityType in project querydsl by querydsl.

the class HibernateDomainExporter method collectTypes.

@Override
protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, NoSuchMethodException {
    // super classes
    Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings();
    while (superClassMappings.hasNext()) {
        MappedSuperclass msc = (MappedSuperclass) superClassMappings.next();
        EntityType entityType = createSuperType(msc.getMappedClass());
        if (msc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty());
        }
        Iterator<?> properties = msc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }
    // entity classes
    Iterator<?> classMappings = configuration.getClassMappings();
    while (classMappings.hasNext()) {
        PersistentClass pc = (PersistentClass) classMappings.next();
        EntityType entityType = createEntityType(pc.getMappedClass());
        if (pc.getDeclaredIdentifierProperty() != null) {
            handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty());
        } else if (!pc.isInherited() && pc.hasIdentifierProperty()) {
            logger.info(entityType.toString() + pc.getIdentifierProperty());
            handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty());
        } else if (pc.getIdentifier() != null) {
            KeyValue identifier = pc.getIdentifier();
            if (identifier instanceof Component) {
                Component component = (Component) identifier;
                Iterator<?> properties = component.getPropertyIterator();
                if (component.isEmbedded()) {
                    while (properties.hasNext()) {
                        handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
                    }
                } else {
                    String name = component.getNodeName();
                    Class<?> clazz = component.getType().getReturnedClass();
                    Type propertyType = getType(pc.getMappedClass(), clazz, name);
                    AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name);
                    Property property = createProperty(entityType, name, propertyType, annotated);
                    entityType.addProperty(property);
                    // handle component properties
                    EntityType embeddedType = createEmbeddableType(propertyType);
                    while (properties.hasNext()) {
                        handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next());
                    }
                }
            }
        // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne
        }
        Iterator<?> properties = pc.getDeclaredPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
        }
    }
}
Also used : org.hibernate.mapping(org.hibernate.mapping) AnnotatedElement(java.lang.reflect.AnnotatedElement) EntityType(com.querydsl.codegen.EntityType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Property(com.querydsl.codegen.Property)

Example 2 with EntityType

use of com.querydsl.codegen.EntityType in project querydsl by querydsl.

the class ExtendedTypeFactory method createEntityType.

@Nullable
private EntityType createEntityType(TypeMirror typeMirror, List<String> key, boolean deep) {
    entityTypeCache.put(key, null);
    Type value = visitor.visit(typeMirror, deep);
    if (value != null) {
        EntityType entityType = null;
        if (value instanceof EntityType) {
            entityType = (EntityType) value;
        } else {
            entityType = new EntityType(value, variableNameFunction);
            typeMappings.register(entityType, queryTypeFactory.create(entityType));
        }
        entityTypeCache.put(key, entityType);
        if (deep) {
            for (Type superType : getSupertypes(typeMirror, deep)) {
                entityType.addSupertype(new Supertype(superType));
            }
        }
        return entityType;
    } else {
        return null;
    }
}
Also used : EntityType(com.querydsl.codegen.EntityType) ErrorType(javax.lang.model.type.ErrorType) EntityType(com.querydsl.codegen.EntityType) DeclaredType(javax.lang.model.type.DeclaredType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) Type(com.querydsl.codegen.utils.model.Type) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) PrimitiveType(javax.lang.model.type.PrimitiveType) Supertype(com.querydsl.codegen.Supertype) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with EntityType

use of com.querydsl.codegen.EntityType in project querydsl by querydsl.

the class ExtendedTypeFactory method createType.

@Nullable
private Type createType(TypeMirror typeMirror, List<String> key, boolean deep) {
    typeCache.put(key, null);
    Type type = visitor.visit(typeMirror, deep);
    if (type != null && (type.getCategory() == TypeCategory.ENTITY || type.getCategory() == TypeCategory.CUSTOM)) {
        EntityType entityType = getEntityType(typeMirror, deep);
        typeCache.put(key, entityType);
        return entityType;
    } else {
        typeCache.put(key, type);
        return type;
    }
}
Also used : EntityType(com.querydsl.codegen.EntityType) ErrorType(javax.lang.model.type.ErrorType) EntityType(com.querydsl.codegen.EntityType) DeclaredType(javax.lang.model.type.DeclaredType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) Type(com.querydsl.codegen.utils.model.Type) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) PrimitiveType(javax.lang.model.type.PrimitiveType) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with EntityType

use of com.querydsl.codegen.EntityType 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) EntityType(com.querydsl.codegen.EntityType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType)

Example 5 with EntityType

use of com.querydsl.codegen.EntityType 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 : HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) Property(com.querydsl.codegen.Property) HashSet(java.util.HashSet)

Aggregations

EntityType (com.querydsl.codegen.EntityType)18 Type (com.querydsl.codegen.utils.model.Type)11 SimpleType (com.querydsl.codegen.utils.model.SimpleType)9 Property (com.querydsl.codegen.Property)6 ArrayType (javax.lang.model.type.ArrayType)4 DeclaredType (javax.lang.model.type.DeclaredType)4 ErrorType (javax.lang.model.type.ErrorType)4 ExecutableType (javax.lang.model.type.ExecutableType)4 NoType (javax.lang.model.type.NoType)4 NullType (javax.lang.model.type.NullType)4 PrimitiveType (javax.lang.model.type.PrimitiveType)4 WildcardType (javax.lang.model.type.WildcardType)4 Before (org.junit.Before)4 ClassType (com.querydsl.codegen.utils.model.ClassType)3 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 SimpleType (com.mysema.codegen.model.SimpleType)2 Type (com.mysema.codegen.model.Type)2 PropertyType (com.querydsl.core.annotations.PropertyType)2 QueryType (com.querydsl.core.annotations.QueryType)2