Search in sources :

Example 6 with MethodDescriptor

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

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

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

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

Example 10 with MethodDescriptor

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

the class JsonAnnotationManager method scanClassForAnnotations.

/**
	 * Scans class for annotations and returns {@link jodd.json.meta.JsonAnnotationManager.TypeData}.
	 */
private TypeData scanClassForAnnotations(Class type) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
    ArrayList<String> includedList = new ArrayList<>();
    ArrayList<String> excludedList = new ArrayList<>();
    ArrayList<String> jsonNames = new ArrayList<>();
    ArrayList<String> realNames = new ArrayList<>();
    JSONAnnotation jsonAnnotation = new JSONAnnotation(JoddJson.jsonAnnotation);
    for (PropertyDescriptor pd : pds) {
        JSONAnnotationData data = null;
        {
            MethodDescriptor md = pd.getReadMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            MethodDescriptor md = pd.getWriteMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            FieldDescriptor fd = pd.getFieldDescriptor();
            if (fd != null) {
                Field field = fd.getField();
                data = jsonAnnotation.readAnnotationData(field);
            }
        }
        if (data != null) {
            // annotation found
            String propertyName = pd.getName();
            String newPropertyName = data.getName();
            if (newPropertyName != null) {
                realNames.add(propertyName);
                jsonNames.add(newPropertyName);
                propertyName = newPropertyName;
            }
            if (data.isIncluded()) {
                includedList.add(propertyName);
            } else {
                excludedList.add(propertyName);
            }
        }
    }
    String[] reals = null;
    if (!realNames.isEmpty()) {
        reals = realNames.toArray(new String[realNames.size()]);
    }
    String[] jsons = null;
    if (!jsonNames.isEmpty()) {
        jsons = jsonNames.toArray(new String[jsonNames.size()]);
    }
    // type
    JSONAnnotationData data = (JSONAnnotationData) jsonAnnotation.readAnnotationData(type);
    return new TypeData(includedList, excludedList, data != null && data.isStrict(), jsons, reals);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) Field(java.lang.reflect.Field)

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