Search in sources :

Example 6 with ClassDescriptor

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

the class JsonParserBase method newObjectInstance.

/**
	 * Creates new object or a <code>HashMap</code> if type is not specified.
	 */
protected Object newObjectInstance(Class targetType) {
    if (targetType == null || targetType == Map.class) {
        return new HashMap();
    }
    ClassDescriptor cd = ClassIntrospector.lookup(targetType);
    CtorDescriptor ctorDescriptor = cd.getDefaultCtorDescriptor(true);
    if (ctorDescriptor == null) {
        throw new JsonException("Default ctor not found for: " + targetType.getName());
    }
    try {
        return ctorDescriptor.getConstructor().newInstance();
    } catch (Exception e) {
        throw new JsonException(e);
    }
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) HashMap(java.util.HashMap) CtorDescriptor(jodd.introspector.CtorDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ClassDescriptor

use of jodd.introspector.ClassDescriptor 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 8 with ClassDescriptor

use of jodd.introspector.ClassDescriptor 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 9 with ClassDescriptor

use of jodd.introspector.ClassDescriptor 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 10 with ClassDescriptor

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

the class PetiteBeans method registerPetiteCtorInjectionPoint.

// ---------------------------------------------------------------- injection points
/**
	 * Registers constructor injection point.
	 *
	 * @param beanName bean name
	 * @param paramTypes constructor parameter types, may be <code>null</code>
	 * @param references references for arguments
	 */
public void registerPetiteCtorInjectionPoint(String beanName, Class[] paramTypes, String[] references) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    String[][] ref = PetiteUtil.convertRefToReferences(references);
    ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
    Constructor constructor = null;
    if (paramTypes == null) {
        CtorDescriptor[] ctors = cd.getAllCtorDescriptors();
        if (ctors != null && ctors.length > 0) {
            if (ctors.length > 1) {
                throw new PetiteException(ctors.length + " suitable constructor found as injection point for: " + beanDefinition.type.getName());
            }
            constructor = ctors[0].getConstructor();
        }
    } else {
        CtorDescriptor ctorDescriptor = cd.getCtorDescriptor(paramTypes, true);
        if (ctorDescriptor != null) {
            constructor = ctorDescriptor.getConstructor();
        }
    }
    if (constructor == null) {
        throw new PetiteException("Constructor not found: " + beanDefinition.type.getName());
    }
    beanDefinition.ctor = injectionPointFactory.createCtorInjectionPoint(constructor, ref);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) Constructor(java.lang.reflect.Constructor) CtorDescriptor(jodd.introspector.CtorDescriptor)

Aggregations

ClassDescriptor (jodd.introspector.ClassDescriptor)33 PropertyDescriptor (jodd.introspector.PropertyDescriptor)16 MethodDescriptor (jodd.introspector.MethodDescriptor)15 ArrayList (java.util.ArrayList)10 Method (java.lang.reflect.Method)8 FieldDescriptor (jodd.introspector.FieldDescriptor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 PetiteInject (jodd.petite.meta.PetiteInject)4 Map (java.util.Map)3 CtorDescriptor (jodd.introspector.CtorDescriptor)3 PetiteException (jodd.petite.PetiteException)3 Annotation (java.lang.annotation.Annotation)2 Constructor (java.lang.reflect.Constructor)2 Collection (java.util.Collection)2 LifeBean (jodd.bean.data.LifeBean)2 CachingIntrospector (jodd.introspector.CachingIntrospector)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1