|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.morph.transform.transformers.BaseTransformer
public abstract class BaseTransformer
Convenient base class for transformers. This base class offers a number of convenient features, including those listed below.
TransformationException
s for youDecoratedTransformer
interface, while only
requiring subclasses to implement the methods in
Transformer
.ExplicitTransformer.isTransformable(Class, Class)
for better performance. This feature is turned on by default
Field Summary | |
---|---|
protected java.lang.Class[] |
destinationClasses
Destination classes |
protected org.apache.commons.logging.Log |
log
BaseTransformer log object |
protected java.lang.Class[] |
sourceClasses
Source classes |
Constructor Summary | |
---|---|
protected |
BaseTransformer()
Create a new BaseTransformer. |
Method Summary | |
---|---|
protected java.lang.Object |
clone()
|
java.lang.Object |
convert(java.lang.Class destinationClass,
java.lang.Object source)
DecoratedConverter.convert(Class, Object) |
java.lang.Object |
convert(java.lang.Class destinationClass,
java.lang.Object source,
java.util.Locale locale)
|
protected java.lang.Object |
convertImpl(java.lang.Class destinationClass,
java.lang.Object source,
java.util.Locale locale)
The implementation of the convert method, which may omit
the invalid argument checks already performed by this base class. |
void |
copy(java.lang.Object destination,
java.lang.Object source)
DecoratedCopier.copy(Object, Object) |
void |
copy(java.lang.Object destination,
java.lang.Object source,
java.util.Locale locale)
Copier.copy(Object, Object, Locale) |
protected void |
copyImpl(java.lang.Object destination,
java.lang.Object source,
java.util.Locale locale,
java.lang.Integer preferredTransformationType)
Implementation of the copy method. |
protected Reflector |
createDefaultReflector()
Create the default reflector instance to be used by this Transformer. |
java.lang.Object |
createNewInstance(java.lang.Class destinationClass,
java.lang.Object source)
NodeCopier.createNewInstance(Class, Object) |
protected java.lang.Object |
createNewInstanceImpl(java.lang.Class destinationClass,
java.lang.Object source)
Implement NodeCopier.createNewInstance(Class, Object) |
protected java.lang.Object |
createReusableSource(java.lang.Class destinationClass,
java.lang.Object source)
NodeCopier.createReusableSource(Class, Object) |
boolean |
equals(java.lang.Object object1,
java.lang.Object object2)
Locale-independent test between two objects for equality. |
boolean |
equals(java.lang.Object object1,
java.lang.Object object2,
java.util.Locale locale)
Test objects for equality. |
protected boolean |
equalsUnidirectionalTest(java.lang.Object cannotBeNull,
java.lang.Object canBeNull,
java.util.Locale locale)
Helper method for equality testing. |
java.lang.Class[] |
getDestinationClasses()
Defines the types of objects that can be used as information sources by this transformer. |
protected abstract java.lang.Class[] |
getDestinationClassesImpl()
Transformer.getDestinationClasses() implementation template method. |
protected InstantiatingReflector |
getInstantiatingReflector()
Get the InstantiatingReflector employed by this Transformer. |
protected java.util.Locale |
getLocale()
Retrieves the current Locale if none is specified in the method arguments for a converter or copier. |
protected org.apache.commons.logging.Log |
getLog()
Get the commons-logging Log in use. |
protected Transformer |
getNestedTransformer()
NodeCopier.getNestedTransformer() |
Reflector |
getReflector()
Get the Reflector employed by this Transformer. |
protected Reflector |
getReflector(java.lang.Class reflectorType)
Get the Reflector of the specified type employed by this Transformer. |
java.lang.Class[] |
getSourceClasses()
Defines the types of objects that can be used as information sources by this transformer. |
protected abstract java.lang.Class[] |
getSourceClassesImpl()
Transformer.getSourceClasses() implementation template method. |
protected java.util.Map |
getTransformableCallCache()
Get the cache of calls to isTransformable(Class, Class) |
java.lang.String |
getTransformerName()
Get the transformerName. |
protected void |
initialize()
Initialize this Transformer |
protected void |
initializeImpl()
Gives subclasses a chance to perform any computations needed to initialize the transformer. |
protected boolean |
isAutomaticallyHandlingNulls()
Indicates whether null values will automatically be
converted to null by this base class before even calling
the subclass's convertImpl(Class, Object, Locale) method. |
boolean |
isCachingIsTransformableCalls()
Learn whether this Transformer is caching calls to isTransformable(Class, Class) |
boolean |
isImpreciseTransformation(java.lang.Class destinationClass,
java.lang.Class sourceClass)
Learn whether the specified transformation yields an imprecise result. |
protected boolean |
isImpreciseTransformationImpl(java.lang.Class destinationClass,
java.lang.Class sourceClass)
Implementation of isImpreciseTransformation |
protected boolean |
isInitialized()
Learn whether this Transformer has been initialized. |
protected boolean |
isPerformingLogging()
Indicates if calls to the main transformation methods (convert, copy) will cause a log message to be recorded |
boolean |
isTransformable(java.lang.Class destinationType,
java.lang.Class sourceType)
Specifies which source classes are transformable to which destination classes. |
protected boolean |
isTransformableImpl(java.lang.Class destinationType,
java.lang.Class sourceType)
Default implementation for Transformer#isTransformable(Class, Class) that assumes that each
source type can be converted into each destination type. |
protected boolean |
isWrappingRuntimeExceptions()
Indicates whether runtime exceptions should be wrapped as TransformationException s. |
void |
setCachingIsTransformableCalls(boolean cachingIsTransformableCalls)
Set whether this Transformer is caching calls to isTransformable(Class, Class) |
protected void |
setDestinationClasses(java.lang.Class[] destinationClasses)
Configures the destinationClasses property of this
transformer. |
protected void |
setInitialized(boolean initialized)
Set whether this Transformer has been initialized. |
protected void |
setLog(org.apache.commons.logging.Log log)
Set the commons-logging Log for this Transformer. |
protected void |
setNestedTransformer(Transformer nestedTransformer)
NodeCopier.setNestedTransformer(Transformer) |
void |
setReflector(Reflector reflector)
Set the Reflector to be used by this Transformer. |
protected void |
setSourceClasses(java.lang.Class[] sourceClasses)
Configures the sourceClasses property of this transformer. |
protected void |
setTransformableCallCache(java.util.Map transformableCallCache)
Get the cache of calls to isTransformable(Class, Class) |
void |
setTransformerName(java.lang.String transformerName)
Set the transformerName. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected transient org.apache.commons.logging.Log log
protected java.lang.Class[] sourceClasses
protected java.lang.Class[] destinationClasses
Constructor Detail |
---|
protected BaseTransformer()
Method Detail |
---|
protected boolean isTransformableImpl(java.lang.Class destinationType, java.lang.Class sourceType) throws java.lang.Exception
Transformer#isTransformable(Class, Class)
that assumes that each
source type can be converted into each destination type.
destinationType
- the destination type to testsourceType
- the source type to test
TransformationException
- if it could not be determined if sourceType
is transformable into destinationType
java.lang.Exception
public final boolean isTransformable(java.lang.Class destinationType, java.lang.Class sourceType) throws TransformationException
isTransformable
in interface ExplicitTransformer
destinationType
- the destination type to testsourceType
- the source type to test
TransformationException
- if it could not be determined if sourceType
is transformable into destinationType
ExplicitTransformer.isTransformable(java.lang.Class, java.lang.Class)
protected abstract java.lang.Class[] getSourceClassesImpl() throws java.lang.Exception
Transformer.getSourceClasses()
implementation template method.
java.lang.Exception
protected abstract java.lang.Class[] getDestinationClassesImpl() throws java.lang.Exception
Transformer.getDestinationClasses()
implementation template method.
java.lang.Exception
public final java.lang.Class[] getSourceClasses() throws TransformationException
getSourceClasses
in interface Transformer
TransformationException
Transformer.getSourceClasses()
public final java.lang.Class[] getDestinationClasses() throws TransformationException
getDestinationClasses
in interface Transformer
TransformationException
Transformer.getDestinationClasses()
protected void setSourceClasses(java.lang.Class[] sourceClasses)
sourceClasses
property of this transformer.
Note that this method should be called before the transformer is used.
Otherwise, if another thread is in the middle of transforming an object
graph and this method is called, the behavior of the transformer can
change partway through the transformation.
sourceClasses
- the new sourceClasses
for this transformerprotected void setDestinationClasses(java.lang.Class[] destinationClasses)
destinationClasses
property of this
transformer. Note that this method should be called before the
transformer is used. Otherwise, if another thread is in the middle of
transforming an object graph and this method is called, the behavior of
the transformer can change partway through the transformation.
destinationClasses
- the new destinationClasses
for this transformerprotected void initializeImpl() throws java.lang.Exception
java.lang.Exception
protected final void initialize() throws TransformationException
TransformationException
protected java.util.Locale getLocale()
org.springframework.context.i18n.LocaleContextHolder
, if Spring
is on the classpath. Otherwise, returns the default Locale by calling
Locale.getDefault()
.
public final java.lang.Object convert(java.lang.Class destinationClass, java.lang.Object source) throws TransformationException
DecoratedConverter.convert(Class, Object)
destinationClass
- source
-
TransformationException
public final java.lang.Object convert(java.lang.Class destinationClass, java.lang.Object source, java.util.Locale locale)
destinationClass
- source
- locale
-
protected java.lang.Object convertImpl(java.lang.Class destinationClass, java.lang.Object source, java.util.Locale locale) throws java.lang.Exception
convert
method, which may omit
the invalid argument checks already performed by this base class. By
default, this method creates a new instance of the destinationClass and
copies information from the source to the destination. This
implementation should be fine as-is for Copiers, but Converters will need
to implement this method since they will not be implementing the copy
method.
locale
- the locale in which the conversion should take place. for
converters that are not locale-aware, the local argument can
simply be ignored
java.lang.Exception
protected java.lang.Object createReusableSource(java.lang.Class destinationClass, java.lang.Object source)
NodeCopier.createReusableSource(Class, Object)
destinationClass
- source
-
public boolean equals(java.lang.Object object1, java.lang.Object object2, java.util.Locale locale)
object1
- object2
- locale
-
true
if object1
is considered equal to object2
.public final boolean equals(java.lang.Object object1, java.lang.Object object2) throws TransformationException
object1
- object2
-
true
if object1
is considered equal to object2
.
TransformationException
protected boolean equalsUnidirectionalTest(java.lang.Object cannotBeNull, java.lang.Object canBeNull, java.util.Locale locale)
cannotBeNull
- canBeNull
- locale
-
true
if object1
is considered equal to object2
.public final void copy(java.lang.Object destination, java.lang.Object source) throws TransformationException
DecoratedCopier.copy(Object, Object)
destination
- source
-
TransformationException
public final void copy(java.lang.Object destination, java.lang.Object source, java.util.Locale locale) throws TransformationException
Copier.copy(Object, Object, Locale)
destination
- source
- locale
-
TransformationException
protected void copyImpl(java.lang.Object destination, java.lang.Object source, java.util.Locale locale, java.lang.Integer preferredTransformationType) throws java.lang.Exception
java.lang.Exception
protected java.lang.Object createNewInstanceImpl(java.lang.Class destinationClass, java.lang.Object source) throws java.lang.Exception
NodeCopier.createNewInstance(Class, Object)
destinationClass
- source
-
java.lang.Exception
public java.lang.Object createNewInstance(java.lang.Class destinationClass, java.lang.Object source)
NodeCopier.createNewInstance(Class, Object)
destinationClass
- source
-
protected boolean isImpreciseTransformationImpl(java.lang.Class destinationClass, java.lang.Class sourceClass)
destinationClass
- sourceClass
-
public final boolean isImpreciseTransformation(java.lang.Class destinationClass, java.lang.Class sourceClass)
destinationClass
- sourceClass
-
protected boolean isPerformingLogging()
protected boolean isAutomaticallyHandlingNulls()
null
values will automatically be
converted to null
by this base class before even calling
the subclass's convertImpl(Class, Object, Locale)
method.
Subclasses which depend on this behavior (which is all subclasses, by
default) should include null
as one of their source and
destination classes so that the actual behavior of the transformer is
consistent with the values that are returned by the
isTransformable(Class, Class)
method. The conversions will
happen automatically even if the source and destination classes don't
contain null
, but for the sake of consistency the
null
s should be included.
null
values will automatically be
converted to null
by this base class before even
calling the subclass's
convertImpl(Class, Object, Locale)
methodprotected boolean isWrappingRuntimeExceptions()
TransformationException
s. By default, this method returns
true
.
Simple transformers in Morph that operate on JDK types like Numbers and
Strings will usually set this value to true
so that they
throw TransformationExceptions if problems occur. More complex
transformers that operate on graphs of objects are encouraged to set this
value to false
so that runtime exceptions are not wrapped.
This way, problems accessing data will be expressed by the native API of
a user's domain objects and avoid the need to catch Morph-specific
exceptions (assuming the use of runtime exceptions in said domain
objects).
true
protected Transformer getNestedTransformer()
NodeCopier.getNestedTransformer()
protected void setNestedTransformer(Transformer nestedTransformer)
NodeCopier.setNestedTransformer(Transformer)
nestedTransformer
- protected boolean isInitialized()
protected void setInitialized(boolean initialized)
initialized
- public boolean isCachingIsTransformableCalls()
isTransformable(Class, Class)
public void setCachingIsTransformableCalls(boolean cachingIsTransformableCalls)
isTransformable(Class, Class)
cachingIsTransformableCalls
- protected java.util.Map getTransformableCallCache()
isTransformable(Class, Class)
protected void setTransformableCallCache(java.util.Map transformableCallCache)
isTransformable(Class, Class)
transformableCallCache
- Mapprotected org.apache.commons.logging.Log getLog()
protected void setLog(org.apache.commons.logging.Log log)
log
- protected InstantiatingReflector getInstantiatingReflector()
protected Reflector getReflector(java.lang.Class reflectorType)
reflectorType
-
reflectorType
public Reflector getReflector()
public void setReflector(Reflector reflector)
reflector
- protected Reflector createDefaultReflector()
protected java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
Object.clone()
public java.lang.String getTransformerName()
public void setTransformerName(java.lang.String transformerName)
transformerName
- the String to setpublic java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |