net.sf.beanlib.hibernate
Class HibernateBeanReplicator

java.lang.Object
  extended by net.sf.beanlib.hibernate.HibernateBeanReplicator
All Implemented Interfaces:
BeanPopulatorBaseSpi
Direct Known Subclasses:
Hibernate3BeanReplicator

@NotThreadSafe
public abstract class HibernateBeanReplicator
extends Object
implements BeanPopulatorBaseSpi

Hibernate Bean Replicator.

This class can be used to conveniently replicate Hibernate objects that follow the JavaBean getter/setter convention on a best attempt basis. The replication is typically recursive in that the whole object graph of the input object is replicated into an equivalent output object graph, resolving circular references, and eagerly fetching proxied instances as necessary. However, the exact behavior of the replication process including

can be controlled and/or supplemented by the client code via various options:

  1. All the configurable options of BeanPopulatorBaseSpi are available, as the replication of JavaBean properties inevitably involves bean population.
  2. If the default property filter HibernatePropertyFilter is used,

  3. For anything else that the existing implementation fails to transform, client can provide one or multiple custom transformer factories via initCustomTransformerFactory(net.sf.beanlib.spi.CustomBeanTransformerSpi.Factory...).

This was originally the base class for both the Hibernate 2.x and Hibernate 3.x replicators, but now Hibernate 2 is no longer supported.

Author:
Joe D. Velopar
See Also:
CustomBeanTransformerSpi, Hibernate3BeanReplicator

Field Summary
private  BeanTransformerSpi hibernateBeanTransformer
          Used to do the heavy lifting of Hibernate object transformation and replication.
 
Constructor Summary
protected HibernateBeanReplicator(BeanTransformerSpi hibernateBeanTransformer)
          You probably want to construct a Hibernate3BeanReplicator directly instead of this ?
 
Method Summary
<T> T
copy(Object from, Class<T> toClass)
          Returns an instance of the given class with values copied from the given object.
<T> T
copy(T from)
          Returns a copy of the given object.
<T> T
deepCopy(Object from, Class<T> toClass)
          Convenient method to deep copy the given object to an instance of the given class using the default behavior.
<T> T
deepCopy(T from)
          Convenient method to deep copy the given object using the default behavior.
 BeanPopulationExceptionHandler getBeanPopulationExceptionHandler()
          Returns the exception handler configured for bean population.
 BeanPopulatorBaseConfig getBeanPopulatorBaseConfig()
          Notes if the returned base config is modified, a subsequent BeanPopulator.initBeanPopulatorBaseConfig(BeanPopulatorBaseConfig) needs to be invoked to keep the configuration in sync.
 BeanSourceHandler getBeanSourceHandler()
          Returns the call-back configured that would be invoked after the property value has been retrieved from the source bean, but before being propagated across to the target bean.
 DetailedPropertyFilter getDetailedPropertyFilter()
          Returns the detailed property filter configured.
 HibernatePropertyFilter getHibernatePropertyFilter()
          Convenient method to return the current property filter as HibernatePropertyFilter if the type matches; or null otherwise.
 PropertyFilter getPropertyFilter()
          Returns the property filter that is used to control what properties get propagated across and what get skipped.
 BeanMethodFinder getReaderMethodFinder()
          Returns the finder configured to find the property getter methods of a source JavaBean.
 BeanMethodCollector getSetterMethodCollector()
          Returns the collector configured to collect the property setter methods of a target JavaBean.
 HibernateBeanReplicator initBeanPopulationExceptionHandler(BeanPopulationExceptionHandler beanPopulationExceptionHandler)
          Used to configure a handler to handle any exception thrown.
 BeanPopulatorBaseSpi initBeanPopulatorBaseConfig(BeanPopulatorBaseConfig baseConfig)
          Used to conveniently provide the bean population related configuration options as a single configuration object.
 HibernateBeanReplicator initBeanSourceHandler(BeanSourceHandler beanSourceHandler)
          Used to configure a call-back (to produce whatever side-effects deemed necessary) that is invoked after the property value has been retrieved from the source bean, but before being propagated across to the target bean.
 HibernateBeanReplicator initCustomTransformerFactory(CustomBeanTransformerSpi.Factory... customBeanTransformerFactories)
          Initializes with one or more custom bean transformer factories that will be chained together.
 HibernateBeanReplicator initDebug(boolean debug)
          Used to control whether debug messages should be logged.
 HibernateBeanReplicator initDetailedPropertyFilter(DetailedPropertyFilter detailedPropertyFilter)
          Note this method is only applicable if either the copy(Object) or copy(Object, Class) is directly invoked, and is ignored otherwise (ie ignored if deep or shallow copy is invoked instead).
 HibernateBeanReplicator initPropertyFilter(PropertyFilter propertyFilter)
          Note this method is only applicable if either the copy(Object) or copy(Object, Class) is directly invoked, and is ignored otherwise (ie ignored if deep or shallow copy is invoked instead).
 HibernateBeanReplicator initReaderMethodFinder(BeanMethodFinder readerMethodFinder)
          Used to configure a finder to find the property getter methods of a source JavaBean.
 HibernateBeanReplicator initSetterMethodCollector(BeanMethodCollector setterMethodCollector)
          Used to configure a collector to collect the property setter methods of a target JavaBean.
 boolean isDebug()
          Returns true if debug message logging is on; false otherwise.
