@Override public void bind(Object object, Object value) throws XerialException { ReflectionUtil.setFieldValue(object, targetField, value); }
@Override public Object get(Object obj) { return ReflectionUtil.getFieldValue(obj, field); }
@Override public Object get(Object obj, String key) { return ReflectionUtil.invokeGetter(obj, getter, key); }
Class< ? > t = field.getType(); if (TypeInfo.isCollection(t)) { Object collection = getFieldValue(bean, field); if (collection == null) { collection = TypeInfo.createInstance(t); ReflectionUtil.setFieldValue_internal(bean, field, collection); Type elementType = getGenericCollectionElementType(field); Object convertedValue = TypeConverter.convertType(getRawClass(elementType), value); adder.invoke(collection, convertedValue); setFieldValue_internal(bean, field, convertedValue);
@SuppressWarnings("unchecked") public static void setMapEntry(Object bean, Field field, Object key, Object value) throws XerialException { ReflectionUtil.initializeCollectionField(bean, field); Object mapObj = ReflectionUtil.getFieldValue(bean, field); if (mapObj == null) throw new XerialException(XerialErrorCode.INVALID_STATE, "cannot set (key, value) to null Map field: " + bean); Pair<Class< ? >, Class< ? >> mapElementType = ReflectionUtil .getGenericMapElementClasses(field); Class< ? > keyType = mapElementType.getFirst(); Class< ? > valueType = mapElementType.getSecond(); Map map = Map.class.cast(mapObj); map.put(TypeConverter.convertType(keyType, key), TypeConverter .convertType(valueType, value)); }
.getGenericMapElementClasses(eachField); Class< ? > elementType = ReflectionUtil.getRawClass(ReflectionUtil .getGenericCollectionElementType(eachField)); setterContainer.add(ParameterSetter .newSetter(elementType, paramName, eachField));
/** * If the field value is null, initialize the field with a new collection * * @param bean * the target bean containing the field * @param field * the field to initialize. This field must have a collection * type * @throws XerialException * when failed to create a new instance of the field type */ public static void initializeCollectionField(Object bean, Field field) throws XerialException { Class< ? > t = field.getType(); if (!TypeInfo.isCollection(t)) return; // not a collection field Object collection = getFieldValue(bean, field); if (collection == null) { collection = TypeInfo.createInstance(t); ReflectionUtil.setFieldValue_internal(bean, field, collection); } }
public void initialize(Object bean) throws OptionParserException { try { ReflectionUtil.initializeCollectionField(bean, field); } catch (XerialException e) { throw new OptionParserException(e); } }
public static Pair<Class< ? >, Class< ? >> getGenericMapElementClasses(Field field) { Pair<Type, Type> t = getGenericMapElementTypes(field); return new Pair<Class< ? >, Class< ? >>(Class.class.cast(t.getFirst()), Class.class.cast(t .getSecond())); }
/** * Get the generic element type of the field that has a collection type. For * example, for a field <em>f</em> * * <pre> * List<String> * </pre> * * getGenericCollectionElementType(f) returns String.class. * * @param field * @return the generic element type */ public static Type getGenericCollectionElementType(Field field) { if (!TypeInfo.isCollection(field.getType())) throw new XerialError(XerialErrorCode.NOT_A_COLLECTION, field.getType().getName()); Type fieldType = field.getGenericType(); if (hasGenericTypes(fieldType)) { ParameterizedType pt = ParameterizedType.class.cast(fieldType); return pt.getActualTypeArguments()[0]; } else return Object.class; }
public static RelationSetter newMapSetter(String keyNodeName, String valueNodeName, Field mapField) { Pair<Class< ? >, Class< ? >> mapElementType = ReflectionUtil .getGenericMapElementClasses(mapField); return new MapFieldSetter(mapElementType.getFirst(), keyNodeName, mapElementType .getSecond(), valueNodeName, mapField); }
public void setOption(Object bean, Object value) throws XerialException { ReflectionUtil.setFieldValue(bean, field, value); }
@Override public Object get(Object obj) { return ReflectionUtil.getFieldValue(obj, field); }
@Override public Object get(Object obj) { return ReflectionUtil.invokeGetter(obj, getter); }
@Override public void bind(Object obj, Object value) throws XerialException { ReflectionUtil.setFieldValue(obj, targetField, value); }
public Object get(Object object) throws XerialException { return ReflectionUtil.getFieldValue(object, targetField); }
@Override public Object get(Object obj, String key) { if (!returnsMapType()) throw new UnsupportedOperationException("get(Object, String)"); return ReflectionUtil.invokeGetter(obj, getter, key); }
continue; ReflectionUtil.setFieldValue(plugin, f.field, argValue); ReflectionUtil.setFieldValue(plugin, f.field, argValue);
@Override public Object get(Object obj, String key) { Object map = ReflectionUtil.getFieldValue(obj, field); if (map != null && Map.class.isAssignableFrom(map.getClass())) { Map< ? , ? > m = Map.class.cast(map); return m.get(key); } else throw new UnsupportedOperationException("get(Object, String)"); }
@Override public void bind(Object object, Object coreValue, Object attributeValue) throws XerialException { try { Object map = Map.class.cast(mapTypeGetter.get(object)); if (map == null) { map = TypeInfo.createInstance(mapField.getType()); ReflectionUtil.setFieldValue(object, mapField, map); } putter.invoke(map, coreValue, attributeValue); } catch (IllegalArgumentException e) { throw new XerialException(XerialErrorCode.WRONG_DATA_TYPE, e); } catch (IllegalAccessException e) { throw new XerialException(XerialErrorCode.INVALID_INPUT, e); } catch (InvocationTargetException e) { throw new XerialError(XerialErrorCode.WRONG_DATA_TYPE, e); } }