Search in sources :

Example 11 with MethodDescriptor

use of jodd.introspector.MethodDescriptor 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 12 with MethodDescriptor

use of jodd.introspector.MethodDescriptor 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 13 with MethodDescriptor

use of jodd.introspector.MethodDescriptor 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 14 with MethodDescriptor

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

the class DestroyMethodResolver method resolve.

public DestroyMethodPoint[] resolve(Object bean) {
    Class<?> type = bean.getClass();
    // lookup methods
    List<DestroyMethodPoint> list = new ArrayList<>();
    ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null);
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        PetiteDestroyMethod petiteDestroyMethod = method.getAnnotation(PetiteDestroyMethod.class);
        if (petiteDestroyMethod == null) {
            continue;
        }
        if (method.getParameterTypes().length > 0) {
            throw new PetiteException("Arguments are not allowed for Petite destroy method: " + type.getName() + '#' + method.getName());
        }
        list.add(new DestroyMethodPoint(method));
    }
    DestroyMethodPoint[] methods;
    if (list.isEmpty()) {
        methods = DestroyMethodPoint.EMPTY;
    } else {
        methods = list.toArray(new DestroyMethodPoint[list.size()]);
    }
    return methods;
}
Also used : DestroyMethodPoint(jodd.petite.DestroyMethodPoint) ClassDescriptor(jodd.introspector.ClassDescriptor) PetiteDestroyMethod(jodd.petite.meta.PetiteDestroyMethod) ArrayList(java.util.ArrayList) PetiteDestroyMethod(jodd.petite.meta.PetiteDestroyMethod) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) PetiteException(jodd.petite.PetiteException)

Example 15 with MethodDescriptor

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

the class InitMethodResolver method resolve.

public InitMethodPoint[] resolve(Object bean) {
    Class<?> type = bean.getClass();
    // lookup methods
    List<InitMethodPoint> list = new ArrayList<>();
    ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null);
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        PetiteInitMethod petiteInitMethod = method.getAnnotation(PetiteInitMethod.class);
        if (petiteInitMethod == null) {
            continue;
        }
        if (method.getParameterTypes().length > 0) {
            throw new PetiteException("Arguments are not allowed for Petite init method: " + type.getName() + '#' + method.getName());
        }
        int order = petiteInitMethod.order();
        list.add(new InitMethodPoint(method, order, petiteInitMethod.invoke()));
    }
    InitMethodPoint[] methods;
    if (list.isEmpty()) {
        methods = InitMethodPoint.EMPTY;
    } else {
        Collections.sort(list);
        methods = list.toArray(new InitMethodPoint[list.size()]);
    }
    return methods;
}
Also used : InitMethodPoint(jodd.petite.InitMethodPoint) PetiteInitMethod(jodd.petite.meta.PetiteInitMethod) ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) PetiteInitMethod(jodd.petite.meta.PetiteInitMethod) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) PetiteException(jodd.petite.PetiteException) InitMethodPoint(jodd.petite.InitMethodPoint)

Aggregations

MethodDescriptor (jodd.introspector.MethodDescriptor)16 ClassDescriptor (jodd.introspector.ClassDescriptor)15 ArrayList (java.util.ArrayList)8 Method (java.lang.reflect.Method)7 FieldDescriptor (jodd.introspector.FieldDescriptor)5 PropertyDescriptor (jodd.introspector.PropertyDescriptor)5 PetiteInject (jodd.petite.meta.PetiteInject)3 PetiteException (jodd.petite.PetiteException)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 MadvocAction (jodd.madvoc.meta.MadvocAction)1 DestroyMethodPoint (jodd.petite.DestroyMethodPoint)1 InitMethodPoint (jodd.petite.InitMethodPoint)1 MethodInjectionPoint (jodd.petite.MethodInjectionPoint)1 PropertyInjectionPoint (jodd.petite.PropertyInjectionPoint)1 ProviderDefinition (jodd.petite.ProviderDefinition)1 SetInjectionPoint (jodd.petite.SetInjectionPoint)1 PetiteDestroyMethod (jodd.petite.meta.PetiteDestroyMethod)1 PetiteInitMethod (jodd.petite.meta.PetiteInitMethod)1