public class JuffrouBeanWrapper 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 |
---|
JuffrouBeanWrapper(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. |
JuffrouBeanWrapper(BeanWrapperContext context,
JuffrouBeanWrapper parentBeanWrapper,
String parentBeanProperty) |
JuffrouBeanWrapper(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. |
JuffrouBeanWrapper(Class<?> clazz)
Construct a bean wrapper around a class.
|
JuffrouBeanWrapper(Object instance)
Construct a bean wrapper around an existing bean instance.
This constructor will have to create a BeanWrapperContext to get introspection metadata. |
Modifier and Type | Method and Description |
---|---|
void |
addElement(String propertyName,
Object element)
Add an element to a collection property.
|
Object |
getBean()
Get the wrapped bean instance.
|
Object |
getBean(boolean instantiateIfNull)
Get the wrapped bean Get the wrapped bean instance.
|
Class<?> |
getBeanClass()
Get the wrapped bean class
|
Class<?> |
getClazz(String propertyName)
Gets the class of a property in the wrapped bean.
|
BeanWrapperContext |
getContext() |
BeanWrapperFactory |
getFactory() |
Field |
getField(String propertyName)
Gets the field of a property in the wrapped bean.
|
JuffrouBeanWrapper |
getLocalNestedWrapper(String thisProperty)
Obtains the BeanWrapper that corresponds to the bean instance of this
property type.
|
JuffrouBeanWrapper |
getNestedWrapper(String propertyName)
Obtains the JuffrouBeanWrapper corresponding to the bean referred by
propertyName.
|
Map<String,JuffrouBeanWrapper> |
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. |
protected String |
getParentBeanProperty() |
protected JuffrouBeanWrapper |
getParentBeanWrapper() |
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 arguments 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 |
removeElement(String propertyName,
Object element)
Remove an element from a collection property.
|
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 JuffrouBeanWrapper(BeanWrapperContext context)
context
- metadata information about the class to instantiate the
wrapped beanpublic JuffrouBeanWrapper(BeanWrapperContext context, Object instance)
context
- metadata and preferences information about the classinstance
- bean instancepublic JuffrouBeanWrapper(BeanWrapperContext context, JuffrouBeanWrapper parentBeanWrapper, String parentBeanProperty)
public JuffrouBeanWrapper(Object instance)
JuffrouBeanWrapper(BeanWrapperContext, Object)
instead.instance
- object to wrap.public JuffrouBeanWrapper(Class<?> clazz)
JuffrouBeanWrapper(BeanWrapperContext, Object)
instead.clazz
- class to instantiate the wrapped beanpublic BeanWrapperContext getContext()
public BeanWrapperFactory getFactory()
public Object getBean(boolean instantiateIfNull)
instantiateIfNull
- is true, this method will force the instantiation of the bean
in case it is null.public Object getBean()
getBean(boolean)
with TRUE passed as
parameter.getBean(boolean)
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,JuffrouBeanWrapper> getNestedWrappers()
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
- property name to search forpublic String[] getPropertyNames()
public String toString()
toString
in class Object
Object.toString()
public Object getValue(String propertyName)
getValue("pro1.prop2")
will get the value of
prop2 from the nested bean referenced by prop1.propertyName
- property to get the value of.public Class<?> getClazz(String propertyName)
getType("pro1.prop2")
will get the type of prop2
from the nested bean referenced by prop1.propertyName
- the property name to searchpublic Type getType(String propertyName)
getType("pro1.prop2")
will get the type of prop2
from the nested bean referenced by prop1.propertyName
- the property namepublic Type[] getTypeArguments(String propertyName)
getTypeArguments("pro1.prop2")
will get the type arguments of prop2
from the nested bean referenced by prop1.propertyName
- the property namepublic Field getField(String propertyName)
getField("pro1.prop2")
will get the field of prop2
from the nested bean referenced by prop1.propertyName
- the propertypublic 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
- property to set the value ofvalue
- 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
- property to set the value of.value
- value to be setpublic void addElement(String propertyName, Object element)
The collection must be a parameterized collection, for example List<ElementBean>
.
This method will first try to find an add method in the bean that has the collection with the pattern addElementBean(ElementBean arg)
.
If no such method is found then the element will be added directly to the collection field using the Collection.add
method.
propertyName
- name of the collection type property. Can be a nested property path.element
- element to addpublic void removeElement(String propertyName, Object element)
The collection must be a parameterized collection, for example List<ElementBean>
.
This method will first try to find a remove method in the bean that has the collection with the pattern removeElementBean(ElementBean arg)
.
If no such method is found then the element will be removed directly from the collection field using the Collection.remove
method.
propertyName
- name of the collection type property. Can be a nested property path.element
- element to removepublic JuffrouBeanWrapper getNestedWrapper(String propertyName)
getNestedWrapper("prop1.prop2")
will ask the
bean wrapper on prop1 to return the bean wrapper of prop2. If the value
of prop1 was originally null, it would also be set to reference the new
bean holding the value of prop2propertyName
- property name in this bean wrapper or inside a nested bean. It
must be of bean type.to obtain a bean wrapper
corresponding to a non nested property.
public JuffrouBeanWrapper getLocalNestedWrapper(String thisProperty)
thisProperty
- property name in this bean wrapper. The property type must be
another bean and nested properties are not allowed.to obtain nested wrappers of
nested wrappers.
protected JuffrouBeanWrapper getParentBeanWrapper()
protected String getParentBeanProperty()
Copyright © 2011–2020 Juffrou. All rights reserved.