<T> T
shallowCopy(Object from, Class<T> toClass)
          Convenient method to shallow copy the given object to an instance of the given class using the default behavior.
<T> T
shallowCopy(T from)
          Convenient method to shallow copy the given object using the default behavior.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hibernateBeanTransformer

private final BeanTransformerSpi hibernateBeanTransformer
Used to do the heavy lifting of Hibernate object transformation and replication.

Constructor Detail

HibernateBeanReplicator

protected HibernateBeanReplicator(BeanTransformerSpi hibernateBeanTransformer)
You probably want to construct a Hibernate3BeanReplicator directly instead of this ?

Method Detail

copy

public final <T> T copy(T from)
Returns a copy of the given object. The exact behavior of the copy depends on how the replicator has been configured via the init* methods.

In the case when none of the init* methods is invoked, this method behaves like deepCopy(Object), but without using the HibernatePropertyFilter.

Type Parameters:
T - type of the given object.
Parameters:
from - given object.

copy

public final <T> T copy(Object from,
                        Class<T> toClass)
Returns an instance of the given class with values copied from the given object. The exact behavior of the copy depends on how the replicator has been configured via the init* methods.

In the case when none of the init* methods is invoked, this method behaves exactly like deepCopy(Object, Class).

Type Parameters:
T - type of the given object.
Parameters:
from - given object.
toClass - target class of the returned object.

deepCopy

public final <T> T deepCopy(T from)
Convenient method to deep copy the given object using the default behavior.

Notes:

Type Parameters:
T - from object type.
Parameters:
from - given object to be copied.
Returns:
a deep clone of the from object.

deepCopy

public final <T> T deepCopy(Object from,
                            Class<T> toClass)
Convenient method to deep copy the given object to an instance of the given class using the default behavior.

Notes:

Type Parameters:
T - to object type.
Parameters:
from - given object to be copied.
toClass - target class of the returned object.
Returns:
an instance of the given class with values deeply copied from the given object.

shallowCopy

public final <T> T shallowCopy(T from)
Convenient method to shallow copy the given object using the default behavior. Shallow copy means skipping those properties that are of type collection, map or under a package that doesn't start with "java.".

Notes:

Type Parameters:
T - from object type.
Parameters:
from - given object to be copied.
Returns:
a shallow clone of the from object.
See Also:
HibernatePropertyFilter

shallowCopy

public final <T> T shallowCopy(Object from,
                               Class<T> toClass)
Convenient method to shallow copy the given object to an instance of the given class using the default behavior. Shallow copy means skipping those properties that are of type collection, map or under a package that doesn't start with "java.".

Notes:

Type Parameters:
T - to object type.
Parameters:
from - given object to be copied.
Returns:
an instance of the given class with values shallow copied from the given object.

initCustomTransformerFactory

public final HibernateBeanReplicator initCustomTransformerFactory(CustomBeanTransformerSpi.Factory... customBeanTransformerFactories)
Initializes with one or more custom bean transformer factories that will be chained together.

See Also:
ChainedCustomBeanTransformer

getPropertyFilter

public final PropertyFilter getPropertyFilter()
Returns the property filter that is used to control what properties get propagated across and what get skipped.

Specified by:
getPropertyFilter in interface BeanPopulatorBaseSpi

initPropertyFilter

public final HibernateBeanReplicator initPropertyFilter(PropertyFilter propertyFilter)
Note this method is only applicable if either the copy(Object) or copy(Object, Class) is directly invoked, and is ignored otherwise (ie ignored if deep or shallow copy is invoked instead).

Specified by:
initPropertyFilter in interface BeanPopulatorBaseSpi
Parameters:
propertyFilter - is similar to DetailedPropertyFilter but with a simpler API that is used to control whether a specific JavaBean property should be propagated from a source bean to a target bean.
Returns:
the current object (ie this) for method chaining purposes.

initDetailedPropertyFilter

public final HibernateBeanReplicator initDetailedPropertyFilter(DetailedPropertyFilter detailedPropertyFilter)
Note this method is only applicable if either the copy(Object) or copy(Object, Class) is directly invoked, and is ignored otherwise (ie ignored if deep or shallow copy is invoked instead).

Specified by:
initDetailedPropertyFilter in interface BeanPopulatorBaseSpi
Parameters:
detailedPropertyFilter - is used to control whether a specific JavaBean property should be propagated from the source bean to the target bean.
Returns:
the current object (ie this) for method chaining purposes.

initBeanSourceHandler

