Search in sources :

Example 11 with ClassDescriptor

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

the class PetiteBeans method registerPetiteProvider.

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

Example 12 with ClassDescriptor

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

the class PetiteBeans method registerPetiteDestroyMethods.

/**
	 * Registers destroy method.
	 *
	 * @param beanName bean name
	 * @param destroyMethodNames destroy method names
	 */
public void registerPetiteDestroyMethods(String beanName, String... destroyMethodNames) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
    if (destroyMethodNames == null) {
        destroyMethodNames = StringPool.EMPTY_ARRAY;
    }
    int total = destroyMethodNames.length;
    DestroyMethodPoint[] destroyMethodPoints = new DestroyMethodPoint[total];
    int i;
    for (i = 0; i < destroyMethodNames.length; i++) {
        MethodDescriptor md = cd.getMethodDescriptor(destroyMethodNames[i], ReflectUtil.NO_PARAMETERS, true);
        if (md == null) {
            throw new PetiteException("Destroy method not found: " + beanDefinition.type.getName() + '#' + destroyMethodNames[i]);
        }
        destroyMethodPoints[i] = new DestroyMethodPoint(md.getMethod());
    }
    beanDefinition.addDestroyMethodPoints(destroyMethodPoints);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) MethodDescriptor(jodd.introspector.MethodDescriptor)

Example 13 with ClassDescriptor

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

the class PetiteBeans method registerPetiteSetInjectionPoint.

/**
	 * Registers set injection point.
	 *
	 * @param beanName bean name
	 * @param property set property name
	 */
public void registerPetiteSetInjectionPoint(String beanName, String property) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    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);
    }
    SetInjectionPoint sip = injectionPointFactory.createSetInjectionPoint(propertyDescriptor);
    beanDefinition.addSetInjectionPoint(sip);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor)

Example 14 with ClassDescriptor

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

the class ScopeDataResolver method inspectClassScopeData.

/**
	 * Inspect action for all In/Out annotations.
	 * Returns <code>null</code> if there are no In and Out data.
	 */
protected ScopeData inspectClassScopeData(Class actionClass, ScopeType scopeType) {
    ClassDescriptor cd = ClassIntrospector.lookup(actionClass);
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    List<ScopeData.In> listIn = new ArrayList<>(allProperties.length);
    List<ScopeData.Out> listOut = new ArrayList<>(allProperties.length);
    for (PropertyDescriptor pd : allProperties) {
        // collect annotations
        In in = null;
        if (pd.getFieldDescriptor() != null) {
            in = pd.getFieldDescriptor().getField().getAnnotation(In.class);
        }
        if (in == null && pd.getWriteMethodDescriptor() != null) {
            in = pd.getWriteMethodDescriptor().getMethod().getAnnotation(In.class);
        }
        if (in == null && pd.getReadMethodDescriptor() != null) {
            in = pd.getReadMethodDescriptor().getMethod().getAnnotation(In.class);
        }
        InOut inout = null;
        if (pd.getFieldDescriptor() != null) {
            inout = pd.getFieldDescriptor().getField().getAnnotation(InOut.class);
        }
        if (inout == null && pd.getWriteMethodDescriptor() != null) {
            inout = pd.getWriteMethodDescriptor().getMethod().getAnnotation(InOut.class);
        }
        if (inout == null && pd.getReadMethodDescriptor() != null) {
            inout = pd.getReadMethodDescriptor().getMethod().getAnnotation(InOut.class);
        }
        Out out = null;
        if (pd.getFieldDescriptor() != null) {
            out = pd.getFieldDescriptor().getField().getAnnotation(Out.class);
        }
        if (out == null && pd.getWriteMethodDescriptor() != null) {
            out = pd.getWriteMethodDescriptor().getMethod().getAnnotation(Out.class);
        }
        if (out == null && pd.getReadMethodDescriptor() != null) {
            out = pd.getReadMethodDescriptor().getMethod().getAnnotation(Out.class);
        }
        if (inout != null) {
            if (in != null || out != null) {
                throw new MadvocException("@InOut can not be used with @In or @Out: " + pd.getClassDescriptor().getClass() + '#' + pd.getName());
            }
        }
        // inspect all
        ScopeData.In ii = inspectIn(in, scopeType, pd.getName(), pd.getType());
        if (ii != null) {
            listIn.add(ii);
        }
        ii = inspectIn(inout, scopeType, pd.getName(), pd.getType());
        if (ii != null) {
            listIn.add(ii);
        }
        ScopeData.Out oi = inspectOut(out, scopeType, pd.getName(), pd.getType());
        if (oi != null) {
            listOut.add(oi);
        }
        oi = inspectOut(inout, scopeType, pd.getName(), pd.getType());
        if (oi != null) {
            listOut.add(oi);
        }
    }
    if ((listIn.isEmpty()) && (listOut.isEmpty())) {
        return null;
    }
    ScopeData scopeData = new ScopeData();
    if (!listIn.isEmpty()) {
        scopeData.in = listIn.toArray(new ScopeData.In[listIn.size()]);
    }
    if (!listOut.isEmpty()) {
        scopeData.out = listOut.toArray(new ScopeData.Out[listOut.size()]);
    }
    return scopeData;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) In(jodd.madvoc.meta.In) ArrayList(java.util.ArrayList) Out(jodd.madvoc.meta.Out) InOut(jodd.madvoc.meta.InOut) ScopeData(jodd.madvoc.ScopeData) InOut(jodd.madvoc.meta.InOut) MadvocException(jodd.madvoc.MadvocException)

Example 15 with ClassDescriptor

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

the class AutomagicMadvocConfigurator method onActionClass.

// ---------------------------------------------------------------- handlers
/**
	 * Builds action configuration on founded action class.
	 * Action classes are annotated with {@link jodd.madvoc.meta.MadvocAction} annotation.
	 */
@SuppressWarnings("NonConstantStringShouldBeStringBuffer")
protected void onActionClass(String className) throws ClassNotFoundException {
    Class<?> actionClass = loadClass(className);
    if (actionClass == null) {
        return;
    }
    if (!checkClass(actionClass)) {
        return;
    }
    if (actionClass.getAnnotation(MadvocAction.class) == null) {
        return;
    }
    ClassDescriptor cd = ClassIntrospector.lookup(actionClass);
    MethodDescriptor[] allMethodDescriptors = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethodDescriptors) {
        if (!methodDescriptor.isPublic()) {
            continue;
        }
        // just public methods
        Method method = methodDescriptor.getMethod();
        boolean hasAnnotation = false;
        for (ActionAnnotation<?> actionAnnotation : madvocConfig.getActionAnnotationInstances()) {
            if (actionAnnotation.hasAnnotation(method)) {
                hasAnnotation = true;
                break;
            }
        }
        if (!hasAnnotation) {
            continue;
        }
        actionsManager.register(actionClass, method);
    }
}
Also used : MadvocAction(jodd.madvoc.meta.MadvocAction) ClassDescriptor(jodd.introspector.ClassDescriptor) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor)

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