Search in sources :

Example 21 with Type

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

the class TypeResolver method resolveVar.

private static Type resolveVar(Type resolved, String varName, Type declaringType, EntityType context) {
    // get parameter index of var in declaring type
    int index = -1;
    for (int i = 0; i < declaringType.getParameters().size(); i++) {
        Type param = unwrap(declaringType.getParameters().get(i));
        if (Objects.equals(getVarName(param), varName)) {
            index = i;
        }
    }
    if (index == -1) {
        throw new IllegalStateException("Did not find type " + varName + " in " + declaringType + " for " + context);
    }
    Supertype type = context.getSuperType();
    while (!type.getEntityType().equals(declaringType)) {
        type = type.getEntityType().getSuperType();
    }
    if (!type.getType().getParameters().isEmpty()) {
        return type.getType().getParameters().get(index);
    } else {
        // raw type
        return resolved;
    }
}
Also used : Type(com.querydsl.codegen.utils.model.Type) SimpleType(com.querydsl.codegen.utils.model.SimpleType)

Example 22 with Type

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

the class AbstractDomainExporter method serialize.

private void serialize(Map<Class<?>, EntityType> types, Serializer serializer) throws IOException {
    for (EntityType entityType : types.values()) {
        if (serialized.add(entityType)) {
            Type type = typeMappings.getPathType(entityType, entityType, true);
            String packageName = type.getPackageName();
            String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
            write(serializer, className.replace('.', '/') + ".java", entityType);
        }
    }
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType)

Example 23 with Type

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

the class JPADomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, Attribute<?, ?> p) throws NoSuchMethodException, ClassNotFoundException {
    Class<?> clazz = Object.class;
    try {
        clazz = p.getJavaType();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isCollection()) {
        if (p instanceof MapAttribute) {
            MapAttribute<?, ?, ?> map = (MapAttribute<?, ?, ?>) p;
            Type keyType = typeFactory.get(map.getKeyJavaType());
            Type valueType = typeFactory.get(map.getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
        } else {
            Type valueType = typeFactory.get(((PluralAttribute<?, ?, ?>) p).getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), valueType));
        }
    } else {
        propertyType = getPropertyType(p, propertyType);
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Property(com.querydsl.codegen.Property) MappingException(org.hibernate.MappingException)

Example 24 with Type

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

the class MetaDataExporter method handleColumn.

private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
    String columnName = normalize(columns.getString("COLUMN_NAME"));
    String normalizedColumnName = namingStrategy.normalizeColumnName(columnName);
    int columnType = columns.getInt("DATA_TYPE");
    String typeName = columns.getString("TYPE_NAME");
    Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
    Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
    int columnIndex = columns.getInt("ORDINAL_POSITION");
    int nullable = columns.getInt("NULLABLE");
    String columnDefaultValue = columns.getString("COLUMN_DEF");
    String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
    Class<?> clazz = configuration.getJavaType(columnType, typeName, columnSize != null ? columnSize.intValue() : 0, columnDigits != null ? columnDigits.intValue() : 0, tableName, columnName);
    if (clazz == null) {
        clazz = Object.class;
    }
    TypeCategory fieldType = TypeCategory.get(clazz.getName());
    if (Number.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.NUMERIC;
    } else if (Enum.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.ENUM;
    }
    Type typeModel = new ClassType(fieldType, clazz);
    Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
    ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
    if (nullable == DatabaseMetaData.columnNoNulls) {
        column = column.notNull();
    }
    if (columnSize != null) {
        column = column.withSize(columnSize.intValue());
    }
    if (columnDigits != null) {
        column = column.withDigits(columnDigits.intValue());
    }
    property.getData().put("COLUMN", column);
    if (columnAnnotations) {
        property.addAnnotation(new ColumnImpl(normalizedColumnName));
    }
    if (validationAnnotations) {
        if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) {
            property.addAnnotation(new NotNullImpl());
        }
        int size = columns.getInt("COLUMN_SIZE");
        if (size > 0 && clazz.equals(String.class)) {
            property.addAnnotation(new SizeImpl(0, size));
        }
    }
    classModel.addProperty(property);
}
Also used : ColumnMetadata(com.querydsl.sql.ColumnMetadata) NotNullImpl(com.querydsl.sql.codegen.support.NotNullImpl) ClassType(com.querydsl.codegen.utils.model.ClassType) SizeImpl(com.querydsl.sql.codegen.support.SizeImpl) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) ColumnImpl(com.querydsl.sql.ColumnImpl) Property(com.querydsl.codegen.Property)

Example 25 with Type

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

the class MetaDataExporter method createEntityType.

protected EntityType createEntityType(SchemaAndTable schemaAndTable, final String className) {
    EntityType classModel;
    if (beanSerializer == null) {
        String packageName = normalizePackage(module.getPackageName(), schemaAndTable);
        String simpleName = module.getPrefix() + className + module.getSuffix();
        Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + simpleName, packageName, simpleName, false, false);
        classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
        typeMappings.register(classModel, classModel);
    } else {
        String beanPackage = normalizePackage(beanPackageName, schemaAndTable);
        String simpleName = module.getBeanPrefix() + className + module.getBeanSuffix();
        Type classTypeModel = new SimpleType(TypeCategory.ENTITY, beanPackage + "." + simpleName, beanPackage, simpleName, false, false);
        classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
        Type mappedType = queryTypeFactory.create(classModel);
        entityToWrapped.put(classModel, mappedType);
        typeMappings.register(classModel, mappedType);
    }
    classModel.getData().put("schema", schemaAndTable.getSchema());
    classModel.getData().put("table", schemaAndTable.getTable());
    return classModel;
}
Also used : EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType)

Aggregations

Type (com.querydsl.codegen.utils.model.Type)56 SimpleType (com.querydsl.codegen.utils.model.SimpleType)35 ClassType (com.querydsl.codegen.utils.model.ClassType)23 EntityType (com.querydsl.codegen.EntityType)15 Test (org.junit.Test)14 PropertyType (com.querydsl.core.annotations.PropertyType)9 QueryType (com.querydsl.core.annotations.QueryType)9 DeclaredType (javax.lang.model.type.DeclaredType)9 NoType (javax.lang.model.type.NoType)9 Parameter (com.querydsl.codegen.utils.model.Parameter)7 ArrayList (java.util.ArrayList)7 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)6 StringWriter (java.io.StringWriter)6 ArrayType (javax.lang.model.type.ArrayType)6 ErrorType (javax.lang.model.type.ErrorType)6 ExecutableType (javax.lang.model.type.ExecutableType)6 NullType (javax.lang.model.type.NullType)6 PrimitiveType (javax.lang.model.type.PrimitiveType)6 WildcardType (javax.lang.model.type.WildcardType)6 Annotation (java.lang.annotation.Annotation)5