Search in sources :

Example 11 with EntityType

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

the class HibernateDomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p) throws NoSuchMethodException, ClassNotFoundException {
    if (p.isBackRef()) {
        return;
    }
    Class<?> clazz = Object.class;
    try {
        clazz = p.getType().getReturnedClass();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    try {
        propertyType = getPropertyType(p, propertyType);
    } catch (MappingException e) {
    // ignore
    }
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isComposite()) {
        EntityType embeddedType = createEmbeddableType(propertyType);
        Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
        while (properties.hasNext()) {
            handleProperty(embeddedType, embeddedType.getJavaClass(), (org.hibernate.mapping.Property) properties.next());
        }
        propertyType = embeddedType;
    } else if (propertyType.getCategory() == TypeCategory.ENTITY || p.getValue() instanceof ManyToOne) {
        propertyType = createEntityType(propertyType);
    } else if (propertyType.getCategory() == TypeCategory.CUSTOM) {
        propertyType = createEmbeddableType(propertyType);
    } else if (p.getValue() instanceof org.hibernate.mapping.Collection) {
        org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) p.getValue();
        if (collection.getElement() instanceof OneToMany) {
            String entityName = ((OneToMany) collection.getElement()).getReferencedEntityName();
            if (entityName != null) {
                if (collection.isMap()) {
                    Type keyType = typeFactory.get(Class.forName(propertyType.getParameters().get(0).getFullName()));
                    Type valueType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
                } else {
                    Type componentType = typeFactory.get(Class.forName(entityName));
                    propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), componentType));
                }
            }
        } else if (collection.getElement() instanceof Component) {
            Component component = (Component) collection.getElement();
            Class<?> embeddedClass = Class.forName(component.getComponentClassName());
            EntityType embeddedType = createEmbeddableType(embeddedClass);
            Iterator<?> properties = component.getPropertyIterator();
            while (properties.hasNext()) {
                handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
            }
        }
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : org.hibernate.mapping(org.hibernate.mapping) AnnotatedElement(java.lang.reflect.AnnotatedElement) MappingException(org.hibernate.MappingException) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) Property(com.querydsl.codegen.Property)

Example 12 with EntityType

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

the class ExtendedTypeFactory method createEnumType.

private Type createEnumType(DeclaredType declaredType, TypeElement typeElement, boolean deep) {
    // fallback
    Type enumType = createType(typeElement, TypeCategory.ENUM, declaredType.getTypeArguments(), deep);
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (typeElement.getAnnotation(entityAnn) != null) {
            EntityType entityType = new EntityType(enumType, variableNameFunction);
            typeMappings.register(entityType, queryTypeFactory.create(entityType));
            return entityType;
        }
    }
    return enumType;
}
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)

Example 13 with EntityType

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

the class ExtendedTypeFactory method createClassType.

// TODO : simplify
private Type createClassType(DeclaredType declaredType, TypeElement typeElement, boolean deep) {
    // other
    String name = typeElement.getQualifiedName().toString();
    if (name.startsWith("java.")) {
        Iterator<? extends TypeMirror> i = declaredType.getTypeArguments().iterator();
        if (isAssignable(declaredType, mapType)) {
            return createMapType(i, deep);
        } else if (isAssignable(declaredType, listType)) {
            return createCollectionType(Types.LIST, i, deep);
        } else if (isAssignable(declaredType, setType)) {
            return createCollectionType(Types.SET, i, deep);
        } else if (isAssignable(declaredType, collectionType)) {
            return createCollectionType(Types.COLLECTION, i, deep);
        }
    }
    TypeCategory typeCategory = TypeCategory.get(name);
    if (typeCategory != TypeCategory.NUMERIC && isAssignable(typeElement.asType(), comparableType) && isSubType(typeElement.asType(), numberType)) {
        typeCategory = TypeCategory.NUMERIC;
    } else if (!typeCategory.isSubCategoryOf(TypeCategory.COMPARABLE) && isAssignable(typeElement.asType(), comparableType)) {
        typeCategory = TypeCategory.COMPARABLE;
    }
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (isSimpleTypeEntity(typeElement, entityAnn)) {
            typeCategory = TypeCategory.ENTITY;
        }
    }
    List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
    // for intersection types etc
    if (name.equals("")) {
        TypeMirror type = objectType;
        if (typeCategory == TypeCategory.COMPARABLE) {
            type = comparableType;
        }
        // find most specific type of superTypes which is a subtype of type
        List<? extends TypeMirror> superTypes = env.getTypeUtils().directSupertypes(declaredType);
        for (TypeMirror superType : superTypes) {
            if (env.getTypeUtils().isSubtype(superType, type)) {
                type = superType;
            }
        }
        typeElement = (TypeElement) env.getTypeUtils().asElement(type);
        if (type instanceof DeclaredType) {
            arguments = ((DeclaredType) type).getTypeArguments();
        }
    }
    Type type = createType(typeElement, typeCategory, arguments, deep);
    TypeMirror superType = typeElement.getSuperclass();
    TypeElement superTypeElement = null;
    if (superType instanceof DeclaredType) {
        superTypeElement = (TypeElement) ((DeclaredType) superType).asElement();
    }
    // entity type
    for (Class<? extends Annotation> entityAnn : entityAnnotations) {
        if (typeElement.getAnnotation(entityAnn) != null || (superTypeElement != null && superTypeElement.getAnnotation(entityAnn) != null)) {
            EntityType entityType = new EntityType(type, variableNameFunction);
            typeMappings.register(entityType, queryTypeFactory.create(entityType));
            return entityType;
        }
    }
    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) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) DeclaredType(javax.lang.model.type.DeclaredType)

Example 14 with EntityType

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

the class TypeElementHandler method toProperty.

private Property toProperty(EntityType entityType, String name, TypeMirror type, Annotations annotations) {
    // type
    Type propertyType = typeFactory.getType(type, true);
    if (annotations.isAnnotationPresent(QueryType.class)) {
        PropertyType propertyTypeAnn = annotations.getAnnotation(QueryType.class).value();
        if (propertyTypeAnn != PropertyType.NONE) {
            TypeCategory typeCategory = TypeCategory.valueOf(annotations.getAnnotation(QueryType.class).value().name());
            if (typeCategory == null) {
                return null;
            }
            propertyType = propertyType.as(typeCategory);
        } else {
            return null;
        }
    }
    // inits
    List<String> inits = Collections.emptyList();
    if (annotations.isAnnotationPresent(QueryInit.class)) {
        inits = Arrays.asList(annotations.getAnnotation(QueryInit.class).value());
    }
    return new Property(entityType, name, propertyType, inits);
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) EntityType(com.querydsl.codegen.EntityType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) QueryInit(com.querydsl.core.annotations.QueryInit) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) PropertyType(com.querydsl.core.annotations.PropertyType) QueryType(com.querydsl.core.annotations.QueryType) Property(com.querydsl.codegen.Property)

Example 15 with EntityType

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

the class DefaultNamingStrategyTest method setUp.

@Before
public void setUp() {
    entityModel = new EntityType(Types.OBJECT);
    // entityModel.addAnnotation(new TableImpl("OBJECT"));
    entityModel.getData().put("table", "OBJECT");
}
Also used : EntityType(com.querydsl.codegen.EntityType) Before(org.junit.Before)

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