net.sf.beanlib.provider.replicator
Class BeanReplicator

java.lang.Object
  extended by net.sf.beanlib.provider.replicator.ReplicatorTemplate
      extended by net.sf.beanlib.provider.replicator.BeanReplicator
All Implemented Interfaces:
BeanReplicatorSpi
Direct Known Subclasses:
Hibernate3JavaBeanReplicator

public class BeanReplicator
extends ReplicatorTemplate
implements BeanReplicatorSpi

Default implementation of BeanReplicatorSpi.

A BeanReplicator can be used to replicate JavaBean's.

Quick Start

To replicate a simple JavaBean with a class definition like:
 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);
 
Notes a no-arg constructor is required for the JavaBean at the top level.

How about a more complex example ? Let's try:

public 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; } }
First, set up the bean
 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);
 
And then replicate it in the same way:
ComplexBean to = new BeanReplicator().replicateBean(from);
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.

Author:
Joe D. Velopar

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
<V,T> T
populate(V from, T to)
          Populates the properties of a "from" JavaBean object to a target "to" JavaBean object.
<V> V
replicateBean(V from)
          Replicates a given JavaBean object.
<V,T> T
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
<V,T> T
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

BeanReplicator

public BeanReplicator(BeanTransformerSpi beanTransformer)
Constructs with a given bean transformer.


BeanReplicator

public 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.

See Also:
ChainedCustomBeanTransformer

BeanReplicator

public BeanReplicator()
Constructs with the default BeanTransformer.

Method Detail

replicateBean

public <V> V replicateBean(V from)
Replicates a given JavaBean object.

Type Parameters:
V - from type
Parameters:
from - from bean to be replicated.

replicateBean

public <V,T> T 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.

Specified by:
replicateBean in interface BeanReplicatorSpi
Type Parameters:
V - from type
T - target type
Parameters:
from - the original bean to be replicated
toClass - target class to be instantiated

replicateBean

protected <V,T> T 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.

Type Parameters:
V - from type
T - target type
Parameters:
from - from bean (after unenhancement) to be replicated
toClass - target class to be instantiated
originalFrom - the original from bean before any "unenhancement"
Returns:
an instance of the replicated bean

populate

public <V,T> T populate(V from,
                        T to)
Populates the properties of a "from" JavaBean object to a target "to" JavaBean object.

Parameters:
from - the bean from which the properties are to be retrieved
to - the target bean to be populated