Class ConvertUtilsBean
2.0
ConvertUtilsBean
implementation that delegates convert()
methods to the new convert(Object, Class)
method.
To configure this implementation for the current context ClassLoader invoke BeanUtilsBean.setInstance(new BeanUtilsBean2());
Pre-2.0
Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class. The actual
Converter
instance to be used can be registered for each possible destination Class. Unless you override them, standard Converter
instances
are provided for all of the following destination Classes:
- java.lang.BigDecimal (no default value)
- java.lang.BigInteger (no default value)
- boolean and java.lang.Boolean (default to false)
- byte and java.lang.Byte (default to zero)
- char and java.lang.Character (default to a space)
- java.lang.Class (no default value)
- java.awt.Color (no default value)
- java.awt.Dimension (no default value)
- double and java.lang.Double (default to zero)
- float and java.lang.Float (default to zero)
- int and java.lang.Integer (default to zero)
- long and java.lang.Long (default to zero)
- short and java.lang.Short (default to zero)
- java.lang.String (default to null)
- java.lang.Enum (default to null)
- java.io.File (no default value)
- java.net.InetAddress (no default value)
- java.util.Locale (no default value)
- java.nio.file.Path (no default value)
- java.net.URL (no default value)
- java.net.URI (no default value)
- java.util.UUID (no default value)
- java.sql.Date (no default value)
- java.sql.Time (no default value)
- java.sql.Timestamp (no default value)
- java.time.LocalDate (no default value)
- java.time.LocalDateTime (no default value)
- java.time.LocalTime (no default value)
- java.time.OffsetDateTime (no default value)
- java.time.OffsetTime (no default value)
- java.time.ZonedDateTime (no default value)
- java.time.Duration (no default value)
- java.time.MonthDay (no default value)
- java.util.regex.Pattern (no default value)
- java.time.Period (no default value)
- java.awt.Point (no default value)
- java.time.Year (no default value)
- java.time.YearMonth (no default value)
- java.time.ZoneId (no default value)
- java.time.ZoneOffset (no default value)
For backwards compatibility, the standard Converters for primitive types (and the corresponding wrapper classes) return a defined default value when a
conversion error occurs. If you prefer to have a ConversionException
thrown instead, replace the standard Converter instances with instances created
with the zero-arguments constructor. For example, to cause the Converters for integers to throw an exception on conversion errors, you could do this:
// No-args constructor gets the version that throws exceptions Converter myConverter = new org.apache.commons.beanutils2.converter.IntegerConverter(); ConvertUtils.register(myConverter, Integer.TYPE); // Native type ConvertUtils.register(myConverter, Integer.class); // Wrapper class
Converters generally treat null input as if it were invalid input, that is, they return their default value if one was specified when the converter was constructed, and throw an exception otherwise. If you prefer nulls to be preserved for converters that are converting to objects (not primitives) then register a converter as above, passing a default value of null to the converter constructor (and of course registering that converter only for the .class target).
When a converter is listed above as having no default value, then that converter will throw an exception when passed null or an invalid value as its input. In particular, by default the BigInteger and BigDecimal converters have no default (and are therefore somewhat inconsistent with the other numerical converters which all have zero as their default).
Converters that generate arrays of each of the primitive types are also automatically configured (including String[]). When passed null or invalid input, these return an empty array (not null). See class AbstractArrayConverter for the supported input formats for these converters.
- Since:
- 1.7
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionDelegates to the newconvert(Object, Class)
method.<T> Object
Converts the value to an object of the specified class (if possible).<T> Object
Delegates to the newconvert(Object, Class)
method.<R> Object
Delegates to the newconvert(Object, Class)
method.void
Deregisters all registeredConverter
s, and re-establish the standard Converters.void
deregister
(Class<?> clazz) Remove any registeredConverter
for the specified destinationClass
.protected static ConvertUtilsBean
Gets singleton instance<T> Converter<T>
Looks up and return any registeredConverter
for the specified source and destination class; if there is no registered Converter, returnnull
.<T> Converter<T>
Looks up and return any registeredConverter
for the specified destination class; if there is no registered Converter, returnnull
.void
register
(boolean throwException, boolean defaultNull, int defaultArraySize) Registers the provided converters with the specified defaults.void
Registers a customConverter
for the specified destinationClass
, replacing any previously registered Converter.
-
Constructor Details
-
ConvertUtilsBean
public ConvertUtilsBean()Constructs a bean with standard converters registered
-
-
Method Details
-
getInstance
Gets singleton instance- Returns:
- The singleton instance
-
convert
Delegates to the newconvert(Object, Class)
method.- Parameters:
value
- Value to be converted (may be null)- Returns:
- The converted String value or null if value is null
- See Also:
-
convert
Converts the value to an object of the specified class (if possible). If no converter for the desired target type is registered, the passed in object is returned unchanged.- Type Parameters:
T
- The Class type.- Parameters:
value
- Value to be converted (may be null)targetType
- Class of the value to be converted to (must not be null)- Returns:
- The converted value
- Throws:
ConversionException
- if thrown by an underlying Converter
-
convert
Delegates to the newconvert(Object, Class)
method.- Type Parameters:
R
- the type of the class for the return value.- Parameters:
value
- Value to be converted (may be null)clazz
- Java class to be converted to (must not be null)- Returns:
- The converted value or null if value is null
- See Also:
-
convert
Delegates to the newconvert(Object, Class)
method.- Type Parameters:
T
- the type of the class for the return value.- Parameters:
value
- Array of values to be convertedclazz
- Java array or element class to be converted to (must not be null)- Returns:
- The converted value
- See Also:
-
deregister
Deregisters all registeredConverter
s, and re-establish the standard Converters. -
deregister
Remove any registeredConverter
for the specified destinationClass
.- Parameters:
clazz
- Class for which to remove a registered Converter
-
lookup
Looks up and return any registeredConverter
for the specified source and destination class; if there is no registered Converter, returnnull
.- Type Parameters:
T
- The converter type.- Parameters:
sourceType
- Class of the value being convertedtargetType
- Class of the value to be converted to- Returns:
- The registered
Converter
ornull
if not found
-
lookup
Looks up and return any registeredConverter
for the specified destination class; if there is no registered Converter, returnnull
.- Type Parameters:
T
- The converter type.- Parameters:
clazz
- Class for which to return a registered Converter- Returns:
- The registered
Converter
ornull
if not found
-
register
Registers the provided converters with the specified defaults.- Parameters:
throwException
-true
if the converters should throw an exception when a conversion error occurs, otherwisefalse
if a default value should be used.defaultNull
-true
if the standard converters (seeregisterStandard(boolean, boolean)
) should use a default value ofnull</code>, otherwise <code>false
. N.B. This values is ignored ifthrowException</code> is <code>true
defaultArraySize
- The size of the default array value for array converters (N.B. This values is ignored ifthrowException</code> is <code>true
). Specifying a value less than zero causes anull
value to be used for the default.
-
register
Registers a customConverter
for the specified destinationClass
, replacing any previously registered Converter.- Parameters:
converter
- Converter to be registeredclazz
- Destination class for conversions performed by this Converter
-