Search in sources :

Example 6 with PropertyDescriptor

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

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

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

the class AnnotatedPropertyInterceptor method intercept.

public Object intercept(ActionRequest actionRequest) throws Exception {
    Object action = actionRequest.getAction();
    Class actionType = action.getClass();
    PropertyDescriptor[] allProperties = lookupAnnotatedProperties(actionType);
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        onAnnotatedProperty(actionRequest, propertyDescriptor);
    }
    return actionRequest.invoke();
}
Also used : PropertyDescriptor(jodd.introspector.PropertyDescriptor)

Example 9 with PropertyDescriptor

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

the class AnnotatedPropertyInterceptor method lookupAnnotatedProperties.

/**
	 * Lookups for annotated properties. Caches all annotated properties on the first
	 * action class scan. 
	 */
protected PropertyDescriptor[] lookupAnnotatedProperties(Class type) {
    PropertyDescriptor[] properties = annotatedProperties.get(type);
    if (properties != null) {
        return properties;
    }
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    List<PropertyDescriptor> list = new ArrayList<>();
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        Annotation ann = null;
        if (propertyDescriptor.getFieldDescriptor() != null) {
            ann = propertyDescriptor.getFieldDescriptor().getField().getAnnotation(annotations);
        }
        if (ann == null && propertyDescriptor.getWriteMethodDescriptor() != null) {
            ann = propertyDescriptor.getWriteMethodDescriptor().getMethod().getAnnotation(annotations);
        }
        if (ann == null && propertyDescriptor.getReadMethodDescriptor() != null) {
            ann = propertyDescriptor.getReadMethodDescriptor().getMethod().getAnnotation(annotations);
        }
        if (ann != null) {
            list.add(propertyDescriptor);
        }
    }
    if (list.isEmpty()) {
        properties = EMPTY;
    } else {
        properties = list.toArray(new PropertyDescriptor[list.size()]);
    }
    annotatedProperties.put(type, properties);
    return properties;
}
Also used : PropertyDescriptor(jodd.introspector.PropertyDescriptor) ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation)

Example 10 with PropertyDescriptor

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

PropertyDescriptor (jodd.introspector.PropertyDescriptor)19 ClassDescriptor (jodd.introspector.ClassDescriptor)16 ArrayList (java.util.ArrayList)8 FieldDescriptor (jodd.introspector.FieldDescriptor)5 MethodDescriptor (jodd.introspector.MethodDescriptor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 LifeBean (jodd.bean.data.LifeBean)2 CachingIntrospector (jodd.introspector.CachingIntrospector)2 PetiteInject (jodd.petite.meta.PetiteInject)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Woof (jodd.bean.data.Woof)1 Getter (jodd.introspector.Getter)1