public class BeanWrapper extends Object
Allows access to the wrapped bean's properties and also to the properties of beans referenced by them. For example
the property path "pro1.prop2" will access the property prop2 from the nested bean referenced by prop1.
This bean wrapper auto grows nested paths, so for each nested bean referenced in this manner, a nested bean wrapper is automatically created. In the previous
example, a bean wrapper would be created for the bean referenced by property prop1. The nested bean wrappers can be
obtained by calling the getNestesWrappers() method.
You can reference nested properties as deep as you like as long as the bean path exists.
| Constructor and Description |
|---|
BeanWrapper(BeanWrapperContext context)
Construct a bean wrapper using the metadata and preferences of an existing BeanWrapperContext.
Performance is better, because there is no need to do introspection. |
BeanWrapper(BeanWrapperContext context,
Object instance)
Construct a bean wrapper using the metadata and preferences of an existing BeanWrapperContext and initializes it with an instance value.
Performance is better, because there is no need to do introspection. |
BeanWrapper(Class<?> clazz)
Deprecated.
|
BeanWrapper(Object instance)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
Object |
getBean()
Get the wrapped bean
|
Class<?> |
getBeanClass()
Get the wrapped bean class
|
Class<?> |
getClazz(String propertyName)
Gets the class of a property in the wrapped bean.
|
BeanWrapperContext |
getContext() |
Field |
getField(String propertyName)
Gets the type of a property in the wrapped bean.
|
BeanWrapper |
getNestedWrapper(String thisProperty)
Obtains the BeanWrapper that corresponds to the bean instance of this property type.
|
Map<String,BeanWrapper> |
getNestedWrappers()
Returns all the nested bean wrappers that have been created inside this bean wrapper.
Nested bean wrappers are created when you access a nested property (i.e. |
String[] |
getPropertyNames()
Get the names of all properties found in this bean and base ascending hierarchy
|
Type |
getType(String propertyName)
Gets the type of a property in the wrapped bean.
|
Type[] |
getTypeArguments(String propertyName)
Gets the type of a property in the wrapped bean.
|
Object |
getValue(String propertyName)
Gets the value of a property in the wrapped bean.
|
boolean |
hasProperty(String propertyName)
Checks whether a property exists in the wrapped bean.
|
void |
reset()
sets all properties to null in this instance and in all nested bean instances
|
void |
setBean(Object bean)
Replaces the wrapped bean with another instance of the same type
|
void |
setValue(String propertyName,
Object value)
Sets the value of a property in the wrapped bean.
|
void |
setValueOfString(String propertyName,
String value)
Same as
setValue(String propertyName, Object value) but the value will be converted from String to
whatever type the property referenced by propertyName is. |
String |
toString()
Returns the string representation of the wrapped bean instance, or and empty string if the instance is null.
|
public BeanWrapper(BeanWrapperContext context)
context - metadata information about the class to instantiate the wrapped beanpublic BeanWrapper(BeanWrapperContext context, Object instance)
context - metadata and preferences information about the classinstance - bean instancepublic BeanWrapper(Object instance)
BeanWrapper(BeanWrapperContext, Object) instead.instance - public BeanWrapper(Class<?> clazz)
BeanWrapper(BeanWrapperContext, Object) instead.clazz - class to instantiate the wrapped beanpublic BeanWrapperContext getContext()
public Object getBean()
public void setBean(Object bean)
bean - instance of the new bean to wrapInvalidParameterException - if the new bean is not of the same type of the initially wrapped bean.public Class<?> getBeanClass()
public Map<String,BeanWrapper> getNestedWrappers()
public void reset()
public boolean hasProperty(String propertyName)
hasProperty("pro1.prop2") returns true only if prop1 exists is this bean and prop2 exists in the bean referenced by prop1.propertyName - public String[] getPropertyNames()
public String toString()
toString in class ObjectObject.toString()public Object getValue(String propertyName)
getValue("pro1.prop2") will get the value of prop2 from the nested bean referenced by
prop1.propertyName - public Class<?> getClazz(String propertyName)
getType("pro1.prop2") will get the type of prop2 from the nested bean referenced by
prop1.propertyName - public Type getType(String propertyName)
getType("pro1.prop2") will get the type of prop2 from the nested bean referenced by
prop1.propertyName - public Type[] getTypeArguments(String propertyName)
getType("pro1.prop2") will get the type of prop2 from the nested bean referenced by
prop1.propertyName - public Field getField(String propertyName)
getType("pro1.prop2") will get the type of prop2 from the nested bean referenced by
prop1.propertyName - public void setValueOfString(String propertyName, String value)
setValue(String propertyName, Object value) but the value will be converted from String to
whatever type the property referenced by propertyName is.propertyName - value - String representation of the value to be setpublic void setValue(String propertyName, Object value)
setValue("pro1.prop2", Boolean.TRUE) will set the value of prop2 from the nested bean
referenced by prop1. If the value of prop1 was originally null, it would also be set to reference the new bean
holding the value of prop2propertyName - value - value to be setpublic BeanWrapper getNestedWrapper(String thisProperty)
thisProperty - property name in this bean wrapper. It must be of bean type.Copyright © 2011-2013. All Rights Reserved.