Search in sources :

Example 1 with PetiteInject

use of jodd.petite.meta.PetiteInject in project jodd by oblac.

the class MethodResolver method resolve.

public MethodInjectionPoint[] resolve(Class type) {
    // lookup methods
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<MethodInjectionPoint> list = new ArrayList<>();
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        if (ReflectUtil.isBeanPropertySetter(method)) {
            // ignore setters
            continue;
        }
        if (method.getParameterTypes().length == 0) {
            // ignore methods with no argument
            continue;
        }
        PetiteInject ref = method.getAnnotation(PetiteInject.class);
        if (ref == null) {
            continue;
        }
        String[][] references = PetiteUtil.convertAnnValueToReferences(ref.value());
        list.add(injectionPointFactory.createMethodInjectionPoint(method, references));
    }
    MethodInjectionPoint[] methods;
    if (list.isEmpty()) {
        methods = MethodInjectionPoint.EMPTY;
    } else {
        methods = list.toArray(new MethodInjectionPoint[list.size()]);
    }
    return methods;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) PetiteInject(jodd.petite.meta.PetiteInject) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) MethodInjectionPoint(jodd.petite.MethodInjectionPoint)

Example 2 with PetiteInject

use of jodd.petite.meta.PetiteInject 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 PetiteInject

use of jodd.petite.meta.PetiteInject 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 PetiteInject

use of jodd.petite.meta.PetiteInject in project jodd by oblac.

the class CtorResolver method resolve.

/**
	 * Resolves constructor injection point from type. Looks for single annotated constructor.
	 * If no annotated constructors found, the total number of constructors will be checked.
	 * If there is only one constructor, that one will be used as injection point. If more
	 * constructors exist, the default one will be used as injection point. Otherwise, exception
	 * is thrown.
	 */
public CtorInjectionPoint resolve(Class type, boolean useAnnotation) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    CtorDescriptor[] allCtors = cd.getAllCtorDescriptors();
    Constructor foundedCtor = null;
    Constructor defaultCtor = null;
    String refValues = null;
    for (CtorDescriptor ctorDescriptor : allCtors) {
        Constructor<?> ctor = ctorDescriptor.getConstructor();
        Class<?>[] paramTypes = ctor.getParameterTypes();
        if (paramTypes.length == 0) {
            // detects default ctors
            defaultCtor = ctor;
        }
        if (!useAnnotation) {
            continue;
        }
        PetiteInject ref = ctor.getAnnotation(PetiteInject.class);
        if (ref == null) {
            continue;
        }
        if (foundedCtor != null) {
            throw new PetiteException("Two or more constructors are annotated as injection points in bean: " + type.getName());
        }
        foundedCtor = ctor;
        refValues = ref.value().trim();
    }
    if (foundedCtor == null) {
        if (allCtors.length == 1) {
            foundedCtor = allCtors[0].getConstructor();
        } else {
            foundedCtor = defaultCtor;
        }
    }
    if (foundedCtor == null) {
        throw new PetiteException("No constructor (annotated, single or default) founded as injection point for: " + type.getName());
    }
    String[][] references = PetiteUtil.convertAnnValueToReferences(refValues);
    return injectionPointFactory.createCtorInjectionPoint(foundedCtor, references);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) Constructor(java.lang.reflect.Constructor) PetiteInject(jodd.petite.meta.PetiteInject) CtorDescriptor(jodd.introspector.CtorDescriptor) PetiteException(jodd.petite.PetiteException)

Aggregations

ClassDescriptor (jodd.introspector.ClassDescriptor)4 PetiteInject (jodd.petite.meta.PetiteInject)4 ArrayList (java.util.ArrayList)3 MethodDescriptor (jodd.introspector.MethodDescriptor)3 FieldDescriptor (jodd.introspector.FieldDescriptor)2 PropertyDescriptor (jodd.introspector.PropertyDescriptor)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 CtorDescriptor (jodd.introspector.CtorDescriptor)1 MethodInjectionPoint (jodd.petite.MethodInjectionPoint)1 PetiteException (jodd.petite.PetiteException)1 PropertyInjectionPoint (jodd.petite.PropertyInjectionPoint)1 SetInjectionPoint (jodd.petite.SetInjectionPoint)1