java.lang.Object | |
↳ | org.apache.commons.beanutils.ConvertUtilsBean |
Known Direct Subclasses |
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:
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.beanutils.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, ie 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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Construct a bean with standard converters registered
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Convert the value to an object of the specified class (if possible). | |||||||||||
Convert the specified value into a String.
| |||||||||||
Convert the specified value to an object of the specified class (if
possible).
| |||||||||||
Convert an array of specified values to an array of objects of the
specified class (if possible).
| |||||||||||
Remove any registered
Converter for the specified destination
Class . | |||||||||||
Remove all registered
Converter s, and re-establish the
standard Converters. | |||||||||||
This method is deprecated.
Register replacement converters for Boolean.TYPE and
Boolean.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Byte.TYPE and
Byte.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Character.TYPE and
Character.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Double.TYPE and
Double.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Float.TYPE and
Float.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Integer.TYPE and
Integer.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Long.TYPE and
Long.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Short.TYPE and
Short.class instead
| |||||||||||
Look up and return any registered
Converter for the specified
destination class; if there is no registered Converter, return
null . | |||||||||||
Look up and return any registered
Converter for the specified
source and destination class; if there is no registered Converter,
return null . | |||||||||||
Register the provided converters with the specified defaults.
| |||||||||||
Register a custom
Converter for the specified destination
Class , replacing any previously registered Converter. | |||||||||||
This method is deprecated.
Register replacement converters for Boolean.TYPE and
Boolean.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Byte.TYPE and
Byte.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Character.TYPE and
Character.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Double.TYPE and
Double.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Float.TYPE and
Float.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Integer.TYPE and
Integer.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Long.TYPE and
Long.class instead
| |||||||||||
This method is deprecated.
Register replacement converters for Short.TYPE and
Short.class instead
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Get singleton instance
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Construct a bean with standard converters registered
Convert the value to an object of the specified class (if possible).
value | Value to be converted (may be null) |
---|---|
targetType | Class of the value to be converted to |
ConversionException | if thrown by an underlying Converter |
---|
Convert the specified value into a String. If the specified value
is an array, the first element (converted to a String) will be
returned. The registered Converter
for the
java.lang.String
class will be used, which allows
applications to customize Object->String conversions (the default
implementation simply uses toString()).
value | Value to be converted (may be null) |
---|
Convert the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.
value | Value to be converted (may be null) |
---|---|
clazz | Java class to be converted to |
ConversionException | if thrown by an underlying Converter |
---|
Convert an array of specified values to an array of objects of the specified class (if possible). If the specified Java class is itself an array class, this class will be the type of the returned value. Otherwise, an array will be constructed whose component type is the specified class.
values | Array of values to be converted |
---|---|
clazz | Java array or element class to be converted to |
ConversionException | if thrown by an underlying Converter |
---|
Remove any registered Converter
for the specified destination
Class
.
clazz | Class for which to remove a registered Converter |
---|
Remove all registered Converter
s, and re-establish the
standard Converters.
This method is deprecated.
Register replacement converters for Boolean.TYPE and
Boolean.class instead
Gets the default value for Boolean conversions.
This method is deprecated.
Register replacement converters for Byte.TYPE and
Byte.class instead
Gets the default value for Byte conversions.
This method is deprecated.
Register replacement converters for Character.TYPE and
Character.class instead
Gets the default value for Character conversions.
This method is deprecated.
Register replacement converters for Double.TYPE and
Double.class instead
Gets the default value for Double conversions.
This method is deprecated.
Register replacement converters for Float.TYPE and
Float.class instead
Gets the default value for Float conversions.
This method is deprecated.
Register replacement converters for Integer.TYPE and
Integer.class instead
Gets the default value for Integer conversions.
This method is deprecated.
Register replacement converters for Long.TYPE and
Long.class instead
Gets the default value for Long conversions.
This method is deprecated.
Register replacement converters for Short.TYPE and
Short.class instead
Gets the default value for Short conversions.
Look up and return any registered Converter
for the specified
source and destination class; if there is no registered Converter,
return null
.
sourceType | Class of the value being converted |
---|---|
targetType | Class of the value to be converted to |
Converter
or null
if not found
Register the provided converters with the specified defaults.
throwException | true if the converters should
throw an exception when a conversion error occurs, otherwise
|
---|---|
defaultNull | true if the standard converters
(see ConvertUtilsBean#registerStandard(boolean, boolean))
should use a default value of null , otherwise false .
N.B. This values is ignored if throwException is true |
defaultArraySize | The size of the default array value for array converters
(N.B. This values is ignored if throwException is true ).
Specifying a value less than zero causes a null |
Register a custom Converter
for the specified destination
Class
, replacing any previously registered Converter.
converter | Converter to be registered |
---|---|
clazz | Destination class for conversions performed by this Converter |
This method is deprecated.
Register replacement converters for Boolean.TYPE and
Boolean.class instead
Sets the default value for Boolean conversions.
newDefaultBoolean | The default Boolean value |
---|
This method is deprecated.
Register replacement converters for Byte.TYPE and
Byte.class instead
Sets the default value for Byte conversions.
newDefaultByte | The default Byte value |
---|
This method is deprecated.
Register replacement converters for Character.TYPE and
Character.class instead
Sets the default value for Character conversions.
newDefaultCharacter | The default Character value |
---|
This method is deprecated.
Register replacement converters for Double.TYPE and
Double.class instead
Sets the default value for Double conversions.
newDefaultDouble | The default Double value |
---|
This method is deprecated.
Register replacement converters for Float.TYPE and
Float.class instead
Sets the default value for Float conversions.
newDefaultFloat | The default Float value |
---|
This method is deprecated.
Register replacement converters for Integer.TYPE and
Integer.class instead
Sets the default value for Integer conversions.
newDefaultInteger | The default Integer value |
---|
This method is deprecated.
Register replacement converters for Long.TYPE and
Long.class instead
Sets the default value for Long conversions.
newDefaultLong | The default Long value |
---|
This method is deprecated.
Register replacement converters for Short.TYPE and
Short.class instead
Sets the default value for Short conversions.
newDefaultShort | The default Short value |
---|
Get singleton instance