Class ConversionSupport
ConversionSupport
provides static utility methods for converting a
given object into an instance of a specified type.- Since:
- 1.11
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
convert
(String source, Class<T> targetType, ClassLoader classLoader) Convert the supplied sourceString
into an instance of the specified target type.
-
Method Details
-
convert
Convert the supplied sourceString
into an instance of the specified target type.If the target type is
String
, the sourceString
will not be modified.Some forms of conversion require a
ClassLoader
. If none is provided, the default ClassLoader will be used.This method is able to convert strings into primitive types and their corresponding wrapper types (
Boolean
,Character
,Byte
,Short
,Integer
,Long
,Float
, andDouble
), enum constants, date and time types from thejava.time
package, as well as common Java types such asClass
,File
,Path
,Charset
,BigDecimal
,BigInteger
,Currency
,Locale
,UUID
,URI
, andURL
.If the target type is not covered by any of the above, a convention-based conversion strategy will be used to convert the source
String
into the given target type by invoking a static factory method or factory constructor defined in the target type. The search algorithm used in this strategy is outlined below.Search Algorithm
- Search for a single, non-private static factory method in the target type that converts from a String to the target type. Use the factory method if present.
- Search for a single, non-private constructor in the target type that accepts a String. Use the constructor if present.
If multiple suitable factory methods are discovered they will be ignored. If neither a single factory method nor a single constructor is found, the convention-based conversion strategy will not apply.
- Type Parameters:
T
- the type of the target- Parameters:
source
- the sourceString
to convert; may benull
but only if the target type is a reference typetargetType
- the target type the source should be converted into; nevernull
classLoader
- theClassLoader
to use; may benull
to use the defaultClassLoader
- Returns:
- the converted object; may be
null
but only if the target type is a reference type - Since:
- 1.11
-