public final HibernateBeanReplicator initBeanSourceHandler(BeanSourceHandler beanSourceHandler)
Used to configure a call-back (to produce whatever side-effects deemed necessary) that is invoked after the property value has been retrieved from the source bean, but before being propagated across to the target bean.

Specified by:
initBeanSourceHandler in interface BeanPopulatorBaseSpi
Parameters:
beanSourceHandler - can be used to act as a call-back (to produce whatever side-effects deemed necessary) after the property value has been retrieved from the source bean, but before being propagated across to the target bean.
Returns:
the current object (ie this) for method chaining purposes.

initDebug

public final HibernateBeanReplicator initDebug(boolean debug)
Used to control whether debug messages should be logged.

Specified by:
initDebug in interface BeanPopulatorBaseSpi
Returns:
the current object (ie this) for method chaining purposes.

initReaderMethodFinder

public final HibernateBeanReplicator initReaderMethodFinder(BeanMethodFinder readerMethodFinder)
Used to configure a finder to find the property getter methods of a source JavaBean.

Specified by:
initReaderMethodFinder in interface BeanPopulatorBaseSpi
Parameters:
readerMethodFinder - can be used to find the property getter methods of a source JavaBean.
Returns:
the current object (ie this) for method chaining purposes.

initSetterMethodCollector

public final HibernateBeanReplicator initSetterMethodCollector(BeanMethodCollector setterMethodCollector)
Used to configure a collector to collect the property setter methods of a target JavaBean.

Specified by:
initSetterMethodCollector in interface BeanPopulatorBaseSpi
Parameters:
setterMethodCollector - can be used to collect the property setter methods of a target JavaBean.
Returns:
the current object (ie this) for method chaining purposes.

initBeanPopulationExceptionHandler

public HibernateBeanReplicator initBeanPopulationExceptionHandler(BeanPopulationExceptionHandler beanPopulationExceptionHandler)
Description copied from interface: BeanPopulatorBaseSpi
Used to configure a handler to handle any exception thrown.

Specified by:
initBeanPopulationExceptionHandler in interface BeanPopulatorBaseSpi
Parameters:
beanPopulationExceptionHandler - can be used to handle any exception thrown.
Returns:
the current object (ie this) for method chaining purposes.

initBeanPopulatorBaseConfig

public BeanPopulatorBaseSpi initBeanPopulatorBaseConfig(BeanPopulatorBaseConfig baseConfig)
Used to conveniently provide the bean population related configuration options as a single configuration object.

Specified by:
initBeanPopulatorBaseConfig in interface BeanPopulatorBaseSpi
Parameters:
baseConfig - is used to conveniently group all the other initializable options into a single unit.
Returns:
the current object (ie this) for method chaining purposes.

getBeanPopulationExceptionHandler

public BeanPopulationExceptionHandler getBeanPopulationExceptionHandler()
Description copied from interface: BeanPopulatorBaseSpi
Returns the exception handler configured for bean population.

Specified by:
getBeanPopulationExceptionHandler in interface BeanPopulatorBaseSpi

getBeanPopulatorBaseConfig

public BeanPopulatorBaseConfig getBeanPopulatorBaseConfig()
Notes if the returned base config is modified, a subsequent BeanPopulator.initBeanPopulatorBaseConfig(BeanPopulatorBaseConfig) needs to be invoked to keep the configuration in sync.

Specified by:
getBeanPopulatorBaseConfig in interface BeanPopulatorBaseSpi

getBeanSourceHandler

public BeanSourceHandler getBeanSourceHandler()
Description copied from interface: BeanPopulatorBaseSpi
Returns the call-back configured that would be invoked after the property value has been retrieved from the source bean, but before being propagated across to the target bean.

Specified by:
getBeanSourceHandler in interface BeanPopulatorBaseSpi

isDebug

public boolean isDebug()
Description copied from interface: BeanPopulatorBaseSpi
Returns true if debug message logging is on; false otherwise.

Specified by:
isDebug in interface BeanPopulatorBaseSpi

getDetailedPropertyFilter

public DetailedPropertyFilter getDetailedPropertyFilter()
Description copied from interface: BeanPopulatorBaseSpi
Returns the detailed property filter configured.

Specified by:
getDetailedPropertyFilter in interface BeanPopulatorBaseSpi

getReaderMethodFinder

public BeanMethodFinder getReaderMethodFinder()
Description copied from interface: BeanPopulatorBaseSpi
Returns the finder configured to find the property getter methods of a source JavaBean.

Specified by:
getReaderMethodFinder in interface BeanPopulatorBaseSpi

getSetterMethodCollector

public BeanMethodCollector getSetterMethodCollector()
Description copied from interface: BeanPopulatorBaseSpi
Returns the collector configured to collect the property setter methods of a target JavaBean.

Specified by:
getSetterMethodCollector in interface BeanPopulatorBaseSpi

getHibernatePropertyFilter

public HibernatePropertyFilter getHibernatePropertyFilter()
Convenient method to return the current property filter as HibernatePropertyFilter if the type matches; or null otherwise.