Search in sources :

Example 1 with MethodDescriptor

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

the class BeanVisitor method getAllBeanPropertyNames.

// ---------------------------------------------------------------- util
/**
	 * Returns all bean property names.
	 */
protected String[] getAllBeanPropertyNames(Class type, boolean declared) {
    ClassDescriptor classDescriptor = ClassIntrospector.lookup(type);
    PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors();
    ArrayList<String> names = new ArrayList<>(propertyDescriptors.length);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        MethodDescriptor getter = propertyDescriptor.getReadMethodDescriptor();
        if (getter != null) {
            if (getter.matchDeclared(declared)) {
                names.add(propertyDescriptor.getName());
            }
        } else if (includeFields) {
            FieldDescriptor field = propertyDescriptor.getFieldDescriptor();
            if (field != null) {
                if (field.matchDeclared(declared)) {
                    names.add(field.getName());
                }
            }
        }
    }
    return names.toArray(new String[names.size()]);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 2 with MethodDescriptor

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

the class BeanUtilTest method testIsGetBoolean.

@Test
public void testIsGetBoolean() {
    IsGetBool i = new IsGetBool();
    Object value = BeanUtil.pojo.getProperty(i, "flag");
    assertNotNull(value);
    assertTrue((Boolean) value);
    ClassDescriptor cd = ClassIntrospector.lookup(IsGetBool.class);
    PropertyDescriptor[] propertyDescriptors = cd.getAllPropertyDescriptors();
    assertEquals(1, propertyDescriptors.length);
    assertEquals("flag", propertyDescriptors[0].getName());
    assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName());
    MethodDescriptor[] mds = cd.getAllMethodDescriptors();
    int c = 0;
    for (MethodDescriptor md : mds) {
        if (md.isPublic())
            c++;
    }
    assertEquals(3, c);
    GetIsBool i2 = new GetIsBool();
    value = BeanUtil.pojo.getProperty(i2, "flag");
    assertNotNull(value);
    assertTrue((Boolean) value);
    cd = ClassIntrospector.lookup(GetIsBool.class);
    assertEquals("flag", propertyDescriptors[0].getName());
    assertEquals("isFlag", propertyDescriptors[0].getReadMethodDescriptor().getMethod().getName());
    mds = cd.getAllMethodDescriptors();
    c = 0;
    for (MethodDescriptor md : mds) {
        if (md.isPublic())
            c++;
    }
    assertEquals(3, c);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) MethodDescriptor(jodd.introspector.MethodDescriptor) Test(org.junit.Test)

Example 3 with MethodDescriptor

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

the class ValidationContext method collectPropertyAnnotationChecks.

/**
	 * Process all annotations of provided properties.
	 */
protected void collectPropertyAnnotationChecks(List<Check> annChecks, PropertyDescriptor propertyDescriptor) {
    FieldDescriptor fd = propertyDescriptor.getFieldDescriptor();
    if (fd != null) {
        Annotation[] annotations = fd.getField().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor();
    if (md != null) {
        Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    md = propertyDescriptor.getWriteMethodDescriptor();
    if (md != null) {
        Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
}
Also used : MethodDescriptor(jodd.introspector.MethodDescriptor) Annotation(java.lang.annotation.Annotation) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 4 with MethodDescriptor

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

the class MethodResolver method resolve.

public MethodInjectionPoint[] resolve(Class type) {
    // lookup methods
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<MethodInjectionPoint> list = new ArrayList<>();
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        if (ReflectUtil.isBeanPropertySetter(method)) {
            // ignore setters
            continue;
        }
        if (method.getParameterTypes().length == 0) {
            // ignore methods with no argument
            continue;
        }
        PetiteInject ref = method.getAnnotation(PetiteInject.class);
        if (ref == null) {
            continue;
        }
        String[][] references = PetiteUtil.convertAnnValueToReferences(ref.value());
        list.add(injectionPointFactory.createMethodInjectionPoint(method, references));
    }
    MethodInjectionPoint[] methods;
    if (list.isEmpty()) {
        methods = MethodInjectionPoint.EMPTY;
    } else {
        methods = list.toArray(new MethodInjectionPoint[list.size()]);
    }
    return methods;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) PetiteInject(jodd.petite.meta.PetiteInject) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) MethodInjectionPoint(jodd.petite.MethodInjectionPoint)

Example 5 with MethodDescriptor

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

the class PropertyResolver method resolve.

/**
	 * Resolves all properties for given type.
	 */
public PropertyInjectionPoint[] resolve(Class type, boolean autowire) {
    // lookup fields
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<PropertyInjectionPoint> list = new ArrayList<>();
    PropertyDescriptor[] allPropertyDescriptors = cd.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : allPropertyDescriptors) {
        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;
        }
        String[] refName = null;
        if (ref != null) {
            String name = ref.value().trim();
            if (name.length() != 0) {
                refName = new String[] { name };
            }
        }
        list.add(injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, refName));
    }
    PropertyInjectionPoint[] fields;
    if (list.isEmpty()) {
        fields = PropertyInjectionPoint.EMPTY;
    } else {
        fields = list.toArray(new PropertyInjectionPoint[list.size()]);
    }
    return fields;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) PetiteInject(jodd.petite.meta.PetiteInject) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) PropertyInjectionPoint(jodd.petite.PropertyInjectionPoint)

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