Top Description Fields Constructors Methods
org.junit.platform.commons.support.conversion

public final Class ConversionSupport

extends Object
Class Inheritance
Annotations
@API
status:MAINTAINED
since:1.13.3
Static Imports
org.apiguardian.api.API.Status.MAINTAINED, org.junit.platform.commons.util.ReflectionUtils.getWrapperType

ConversionSupport provides static utility methods for converting a given object into an instance of a specified type.
Since
1.11

Field Summary

Modifier and TypeField and Description
private static final List<StringToObjectConverter>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the target
T
>
@Nullable T

Returns:

the converted object; may be null but only if the target type is a reference type
convert
(@Nullable String
the source String to convert; may be null but only if the target type is a reference type
source
,
Class<T>
the target type the source should be converted into; never null
targetType
,
@Nullable ClassLoader
the ClassLoader to use; may be null to use the default ClassLoader
classLoader
)

Convert the supplied source String into an instance of the specified target type.

private static Class<?>
toWrapperType(Class<?> targetType)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

stringToObjectConvertersback to summary
private static final List<StringToObjectConverter> stringToObjectConverters

Constructor Detail

ConversionSupportback to summary
private ConversionSupport()

Method Detail

convertback to summary
public static <T> @Nullable T convert(@Nullable String source, Class<T> targetType, @Nullable ClassLoader classLoader)

Convert the supplied source String into an instance of the specified target type.

If the target type is String, the source String 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, and Double), enum constants, date and time types from the java.time package, as well as common Java types such as Class, java.io.File, java.nio.file.Path, java.nio.charset.Charset, java.math.BigDecimal, java.math.BigInteger, java.util.Currency, java.util.Locale, java.util.UUID, java.net.URI, and java.net.URL.

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

  1. 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.
  2. Search for a single, non-private constructor in the target type that accepts a String. Use the constructor if present.
  3. Search for a single, non-private static factory method in the target type that converts from a CharSequence to the target type. Use the factory method if present.
  4. 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.

Parameters
<T>
the type of the target
source:@Nullable String

the source String to convert; may be null but only if the target type is a reference type

targetType:Class<T>

the target type the source should be converted into; never null

classLoader:@Nullable ClassLoader

the ClassLoader to use; may be null to use the default ClassLoader

Returns:@Nullable T

the converted object; may be null but only if the target type is a reference type

Annotations
@SuppressWarnings:unchecked
Since
1.11
toWrapperTypeback to summary
private static Class<?> toWrapperType(Class<?> targetType)