Search in sources :

Example 26 with ClassDescriptor

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

the class PetiteBeans method registerPetiteInitMethods.

/**
	 * Registers init method.
	 *
	 * @param beanName bean name
	 * @param invocationStrategy moment of invocation
	 * @param initMethodNames init method names
	 */
public void registerPetiteInitMethods(String beanName, InitMethodInvocationStrategy invocationStrategy, String... initMethodNames) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
    if (initMethodNames == null) {
        initMethodNames = StringPool.EMPTY_ARRAY;
    }
    int total = initMethodNames.length;
    InitMethodPoint[] initMethodPoints = new InitMethodPoint[total];
    int i;
    for (i = 0; i < initMethodNames.length; i++) {
        MethodDescriptor md = cd.getMethodDescriptor(initMethodNames[i], ReflectUtil.NO_PARAMETERS, true);
        if (md == null) {
            throw new PetiteException("Init method not found: " + beanDefinition.type.getName() + '#' + initMethodNames[i]);
        }
        initMethodPoints[i] = new InitMethodPoint(md.getMethod(), i, invocationStrategy);
    }
    beanDefinition.addInitMethodPoints(initMethodPoints);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) MethodDescriptor(jodd.introspector.MethodDescriptor)

Example 27 with ClassDescriptor

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

the class PetiteBeans method registerPetiteMethodInjectionPoint.

/**
	 * Registers method injection point.
	 *
	 * @param beanName bean name
	 * @param methodName method name
	 * @param arguments method arguments, may be <code>null</code>
	 * @param references injection references
	 */
public void registerPetiteMethodInjectionPoint(String beanName, String methodName, Class[] arguments, String[] references) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    String[][] ref = PetiteUtil.convertRefToReferences(references);
    ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
    Method method = null;
    if (arguments == null) {
        MethodDescriptor[] methods = cd.getAllMethodDescriptors(methodName);
        if (methods != null && methods.length > 0) {
            if (methods.length > 1) {
                throw new PetiteException(methods.length + " suitable methods found as injection points for: " + beanDefinition.type.getName() + '#' + methodName);
            }
            method = methods[0].getMethod();
        }
    } else {
        MethodDescriptor md = cd.getMethodDescriptor(methodName, arguments, true);
        if (md != null) {
            method = md.getMethod();
        }
    }
    if (method == null) {
        throw new PetiteException("Method not found: " + beanDefinition.type.getName() + '#' + methodName);
    }
    MethodInjectionPoint mip = injectionPointFactory.createMethodInjectionPoint(method, ref);
    beanDefinition.addMethodInjectionPoint(mip);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor)

Example 28 with ClassDescriptor

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

the class PetiteBeans method registerPetitePropertyInjectionPoint.

/**
	 * Registers property injection point.
	 *
	 * @param beanName bean name
	 * @param property property name
	 * @param reference explicit injection reference, may be <code>null</code>
	 */
public void registerPetitePropertyInjectionPoint(String beanName, String property, String reference) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    String[] references = reference == null ? null : new String[] { reference };
    ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
    PropertyDescriptor propertyDescriptor = cd.getPropertyDescriptor(property, true);
    if (propertyDescriptor == null) {
        throw new PetiteException("Property not found: " + beanDefinition.type.getName() + '#' + property);
    }
    PropertyInjectionPoint pip = injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, references);
    beanDefinition.addPropertyInjectionPoint(pip);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor)

Example 29 with ClassDescriptor

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

the class PetiteBeans method registerPetiteProvider.

/**
	 * Registers static method provider.
	 *
	 * @param providerName provider name
	 * @param type class type
	 * @param staticMethodName static method name
	 * @param arguments method argument types
	 */
public void registerPetiteProvider(String providerName, Class type, String staticMethodName, Class[] arguments) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    MethodDescriptor md = cd.getMethodDescriptor(staticMethodName, arguments, true);
    if (md == null) {
        throw new PetiteException("Provider method not found: " + staticMethodName);
    }
    ProviderDefinition providerDefinition = new ProviderDefinition(providerName, md.getMethod());
    providers.put(providerName, providerDefinition);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) MethodDescriptor(jodd.introspector.MethodDescriptor)

Example 30 with ClassDescriptor

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