Interface CustomBeanTransformerSpi

All Superinterfaces:
All Known Implementing Classes:
ChainedCustomBeanTransformer, JodaTimeTransformer, LazyHibernateCustomBeanTransformer

public interface CustomBeanTransformerSpi
extends Transformable

Custom Bean Transformer SPI.

Used to provide custom transformation that takes precedence over the default transformation.

Quick Start

For example, by default when BeanReplicator is replicating the content of a JavaBean, it would try to instantiate the target class via the no-arg constructor. If the no-arg constructor is not defined (such as in the Point class below), it would cause a NoSuchMethodException to be thrown:
public class Point { private final int x, y; // missing no-arg constructor public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } public class Bean { private Point point; public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } } ... // Initialize a bean Bean from = new Bean(); from.setPoint(new Point(1,2)); // Tries to replicate the bean using the default implementation BeanReplicator replicator = new BeanReplicator(); // Will cause NoSuchMethodException, as Point does not have a no-arg constructor Bean to = replicator.replicateBean(from);
One way to get around this problem is to define a custom transformer and the respective factory:
public class MyBeanTransformer implements CustomBeanTransformerSpi { public boolean isTransformable(Object from, Class toClass, PropertyInfo propertyInfo) { return toClass == Point.class; } public T transform(Object in, Class toClass, PropertyInfo propertyInfo) { Point from = (Point)in; // Note the framework takes care of the issue of object identity, // so we don't need to here. return (T)new Point(from.getX(), from.getY()); } } public class MyBeanTransformerFactory implements CustomBeanTransformerSpi.Factory { public CustomBeanTransformerSpi newCustomBeanTransformer(BeanTransformerSpi beanTransformer) { return new MyBeanTransformer(); } } ... // Initialize a bean Bean from = new Bean(); from.setPoint(new Point(1,2)); // Partially overrides the default transformer behavior BeanTransformerSpi transformer = new BeanTransformer(new MyBeanTransformerFactory()); BeanReplicator replicator = new BeanReplicator(transformer); // Replicates the bean Bean to = replicator.replicateBean(from); // now works!

Customizing HibernateBeanReplicator

Customizing the behavior of HibernateBeanReplicator is basically identical to that of BeanReplicator as decribed above. For example, assuming the same MyBeanTransformerFactory defined above is used,
// Partially overrides the default Hibernate bean transformer's behavior HibernateBeanReplicator replicator = new Hibernate3BeanReplicator(); replicator.initCustomTransformerFactory(new MyBeanTransformerFactory()); replicator.copy(...);

Joe D. Velopar
See Also:

Nested Class Summary
static interface CustomBeanTransformerSpi.Factory
          Custom Bean Transformer Factory SPI.
Method Summary
 boolean isTransformable(Object from, Class<?> toClass, PropertyInfo propertyInfo)
          Returns true if the given object is to be transformed by this transformer; false otherwise.
Methods inherited from interface net.sf.beanlib.spi.Transformable

Method Detail


boolean isTransformable(Object from,
                        Class<?> toClass,
                        PropertyInfo propertyInfo)
Returns true if the given object is to be transformed by this transformer; false otherwise.

from - source object
propertyInfo - If null, it means the in object is a root level object. Otherwise, propertyInfo contains information about the input object as a java bean property value to be transformed.
toClass - target class