Class ConversionSupport
- java.lang.Object
-
- org.junit.platform.commons.support.conversion.ConversionSupport
-
@API(status=MAINTAINED, since="1.13.3") public final class ConversionSupport extends java.lang.ObjectConversionSupportprovides static utility methods for converting a given object into an instance of a specified type.- Since:
- 1.11
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tconvert(java.lang.String source, java.lang.Class<T> targetType, java.lang.ClassLoader classLoader)Convert the supplied sourceStringinto an instance of the specified target type.
-
-
-
Method Detail
-
convert
public static <T> T convert(java.lang.String source, java.lang.Class<T> targetType, java.lang.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 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 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
-
-