Search in sources :

Example 1 with Annotations

use of com.querydsl.core.util.Annotations in project querydsl by querydsl.

the class TypeElementHandler method handleEntityType.

public EntityType handleEntityType(TypeElement element) {
    EntityType entityType = typeFactory.getEntityType(element.asType(), true);
    List<? extends Element> elements = element.getEnclosedElements();
    VisitorConfig config = configuration.getConfig(element, elements);
    Set<String> blockedProperties = new HashSet<String>();
    Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
    Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
    Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
    // constructors
    if (config.visitConstructors()) {
        handleConstructors(entityType, elements);
    }
    // fields
    if (config.visitFieldProperties()) {
        for (VariableElement field : ElementFilter.fieldsIn(elements)) {
            String name = field.getSimpleName().toString();
            if (configuration.isBlockedField(field)) {
                blockedProperties.add(name);
            } else if (configuration.isValidField(field)) {
                Annotations annotations = new Annotations();
                configuration.inspect(field, annotations);
                annotations.addAnnotation(field.getAnnotation(QueryType.class));
                annotations.addAnnotation(field.getAnnotation(QueryInit.class));
                propertyAnnotations.put(name, annotations);
                propertyTypes.put(name, field.asType());
                TypeMirror fixedType = configuration.getRealType(field);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // methods
    if (config.visitMethodProperties()) {
        for (ExecutableElement method : ElementFilter.methodsIn(elements)) {
            String name = method.getSimpleName().toString();
            if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(3));
            } else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(2));
            } else {
                continue;
            }
            if (configuration.isBlockedGetter(method)) {
                blockedProperties.add(name);
            } else if (configuration.isValidGetter(method) && !blockedProperties.contains(name)) {
                Annotations annotations = propertyAnnotations.get(name);
                if (annotations == null) {
                    annotations = new Annotations();
                    propertyAnnotations.put(name, annotations);
                }
                configuration.inspect(method, annotations);
                annotations.addAnnotation(method.getAnnotation(QueryType.class));
                annotations.addAnnotation(method.getAnnotation(QueryInit.class));
                propertyTypes.put(name, method.getReturnType());
                TypeMirror fixedType = configuration.getRealType(method);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // fixed types override property types
    propertyTypes.putAll(fixedTypes);
    for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
        Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
        if (property != null) {
            entityType.addProperty(property);
        }
    }
    return entityType;
}
Also used : HashMap(java.util.HashMap) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) EntityType(com.querydsl.codegen.EntityType) Annotations(com.querydsl.core.util.Annotations) TypeMirror(javax.lang.model.type.TypeMirror) HashMap(java.util.HashMap) Map(java.util.Map) Property(com.querydsl.codegen.Property) HashSet(java.util.HashSet)

Example 2 with Annotations

use of com.querydsl.core.util.Annotations 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)

Example 3 with Annotations

use of com.querydsl.core.util.Annotations in project querydsl by querydsl.

the class AbstractDomainExporter method getAnnotatedElement.

protected AnnotatedElement getAnnotatedElement(Class<?> cl, String propertyName) throws NoSuchMethodException {
    Field field = ReflectionUtils.getFieldOrNull(cl, propertyName);
    Method method = ReflectionUtils.getGetterOrNull(cl, propertyName);
    if (field != null) {
        if (method != null) {
            return new Annotations(field, method);
        } else {
            return field;
        }
    } else if (method != null) {
        return method;
    } else {
        throw new IllegalArgumentException("No property found for " + cl.getName() + "." + propertyName);
    }
}
Also used : Field(java.lang.reflect.Field) Annotations(com.querydsl.core.util.Annotations) Method(java.lang.reflect.Method)

Aggregations

Annotations (com.querydsl.core.util.Annotations)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EntityType (com.querydsl.codegen.EntityType)1 Property (com.querydsl.codegen.Property)1 Type (com.querydsl.codegen.utils.model.Type)1 PropertyType (com.querydsl.core.annotations.PropertyType)1 QueryType (com.querydsl.core.annotations.QueryType)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 HashSet (java.util.HashSet)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1