Search in sources :

Example 1 with FieldDescriptor

use of jodd.introspector.FieldDescriptor in project jodd by oblac.

the class JsonAnnotationManager method scanClassForAnnotations.

/**
	 * Scans class for annotations and returns {@link jodd.json.meta.JsonAnnotationManager.TypeData}.
	 */
private TypeData scanClassForAnnotations(Class type) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
    ArrayList<String> includedList = new ArrayList<>();
    ArrayList<String> excludedList = new ArrayList<>();
    ArrayList<String> jsonNames = new ArrayList<>();
    ArrayList<String> realNames = new ArrayList<>();
    JSONAnnotation jsonAnnotation = new JSONAnnotation(JoddJson.jsonAnnotation);
    for (PropertyDescriptor pd : pds) {
        JSONAnnotationData data = null;
        {
            MethodDescriptor md = pd.getReadMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            MethodDescriptor md = pd.getWriteMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            FieldDescriptor fd = pd.getFieldDescriptor();
            if (fd != null) {
                Field field = fd.getField();
                data = jsonAnnotation.readAnnotationData(field);
            }
        }
        if (data != null) {
            // annotation found
            String propertyName = pd.getName();
            String newPropertyName = data.getName();
            if (newPropertyName != null) {
                realNames.add(propertyName);
                jsonNames.add(newPropertyName);
                propertyName = newPropertyName;
            }
            if (data.isIncluded()) {
                includedList.add(propertyName);
            } else {
                excludedList.add(propertyName);
            }
        }
    }
    String[] reals = null;
    if (!realNames.isEmpty()) {
        reals = realNames.toArray(new String[realNames.size()]);
    }
    String[] jsons = null;
    if (!jsonNames.isEmpty()) {
        jsons = jsonNames.toArray(new String[jsonNames.size()]);
    }
    // type
    JSONAnnotationData data = (JSONAnnotationData) jsonAnnotation.readAnnotationData(type);
    return new TypeData(includedList, excludedList, data != null && data.isStrict(), jsons, reals);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) Field(java.lang.reflect.Field)

Example 2 with FieldDescriptor

use of jodd.introspector.FieldDescriptor in project jodd by oblac.

the class PropertyResolver method resolve.

/**
	 * Resolves all properties for given type.
	 */
public PropertyInjectionPoint[] resolve(Class type, boolean autowire) {
    // lookup fields
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<PropertyInjectionPoint> list = new ArrayList<>();
    PropertyDescriptor[] allPropertyDescriptors = cd.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : allPropertyDescriptors) {
        if (propertyDescriptor.isGetterOnly()) {
            continue;
        }
        Class propertyType = propertyDescriptor.getType();
        if (ReflectUtil.isTypeOf(propertyType, Collection.class)) {
            continue;
        }
        MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
        FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
        PetiteInject ref = null;
        if (writeMethodDescriptor != null) {
            ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
        }
        if (ref == null && fieldDescriptor != null) {
            ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
        }
        if ((!autowire) && (ref == null)) {
            continue;
        }
        String[] refName = null;
        if (ref != null) {
            String name = ref.value().trim();
            if (name.length() != 0) {
                refName = new String[] { name };
            }
        }
        list.add(injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, refName));
    }
    PropertyInjectionPoint[] fields;
    if (list.isEmpty()) {
        fields = PropertyInjectionPoint.EMPTY;
    } else {
        fields = list.toArray(new PropertyInjectionPoint[list.size()]);
    }
    return fields;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) PetiteInject(jodd.petite.meta.PetiteInject) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) PropertyInjectionPoint(jodd.petite.PropertyInjectionPoint)

Example 3 with FieldDescriptor

use of jodd.introspector.FieldDescriptor in project jodd by oblac.

the class SetResolver method resolve.

/**
	 * Resolves all collections for given type.
	 */
public SetInjectionPoint[] resolve(Class type, boolean autowire) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<SetInjectionPoint> list = new ArrayList<>();
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        if (propertyDescriptor.isGetterOnly()) {
            continue;
        }
        Class propertyType = propertyDescriptor.getType();
        if (!ReflectUtil.isTypeOf(propertyType, Collection.class)) {
            continue;
        }
        MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
        FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
        PetiteInject ref = null;
        if (writeMethodDescriptor != null) {
            ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
        }
        if (ref == null && fieldDescriptor != null) {
            ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
        }
        if ((!autowire) && (ref == null)) {
            continue;
        }
        list.add(injectionPointFactory.createSetInjectionPoint(propertyDescriptor));
    }
    SetInjectionPoint[] fields;
    if (list.isEmpty()) {
        fields = SetInjectionPoint.EMPTY;
    } else {
        fields = list.toArray(new SetInjectionPoint[list.size()]);
    }
    return fields;
}
Also used : SetInjectionPoint(jodd.petite.SetInjectionPoint) ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) Collection(java.util.Collection) PetiteInject(jodd.petite.meta.PetiteInject) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 4 with FieldDescriptor

use of jodd.introspector.FieldDescriptor in project jodd by oblac.

the class BeanVisitor method getAllBeanPropertyNames.

// ---------------------------------------------------------------- util
/**
	 * Returns all bean property names.
	 */
protected String[] getAllBeanPropertyNames(Class type, boolean declared) {
    ClassDescriptor classDescriptor = ClassIntrospector.lookup(type);
    PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors();
    ArrayList<String> names = new ArrayList<>(propertyDescriptors.length);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        MethodDescriptor getter = propertyDescriptor.getReadMethodDescriptor();
        if (getter != null) {
            if (getter.matchDeclared(declared)) {
                names.add(propertyDescriptor.getName());
            }
        } else if (includeFields) {
            FieldDescriptor field = propertyDescriptor.getFieldDescriptor();
            if (field != null) {
                if (field.matchDeclared(declared)) {
                    names.add(field.getName());
                }
            }
        }
    }
    return names.toArray(new String[names.size()]);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 5 with FieldDescriptor

use of jodd.introspector.FieldDescriptor in project jodd by oblac.

the class ValidationContext method collectPropertyAnnotationChecks.

/**
 * Process all annotations of provided properties.
 */
protected void collectPropertyAnnotationChecks(final List<Check> annChecks, final PropertyDescriptor propertyDescriptor) {
    final FieldDescriptor fd = propertyDescriptor.getFieldDescriptor();
    if (fd != null) {
        final Annotation[] annotations = fd.getField().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor();
    if (md != null) {
        final Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    md = propertyDescriptor.getWriteMethodDescriptor();
    if (md != null) {
        final Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
}
Also used : MethodDescriptor(jodd.introspector.MethodDescriptor) Annotation(java.lang.annotation.Annotation) FieldDescriptor(jodd.introspector.FieldDescriptor)

Aggregations

FieldDescriptor (jodd.introspector.FieldDescriptor)6 ClassDescriptor (jodd.introspector.ClassDescriptor)5 MethodDescriptor (jodd.introspector.MethodDescriptor)5 PropertyDescriptor (jodd.introspector.PropertyDescriptor)5 ArrayList (java.util.ArrayList)4 PetiteInject (jodd.petite.meta.PetiteInject)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Getter (jodd.introspector.Getter)1 PropertyInjectionPoint (jodd.petite.PropertyInjectionPoint)1 SetInjectionPoint (jodd.petite.SetInjectionPoint)1