Search in sources :

Example 1 with TypeCategory

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

Example 2 with TypeCategory

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

the class MetaDataSerializer method introClassHeader.

@Override
protected void introClassHeader(CodeWriter writer, EntityType model) throws IOException {
    Type queryType = typeMappings.getPathType(model, model, true);
    writer.line("@", generatedAnnotationClass.getSimpleName(), "(\"", getClass().getName(), "\")");
    TypeCategory category = model.getOriginalCategory();
    // serialize annotations only, if no bean types are used
    if (model.equals(queryType)) {
        for (Annotation annotation : model.getAnnotations()) {
            writer.annotation(annotation);
        }
    }
    writer.beginClass(queryType, new ClassType(category, entityPathType, model));
    writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", String.valueOf(model.hashCode()));
}
Also used : 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) ClassType(com.querydsl.codegen.utils.model.ClassType) Annotation(java.lang.annotation.Annotation)

Example 3 with TypeCategory

use of com.querydsl.codegen.utils.model.TypeCategory 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 4 with TypeCategory

use of com.querydsl.codegen.utils.model.TypeCategory 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 5 with TypeCategory

use of com.querydsl.codegen.utils.model.TypeCategory 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)

Aggregations

Type (com.querydsl.codegen.utils.model.Type)6 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)6 EntityType (com.querydsl.codegen.EntityType)4 ClassType (com.querydsl.codegen.utils.model.ClassType)3 SimpleType (com.querydsl.codegen.utils.model.SimpleType)3 Property (com.querydsl.codegen.Property)2 PropertyType (com.querydsl.core.annotations.PropertyType)2 QueryType (com.querydsl.core.annotations.QueryType)2 Annotation (java.lang.annotation.Annotation)2 QueryInit (com.querydsl.core.annotations.QueryInit)1 ColumnImpl (com.querydsl.sql.ColumnImpl)1 ColumnMetadata (com.querydsl.sql.ColumnMetadata)1 NotNullImpl (com.querydsl.sql.codegen.support.NotNullImpl)1 SizeImpl (com.querydsl.sql.codegen.support.SizeImpl)1 TypeElement (javax.lang.model.element.TypeElement)1 ArrayType (javax.lang.model.type.ArrayType)1 DeclaredType (javax.lang.model.type.DeclaredType)1 ErrorType (javax.lang.model.type.ErrorType)1 ExecutableType (javax.lang.model.type.ExecutableType)1 NoType (javax.lang.model.type.NoType)1