Search in sources :

Example 11 with Type

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

the class MetaDataSerializer method introDefaultInstance.

@Override
protected void introDefaultInstance(CodeWriter writer, EntityType entityType, String defaultName) throws IOException {
    String variableName = !defaultName.isEmpty() ? defaultName : namingStrategy.getDefaultVariableName(entityType);
    String alias = namingStrategy.getDefaultAlias(entityType);
    Type queryType = typeMappings.getPathType(entityType, entityType, true);
    writer.publicStaticFinal(queryType, variableName, NEW + queryType.getSimpleName() + "(\"" + alias + "\")");
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType)

Example 12 with Type

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

the class MetaDataSerializer method serializeProperties.

@SuppressWarnings("unchecked")
@Override
protected void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
    Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
    if (innerClassesForKeys) {
        Type primaryKeyType = new SimpleType(namingStrategy.getPrimaryKeysClassName());
        Type foreignKeysType = new SimpleType(namingStrategy.getForeignKeysClassName());
        // primary keys
        if (primaryKeys != null) {
            writer.beginClass(primaryKeyType);
            serializePrimaryKeys(model, writer, primaryKeys);
            writer.end();
        }
        // foreign keys
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.beginClass(foreignKeysType);
            if (foreignKeys != null) {
                serializeForeignKeys(model, writer, foreignKeys, false);
            }
            // inverse foreign keys
            if (inverseForeignKeys != null) {
                serializeForeignKeys(model, writer, inverseForeignKeys, true);
            }
            writer.end();
        }
        super.serializeProperties(model, config, writer);
        if (primaryKeys != null) {
            writer.publicFinal(primaryKeyType, namingStrategy.getPrimaryKeysVariable(model), "new " + primaryKeyType.getSimpleName() + "()");
        }
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.publicFinal(foreignKeysType, namingStrategy.getForeignKeysVariable(model), "new " + foreignKeysType.getSimpleName() + "()");
        }
    } else {
        super.serializeProperties(model, config, writer);
        // primary keys
        if (primaryKeys != null) {
            serializePrimaryKeys(model, writer, primaryKeys);
        }
        // foreign keys
        if (foreignKeys != null) {
            serializeForeignKeys(model, writer, foreignKeys, false);
        }
        // inverse foreign keys
        if (inverseForeignKeys != null) {
            serializeForeignKeys(model, writer, inverseForeignKeys, true);
        }
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) SimpleType(com.querydsl.codegen.utils.model.SimpleType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) Collection(java.util.Collection) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 13 with Type

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

the class MetaDataSerializer method customField.

@Override
protected void customField(EntityType model, Property field, SerializerConfig config, CodeWriter writer) throws IOException {
    Type queryType = typeMappings.getPathType(field.getType(), model, false);
    if (queryType.getPackageName().startsWith("com.querydsl")) {
        String localRawName = writer.getRawName(field.getType());
        serialize(model, field, queryType, writer, "create" + field.getType().getSimpleName(), writer.getClassConstant(localRawName));
    } else {
        super.customField(model, field, config, writer);
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType)

Example 14 with Type

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

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

the class GenericExporter method addProperties.

private void addProperties(Class<?> cl, EntityType type) {
    Map<String, Type> types = new HashMap<>();
    Map<String, Annotations> annotations = new HashMap<>();
    PropertyHandling.Config config = propertyHandling.getConfig(cl);
    // fields
    if (config.isFields()) {
        for (Field field : cl.getDeclaredFields()) {
            if (!Modifier.isStatic(field.getModifiers())) {
                if (Modifier.isTransient(field.getModifiers()) && !field.isAnnotationPresent(QueryType.class)) {
                    continue;
                }
                AnnotatedElement annotated = ReflectionUtils.getAnnotatedElement(cl, field.getName(), field.getType());
                Type propertyType = getPropertyType(cl, annotated, field.getType(), field.getGenericType());
                Annotations ann = new Annotations(field);
                types.put(field.getName(), propertyType);
                annotations.put(field.getName(), ann);
            }
        }
    }
    // getters
    if (config.isMethods()) {
        for (Method method : cl.getDeclaredMethods()) {
            String name = method.getName();
            if (method.getParameterTypes().length == 0 && !Modifier.isStatic(method.getModifiers()) && !method.isBridge() && ((name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2))) {
                String propertyName;
                if (name.startsWith("get")) {
                    propertyName = BeanUtils.uncapitalize(name.substring(3));
                } else {
                    propertyName = BeanUtils.uncapitalize(name.substring(2));
                }
                Type propertyType = getPropertyType(cl, method, method.getReturnType(), method.getGenericReturnType());
                if (!types.containsKey(propertyName) || !useFieldTypes) {
                    types.put(propertyName, propertyType);
                }
                Annotations ann = annotations.get(propertyName);
                if (ann == null) {
                    ann = new Annotations();
                    annotations.put(propertyName, ann);
                }
                ann.addAnnotations(method);
            }
        }
    }
    for (Map.Entry<String, Type> entry : types.entrySet()) {
        Annotations ann = annotations.get(entry.getKey());
        Property property = createProperty(type, entry.getKey(), entry.getValue(), ann);
        if (property != null) {
            type.addProperty(property);
        }
    }
}
Also used : HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) QueryType(com.querydsl.core.annotations.QueryType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) Annotations(com.querydsl.core.util.Annotations) HashMap(java.util.HashMap) Map(java.util.Map)

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