Search in sources :

Example 1 with Getter

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

the class BeanUtilBean method getSimpleProperty.

protected Object getSimpleProperty(BeanProperty bp) {
    if (bp.name.length() == 0) {
        if (bp.indexString != null) {
            // index string exist, but property name is missing
            return bp.bean;
        }
        throw new BeanException("Invalid property", bp);
    }
    Getter getter = bp.getGetter(isDeclared);
    if (getter != null) {
        Object result;
        try {
            result = getter.invokeGetter(bp.bean);
        } catch (Exception ex) {
            if (isSilent) {
                return null;
            }
            throw new BeanException("Getter failed: " + getter, ex);
        }
        if ((result == null) && (isForced)) {
            result = createBeanProperty(bp);
        }
        return result;
    }
    // try: (Map) get("property")
    if (bp.isMap()) {
        Map map = (Map) bp.bean;
        Object key = convertIndexToMapKey(getter, bp.name);
        if (!map.containsKey(key)) {
            if (!isForced) {
                if (isSilent) {
                    return null;
                }
                throw new BeanException("Map key not found: " + bp.name, bp);
            }
            Map value = new HashMap();
            //noinspection unchecked
            map.put(key, value);
            return value;
        }
        return map.get(key);
    }
    // failed
    if (isSilent) {
        return null;
    }
    throw new BeanException("Simple property not found: " + bp.name, bp);
}
Also used : HashMap(java.util.HashMap) Getter(jodd.introspector.Getter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Getter

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

the class BeanUtilBean method _getIndexProperty.

private Object _getIndexProperty(BeanProperty bp) {
    Object resultBean = getSimpleProperty(bp);
    Getter getter = bp.getGetter(isDeclared);
    if (bp.indexString == null) {
        // no index, just simple bean
        return resultBean;
    }
    if (resultBean == null) {
        if (isSilent) {
            return null;
        }
        throw new BeanException("Index property is null: " + bp.name, bp);
    }
    // try: property[index]
    if (resultBean.getClass().isArray()) {
        int index = parseInt(bp.indexString, bp);
        if (isForced) {
            return arrayForcedGet(bp, resultBean, index);
        } else {
            return Array.get(resultBean, index);
        }
    }
    // try: list.get(index)
    if (resultBean instanceof List) {
        int index = parseInt(bp.indexString, bp);
        List list = (List) resultBean;
        if (!isForced) {
            return list.get(index);
        }
        if (!bp.last) {
            ensureListSize(list, index);
        }
        Object value = list.get(index);
        if (value == null) {
            Class listComponentType = extractGenericComponentType(getter);
            if (listComponentType == Object.class) {
                // not an error: when component type is unknown, use Map as generic bean
                listComponentType = Map.class;
            }
            try {
                value = ReflectUtil.newInstance(listComponentType);
            } catch (Exception ex) {
                if (isSilent) {
                    return null;
                }
                throw new BeanException("Invalid list element: " + bp.name + '[' + index + ']', bp, ex);
            }
            //noinspection unchecked
            list.set(index, value);
        }
        return value;
    }
    // try: map.get('index')
    if (resultBean instanceof Map) {
        Map map = (Map) resultBean;
        Object key = convertIndexToMapKey(getter, bp.indexString);
        if (!isForced) {
            return map.get(key);
        }
        Object value = map.get(key);
        if (!bp.last) {
            if (value == null) {
                Class mapComponentType = extractGenericComponentType(getter);
                if (mapComponentType == Object.class) {
                    mapComponentType = Map.class;
                }
                try {
                    value = ReflectUtil.newInstance(mapComponentType);
                } catch (Exception ex) {
                    if (isSilent) {
                        return null;
                    }
                    throw new BeanException("Invalid map element: " + bp.name + '[' + bp.indexString + ']', bp, ex);
                }
                //noinspection unchecked
                map.put(key, value);
            }
        }
        return value;
    }
    // failed
    if (isSilent) {
        return null;
    }
    throw new BeanException("Index property is not an array, list or map: " + bp.name, bp);
}
Also used : Getter(jodd.introspector.Getter) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Getter

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

the class BeanUtilBean method _setIndexProperty.

@SuppressWarnings({ "unchecked" })
private void _setIndexProperty(BeanProperty bp, Object value) {
    if (bp.indexString == null) {
        setSimpleProperty(bp, value);
        return;
    }
    // try: getInner()
    Object nextBean = getSimpleProperty(bp);
    Getter getter = bp.getGetter(isDeclared);
    if (nextBean == null) {
        if (isSilent) {
            return;
        }
        throw new BeanException("Index property is null:" + bp.name, bp);
    }
    // inner bean found
    if (nextBean.getClass().isArray()) {
        int index = parseInt(bp.indexString, bp);
        if (isForced) {
            arrayForcedSet(bp, nextBean, index, value);
        } else {
            Array.set(nextBean, index, value);
        }
        return;
    }
    if (nextBean instanceof List) {
        int index = parseInt(bp.indexString, bp);
        Class listComponentType = extractGenericComponentType(getter);
        if (listComponentType != Object.class) {
            value = convertType(value, listComponentType);
        }
        List list = (List) nextBean;
        if (isForced) {
            ensureListSize(list, index);
        }
        list.set(index, value);
        return;
    }
    if (nextBean instanceof Map) {
        Map map = (Map) nextBean;
        Object key = convertIndexToMapKey(getter, bp.indexString);
        Class mapComponentType = extractGenericComponentType(getter);
        if (mapComponentType != Object.class) {
            value = convertType(value, mapComponentType);
        }
        map.put(key, value);
        return;
    }
    // failed
    if (isSilent) {
        return;
    }
    throw new BeanException("Index property is not an array, list or map: " + bp.name, bp);
}
Also used : Getter(jodd.introspector.Getter) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Getter

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

Aggregations

Getter (jodd.introspector.Getter)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 List (java.util.List)2 ClassDescriptor (jodd.introspector.ClassDescriptor)1 FieldDescriptor (jodd.introspector.FieldDescriptor)1 PropertyDescriptor (jodd.introspector.PropertyDescriptor)1