|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.beanlib.provider.replicator.ReplicatorTemplate
net.sf.beanlib.provider.replicator.BeanReplicator
public class BeanReplicator
Default implementation of BeanReplicatorSpi
.
A BeanReplicator can be used to replicate JavaBean's.
Notes a no-arg constructor is required for the JavaBean at the top level.public class SimpleBean { private String name; public SimpleBean() {} public SimpleBean(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ... SimpleBean from = new SimpleBean("foo"); SimpleBean to = new BeanReplicator().replicateBean(from);
How about a more complex example ? Let's try:
First, set up the beanpublic class ComplexBean { private String name; private ComplexBean[] array; private Collection<ComplexBean> collection; private Map<String,ComplexBean> map; public ComplexBean() {} public ComplexBean(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Collection<ComplexBean> getCollection() { return collection; } public void setCollection(Collection<ComplexBean> collection) { this.collection = collection; } public ComplexBean[] getArray() { return array; } public void setArray(ComplexBean[] array) { this.array = array; } public Map<String, ComplexBean> getMap() { return map; } public void setMap(Map<String, ComplexBean> map) { this.map = map; } }
And then replicate it in the same way:ComplexBean from = new ComplexBean("foo"); ComplexBean[] a = { from }; Collection<ComplexBean> col = Arrays.asList(a); from.setArray(a); from.setCollection(col); Map<String,ComplexBean> map = new HashMap<String,ComplexBean>(); map.put(from.getName(), from); from.setMap(map);
Voila! The "to" and "from" beans are different object instances, but the content (ie the entire object graph) has been replicated. Note this works as long as these objects follow the JavaBean convention. See BeanReplicatorTest.java for more details.ComplexBean to = new BeanReplicator().replicateBean(from);
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface net.sf.beanlib.spi.replicator.BeanReplicatorSpi |
---|
BeanReplicatorSpi.Factory |
Field Summary |
---|
Fields inherited from class net.sf.beanlib.provider.replicator.ReplicatorTemplate |
---|
log |
Constructor Summary | |
---|---|
BeanReplicator()
Constructs with the default BeanTransformer . |
|
BeanReplicator(BeanTransformerSpi beanTransformer)
Constructs with a given bean transformer. |
|
BeanReplicator(CustomBeanTransformerSpi.Factory... customTransformerFactory)
Convenient constructor that both defaults to use BeanTransformer ,
and allows plugging in one or more custom bean transformer factories
that will be chained together. |
Method Summary | ||
---|---|---|
|
populate(V from,
T to)
Populates the properties of a "from" JavaBean object to a target "to" JavaBean object. |
|
|
replicateBean(V from)
Replicates a given JavaBean object. |
|
|
replicateBean(V from,
Class<T> toClass)
Replicates the properties of a JavaBean object to an instance of a target class, which is selected from the given "from" and "to" classes, giving priority to the one which is more specific whenever possible. |
|
protected
|
replicateBean(V from,
Class<T> toClass,
V originalFrom)
Replicates the properties of a JavaBean object to an instance of a target class, which is selected from the given "from" and "to" classes, giving priority to the one which is more specific whenever possible. |
Methods inherited from class net.sf.beanlib.provider.replicator.ReplicatorTemplate |
---|
chooseClass, containsTargetCloned, createToInstance, createToInstance, createToInstanceWithComparator, getCustomerBeanTransformer, getTargetCloned, newInstanceAsPrivileged, populateBean, putTargetCloned, replicate, replicate, replicateByBeanReplicatable, transform, unenhanceObject |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BeanReplicator(BeanTransformerSpi beanTransformer)
public BeanReplicator(CustomBeanTransformerSpi.Factory... customTransformerFactory)
BeanTransformer
,
and allows plugging in one or more custom bean transformer factories
that will be chained together.
ChainedCustomBeanTransformer
public BeanReplicator()
BeanTransformer
.
Method Detail |
---|
public <V> V replicateBean(V from)
V
- from typefrom
- from bean to be replicated.public <V,T> T replicateBean(V from, Class<T> toClass)
replicateBean
in interface BeanReplicatorSpi
V
- from typeT
- target typefrom
- the original bean to be replicatedtoClass
- target class to be instantiatedprotected <V,T> T replicateBean(V from, Class<T> toClass, V originalFrom)
V
- from typeT
- target typefrom
- from bean (after unenhancement) to be replicatedtoClass
- target class to be instantiatedoriginalFrom
- the original from bean before any "unenhancement"
public <V,T> T populate(V from, T to)
from
- the bean from which the properties are to be retrievedto
- the target bean to be populated
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |