public class PropertySetter extends Object
Utility for setting beans fields and it's inner fields.
Constructor and Description |
---|
PropertySetter() |
Modifier and Type | Method and Description |
---|---|
static Map<String,Object> |
parseObject(Object bean) |
static void |
setNestedProperty(Object obj,
String fieldName,
Object data)
Set the object field specified in fieldName with the given data.
|
public static void setNestedProperty(Object obj, String fieldName, Object data) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException
Set the object field specified in fieldName with the given data.
The name of the property must be giving with the root object implicit in its string, so if a root's property is to be set, the root object itself must be omitted in the field name, for example:
public class MyBean {
private String name;
private MyBean inner;
... // Setters and getters declaration.
}
...
PropertySetter.setNestedProperty( myBean, "name", "some name");
If the property name of the inner bean is to be set, it must be explicit in the property path, for example:
PropertySetter.setNestedProperty( myBean, "inner.name", "some inner name");
The root object passed must be initialized, otherwise an IllegalArgumentException
is thrown.
obj
- The root object where the property must be set.fieldName
- The name of the field to be set.data
- The data to be set in the field given by its name.IllegalAccessException
InvocationTargetException
NoSuchMethodException
- If no property is found with the specified name.InstantiationException
- If a inner bean can't be instantiated.public static Map<String,Object> parseObject(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException
Copyright © 2014. All Rights Reserved.