public class ConversionHelper extends Object
A helper class providing functionality related to data type conversion and registration of custom converters.
The conversion of data types - e.g. for properties or method parameters - is an important feature: Because beans to be managed by the dependency injection framework are typically defined in XML builder scripts all property values or method parameters are initially strings. The framework has then to find appropriate methods compatible with the specified parameters. If necessary, a type conversion of the values involved has to be performed.
This class uses Commons BeanUtils for implementing type conversion facilities. The BeanUtils library allows defining custom type converters. Such converters can be registered at an instance of this class to enhance the type conversion capabilities.
Objects of this class can be connected in a hierarchical way: an instance can have a parent. If a required type converter is not found in this instance, the parent's converters are searched. This allows for instance to define base converters on a top-level instance. Child instances can define specialized converters and even override converters of their parents.
Implementation note: This class is thread-safe.
Constructor and Description |
---|
ConversionHelper()
Creates a new instance of
ConversionHelper which does not have a
parent. |
ConversionHelper(ConversionHelper parent)
Creates a new instance of
ConversionHelper and initializes it
with the given parent instance. |
Modifier and Type | Method and Description |
---|---|
<T> T |
convert(Class<T> targetClass,
Object value)
Performs a type conversion.
|
protected <T> T |
convertObject(Class<T> targetClass,
Object value)
Performs a type conversion to the given target class if possible.
|
protected org.apache.commons.beanutils.ConvertUtilsBean |
getConvertBean()
Returns the helper object for type conversions.
|
ConversionHelper |
getParent()
Returns the parent of this instance.
|
void |
registerBaseClassConverter(org.apache.commons.beanutils.Converter converter,
Class<?> targetClass)
Registers a converter for the given base class and all derived classes.
|
void |
registerConverter(org.apache.commons.beanutils.Converter converter,
Class<?> targetClass)
Registers a converter for the specified target class.
|
public ConversionHelper()
ConversionHelper
which does not have a
parent.public ConversionHelper(ConversionHelper parent)
ConversionHelper
and initializes it
with the given parent instance. If the parent is defined, it may be
queried for type converters if this instance cannot resolve specific data
types.parent
- the parent instance (may be null)public ConversionHelper getParent()
public final void registerConverter(org.apache.commons.beanutils.Converter converter, Class<?> targetClass)
convert(Class, Object)
method if a conversion to the
specified target class is needed.converter
- the converter to be registered (must not be null)targetClass
- the target class of the converter (must not be
null)IllegalArgumentException
- if a required parameter is missingpublic final void registerBaseClassConverter(org.apache.commons.beanutils.Converter converter, Class<?> targetClass)
registerConverter(Converter, Class)
method). If this is the
case, this converter is used. Otherwise, it is checked whether a
converter has been registered using this method whose target class is a
super class of the desired target class. If this is the case, this
converter is invoked. With this method converters for whole class
hierarchies can be registered. This can be useful if all members of the
hierarchy require a similar conversion. An example are enumeration
classes. Base class converters are checked in reverse order they have
been registered. So if they form a hierarchy themselves, the least
specific converter should be registered first, followed by more specific
ones. For instance, consider some base class converters dealing with
collection classes. There may be one converter that handles
java.util.List
objects and one for generic
java.util.Collection
objects. In this scenario the collection
converter has to be registered first followed by the specific one for
lists. Otherwise, the generic collection converter will also be used for
lists. This reverse order approach makes it possible for applications to
override default base class converters with their own implementations: if
custom converters are added later, they take precedence over the already
registered converters.converter
- the converter to be registered (must not be null)targetClass
- the target class of the converter (must not be
null)IllegalArgumentException
- if a required parameter is missingpublic <T> T convert(Class<T> targetClass, Object value)
InjectionException
exception is thrown.T
- the type of the target classtargetClass
- the target class (must not be null)value
- the value to be convertedInjectionException
- if no conversion is possibleIllegalArgumentException
- if the target class is nullprotected org.apache.commons.beanutils.ConvertUtilsBean getConvertBean()
ConvertUtilsBean
object responsible for conversionsprotected <T> T convertObject(Class<T> targetClass, Object value)
convert(Class, Object)
if a conversion is
actually required. The arguments have already been checked for
null. This implementation uses the conversion helper object
returned by getConvertBean()
to do the conversion.T
- the type of the target classtargetClass
- the target class of the conversionvalue
- the value to be convertedInjectionException
- if conversion failsCopyright © 2016 The JGUIraffe Team. All rights reserved.