Search in sources :

Example 6 with Type

use of com.mysema.codegen.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 : Type(com.mysema.codegen.model.Type)

Example 7 with Type

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

the class GenericExporter method addProperties.

private void addProperties(Class<?> cl, EntityType type) {
    Map<String, Type> types = Maps.newHashMap();
    Map<String, Annotations> annotations = Maps.newHashMap();
    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 : Type(com.mysema.codegen.model.Type) Annotations(com.querydsl.core.util.Annotations)

Example 8 with Type

use of com.mysema.codegen.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.mysema.codegen.model.Type) SimpleType(com.mysema.codegen.model.SimpleType)

Example 9 with Type

use of com.mysema.codegen.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.equal(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.mysema.codegen.model.Type) SimpleType(com.mysema.codegen.model.SimpleType)

Example 10 with Type

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

the class PropertyTest method equals_and_hashCode.

@Test
public void equals_and_hashCode() {
    Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false, false);
    EntityType type = new EntityType(typeModel);
    Property p1 = new Property(type, "property", type, Collections.<String>emptyList());
    Property p2 = new Property(type, "property", type, Collections.<String>emptyList());
    assertEquals(p1, p1);
    assertEquals(p1, p2);
    assertEquals(p1.hashCode(), p2.hashCode());
}
Also used : SimpleType(com.mysema.codegen.model.SimpleType) Type(com.mysema.codegen.model.Type) SimpleType(com.mysema.codegen.model.SimpleType) Test(org.junit.Test)

Aggregations

Type (com.mysema.codegen.model.Type)31 SimpleType (com.mysema.codegen.model.SimpleType)11 EntityType (com.querydsl.codegen.EntityType)8 Test (org.junit.Test)8 ClassType (com.mysema.codegen.model.ClassType)6 PropertyType (com.querydsl.core.annotations.PropertyType)5 QueryType (com.querydsl.core.annotations.QueryType)5 Parameter (com.mysema.codegen.model.Parameter)4 TypeCategory (com.mysema.codegen.model.TypeCategory)4 Property (com.querydsl.codegen.Property)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 DeclaredType (javax.lang.model.type.DeclaredType)3 NoType (javax.lang.model.type.NoType)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 MappingException (org.hibernate.MappingException)2 org.hibernate.mapping (org.hibernate.mapping)2 JavaWriter (com.mysema.codegen.JavaWriter)1 ScalaWriter (com.mysema.codegen.ScalaWriter)1 QueryDelegate (com.querydsl.core.annotations.QueryDelegate)1