Search in sources :

Example 16 with Type

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

the class GenericExporter method serialize.

private void serialize(Serializer serializer, Map<Class<?>, EntityType> types) throws IOException {
    for (Map.Entry<Class<?>, EntityType> entityType : types.entrySet()) {
        Type type = typeMappings.getPathType(entityType.getValue(), entityType.getValue(), true);
        String packageName = type.getPackageName();
        String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
        SerializerConfig config = serializerConfig;
        if (entityType.getKey().isAnnotationPresent(Config.class)) {
            config = SimpleSerializerConfig.getConfig(entityType.getKey().getAnnotation(Config.class));
        }
        String fileSuffix = createScalaSources ? ".scala" : ".java";
        write(serializer, className.replace('.', '/') + fileSuffix, config, entityType.getValue());
    }
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with Type

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

the class BeanSerializerTest method properties.

@Test
public void properties() throws IOException {
    // property
    type.addProperty(new Property(type, "entityField", type));
    type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
    type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
    type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
    type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
    type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
    for (Class<?> cl : Arrays.<Class<?>>asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)) {
        Type classType = new ClassType(TypeCategory.get(cl.getName()), cl);
        type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType));
    }
    BeanSerializer serializer = new BeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String str = writer.toString();
    // System.err.println(str);
    for (String prop : Arrays.asList("String[] arrayField;", "Boolean boolean$;", "Collection<DomainClass> collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List<DomainClass> listField;", "Map<DomainClass, DomainClass> mapField;", "Set<DomainClass> setField;", "java.sql.Time time;")) {
        assertTrue(prop + " was not contained", str.contains(prop));
    }
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) JavaWriter(com.querydsl.codegen.utils.JavaWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Example 18 with Type

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

the class TypeFactory method get.

public Type get(boolean entity, Class<?> cl, AnnotatedElement annotated, java.lang.reflect.Type genericType) {
    List<Object> key = new ArrayList<>();
    key.add(genericType);
    AnnotationHelper annotationHelper = null;
    Annotation selectedAnnotation = null;
    if (annotated != null) {
        for (Annotation annotation : annotated.getDeclaredAnnotations()) {
            for (AnnotationHelper helper : annotationHelpers) {
                if (helper.isSupported(annotation.annotationType())) {
                    key.add(annotation.annotationType());
                    selectedAnnotation = annotated.getAnnotation(annotation.annotationType());
                    annotationHelper = helper;
                    key.add(helper.getCustomKey(selectedAnnotation));
                    break;
                }
            }
        }
    }
    key = Collections.unmodifiableList(key);
    if (cache.containsKey(key)) {
        Type value = cache.get(key);
        if (entity && !(value instanceof EntityType)) {
            value = new EntityType(value, variableNameFunction);
            cache.put(key, value);
        }
        return value;
    } else {
        Type value = create(entity, cl, annotationHelper, selectedAnnotation, genericType, key);
        cache.put(key, value);
        return value;
    }
}
Also used : Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) WildcardType(java.lang.reflect.WildcardType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation)

Example 19 with Type

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

the class TypeFactory method create.

private Type create(boolean entity, Class<?> cl, AnnotationHelper annotationHelper, Annotation annotation, java.lang.reflect.Type genericType, List<?> key) {
    if (cl.isPrimitive()) {
        cl = PrimitiveUtils.wrap(cl);
    }
    Type value;
    Type[] tempParams = (Type[]) Array.newInstance(Type.class, ReflectionUtils.getTypeParameterCount(genericType));
    cache.put(key, new ClassType(cl, tempParams));
    Type[] parameters = getParameters(cl, genericType);
    if (cl.isArray()) {
        Type componentType = get(cl.getComponentType());
        if (cl.getComponentType().isPrimitive()) {
            componentType = Types.PRIMITIVES.get(componentType);
        }
        value = componentType.asArrayType();
    } else if (cl.isEnum()) {
        value = new ClassType(TypeCategory.ENUM, cl);
    } else if (Number.class.isAssignableFrom(cl) && Comparable.class.isAssignableFrom(cl)) {
        value = new ClassType(TypeCategory.NUMERIC, cl, parameters);
    } else if (entity) {
        value = createOther(cl, entity, annotationHelper, annotation, parameters);
    } else if (Map.class.isAssignableFrom(cl)) {
        value = new SimpleType(Types.MAP, parameters[0], asGeneric(parameters[1]));
    } else if (List.class.isAssignableFrom(cl)) {
        value = new SimpleType(Types.LIST, asGeneric(parameters[0]));
    } else if (Set.class.isAssignableFrom(cl)) {
        value = new SimpleType(Types.SET, asGeneric(parameters[0]));
    } else if (Collection.class.isAssignableFrom(cl)) {
        value = new SimpleType(Types.COLLECTION, asGeneric(parameters[0]));
    } else {
        value = createOther(cl, entity, annotationHelper, annotation, parameters);
    }
    if (genericType instanceof TypeVariable) {
        TypeVariable tv = (TypeVariable) genericType;
        if (tv.getBounds().length == 1 && tv.getBounds()[0].equals(Object.class)) {
            value = new TypeSuper(tv.getName(), value);
        } else {
            value = new TypeExtends(tv.getName(), value);
        }
    }
    if (entity && !(value instanceof EntityType)) {
        value = new EntityType(value, variableNameFunction);
    }
    return value;
}
Also used : ClassType(com.querydsl.codegen.utils.model.ClassType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) TypeExtends(com.querydsl.codegen.utils.model.TypeExtends) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) WildcardType(java.lang.reflect.WildcardType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) TypeVariable(java.lang.reflect.TypeVariable) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) TypeSuper(com.querydsl.codegen.utils.model.TypeSuper)

Example 20 with Type

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

the class TypeResolver method resolve.

/**
 * Resolve type declared in declaringType for context
 *
 * @param type type to be resolved
 * @param declaringType declaration context of type
 * @param context target context of type
 * @return resolved type
 */
public static Type resolve(Type type, Type declaringType, EntityType context) {
    Type resolved = unwrap(type);
    String varName = getVarName(resolved);
    if (varName != null) {
        resolved = resolveVar(resolved, varName, declaringType, context);
    } else if (!resolved.getParameters().isEmpty()) {
        resolved = resolveWithParameters(resolved, declaringType, context);
    }
    // rewrap entity type
    if (type instanceof EntityType) {
        if (!unwrap(type).equals(resolved)) {
            resolved = new EntityType(resolved, ((EntityType) type).getSuperTypes());
        } else {
            // reset to original type
            resolved = type;
        }
    }
    return resolved;
}
Also used : Type(com.querydsl.codegen.utils.model.Type) SimpleType(com.querydsl.codegen.utils.model.SimpleType)

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