Class ConversionSupport
ConversionSupport provides static utility methods for converting a
given object into an instance of a specified type.- Since:
- 1.11
-
Method Summary
-
Method Details
-
convert
public static <T> @Nullable T convert(@Nullable String source, Class<T> targetType, @Nullable ClassLoader classLoader) Convert the supplied sourceStringinto an instance of the specified target type.If the target type is
String, the sourceStringwill 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.timepackage, 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
Stringinto 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
Stringto 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. - Search for a single, non-private static factory method in the target
type that converts from a
CharSequenceto the target type. Use the factory method if present. - Search for a single, non-private constructor in the target type that
accepts a
CharSequence. Use the constructor if present.
If multiple suitable factory methods or constructors 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 sourceStringto convert; may benullbut only if the target type is a reference typetargetType- the target type the source should be converted into; nevernullclassLoader- theClassLoaderto use; may benullto use the defaultClassLoader- Returns:
- the converted object; may be
nullbut only if the target type is a reference type - Since:
- 1.11
- Search for a single, non-private static factory method in the target
type that converts from a
-