Search in sources :

Example 16 with PropertyDescriptor

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

the class ValidationContextTest method testCollectPropertyAnnotationChecks.

@Test
public void testCollectPropertyAnnotationChecks() {
    ValidationContext context = spy(new ValidationContext());
    PropertyDescriptor propertyDescriptor = mock(PropertyDescriptor.class);
    List<Check> list = new ArrayList<>();
    context.collectPropertyAnnotationChecks(list, propertyDescriptor);
    verify(context, never()).collectAnnotationChecks(any(List.class), any(Class.class), any(String.class), any(Annotation[].class));
}
Also used : PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 17 with PropertyDescriptor

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

the class MapToBean method map2bean.

/**
	 * Converts map to target type.
	 */
public Object map2bean(Map map, Class targetType) {
    Object target = null;
    // create targets type
    String className = (String) map.get(classMetadataName);
    if (className == null) {
        if (targetType == null) {
            // nothing to do, no information about target type found
            target = map;
        }
    } else {
        try {
            targetType = ClassLoaderUtil.loadClass(className);
        } catch (ClassNotFoundException cnfex) {
            throw new JsonException(cnfex);
        }
    }
    if (target == null) {
        target = jsonParser.newObjectInstance(targetType);
    }
    ClassDescriptor cd = ClassIntrospector.lookup(target.getClass());
    boolean targetIsMap = target instanceof Map;
    for (Object key : map.keySet()) {
        String keyName = key.toString();
        if (classMetadataName != null) {
            if (keyName.equals(classMetadataName)) {
                continue;
            }
        }
        PropertyDescriptor pd = cd.getPropertyDescriptor(keyName, declared);
        if (!targetIsMap && pd == null) {
            // target property does not exist, continue
            continue;
        }
        // value is one of JSON basic types, like Number, Map, List...
        Object value = map.get(key);
        Class propertyType = pd == null ? null : pd.getType();
        Class componentType = pd == null ? null : pd.resolveComponentType(true);
        if (value != null) {
            if (value instanceof List) {
                if (componentType != null && componentType != String.class) {
                    value = generifyList((List) value, componentType);
                }
            } else if (value instanceof Map) {
                // if the value we want to inject is a Map...
                if (!ReflectUtil.isTypeOf(propertyType, Map.class)) {
                    // ... and if target is NOT a map
                    value = map2bean((Map) value, propertyType);
                } else {
                    // target is also a Map, but we might need to generify it
                    Class keyType = pd == null ? null : pd.resolveKeyType(true);
                    if (keyType != String.class || componentType != String.class) {
                        // generify
                        value = generifyMap((Map) value, keyType, componentType);
                    }
                }
            }
        }
        if (targetIsMap) {
            ((Map) target).put(keyName, value);
        } else {
            try {
                setValue(target, pd, value);
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
    return target;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with PropertyDescriptor

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

the class TypeJsonVisitor method visit.

/**
	 * Visits a type.
	 */
public void visit() {
    ClassDescriptor classDescriptor = ClassIntrospector.lookup(type);
    if (classMetadataName != null) {
        // process first 'meta' fields 'class'
        onProperty(classMetadataName, null, false);
    }
    PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        Getter getter = propertyDescriptor.getGetter(declared);
        if (getter != null) {
            String propertyName = propertyDescriptor.getName();
            boolean isTransient = false;
            // check for transient flag
            FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
            if (fieldDescriptor != null) {
                isTransient = Modifier.isTransient(fieldDescriptor.getField().getModifiers());
            }
            onProperty(propertyName, propertyDescriptor, isTransient);
        }
    }
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) Getter(jodd.introspector.Getter) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 19 with PropertyDescriptor

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

the class PetiteBeans method registerPetitePropertyInjectionPoint.

/**
	 * Registers property injection point.
	 *
	 * @param beanName bean name
	 * @param property property name
	 * @param reference explicit injection reference, may be <code>null</code>
	 */
public void registerPetitePropertyInjectionPoint(String beanName, String property, String reference) {
    BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
    String[] references = reference == null ? null : new String[] { reference };
    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);
    }
    PropertyInjectionPoint pip = injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, references);
    beanDefinition.addPropertyInjectionPoint(pip);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor)

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