1010
1111package org .junit .platform .commons .support .conversion ;
1212
13+ import static org .apiguardian .api .API .Status .DEPRECATED ;
1314import static org .apiguardian .api .API .Status .EXPERIMENTAL ;
14- import static org .junit .platform .commons .util .ReflectionUtils .getWrapperType ;
1515
16- import java .util .List ;
17- import java .util .Optional ;
16+ import java .util .ServiceLoader ;
17+ import java .util .stream .Stream ;
18+ import java .util .stream .StreamSupport ;
1819
1920import org .apiguardian .api .API ;
2021import org .jspecify .annotations .Nullable ;
21- import org .junit .platform .commons .util .ClassLoaderUtils ;
2222
2323/**
2424 * {@code ConversionSupport} provides static utility methods for converting a
2929@ API (status = EXPERIMENTAL , since = "1.11" )
3030public final class ConversionSupport {
3131
32- private static final List <StringToObjectConverter > stringToObjectConverters = List .of ( //
33- new StringToBooleanConverter (), //
34- new StringToCharacterConverter (), //
35- new StringToNumberConverter (), //
36- new StringToClassConverter (), //
37- new StringToEnumConverter (), //
38- new StringToJavaTimeConverter (), //
39- new StringToCommonJavaTypesConverter (), //
40- new FallbackStringToObjectConverter () //
41- );
42-
4332 private ConversionSupport () {
4433 /* no-op */
4534 }
@@ -48,43 +37,6 @@ private ConversionSupport() {
4837 * Convert the supplied source {@code String} into an instance of the specified
4938 * target type.
5039 *
51- * <p>If the target type is {@code String}, the source {@code String} will not
52- * be modified.
53- *
54- * <p>Some forms of conversion require a {@link ClassLoader}. If none is
55- * provided, the {@linkplain ClassLoaderUtils#getDefaultClassLoader() default
56- * ClassLoader} will be used.
57- *
58- * <p>This method is able to convert strings into primitive types and their
59- * corresponding wrapper types ({@link Boolean}, {@link Character}, {@link Byte},
60- * {@link Short}, {@link Integer}, {@link Long}, {@link Float}, and
61- * {@link Double}), enum constants, date and time types from the
62- * {@code java.time} package, as well as common Java types such as {@link Class},
63- * {@link java.io.File}, {@link java.nio.file.Path}, {@link java.nio.charset.Charset},
64- * {@link java.math.BigDecimal}, {@link java.math.BigInteger},
65- * {@link java.util.Currency}, {@link java.util.Locale}, {@link java.util.UUID},
66- * {@link java.net.URI}, and {@link java.net.URL}.
67- *
68- * <p>If the target type is not covered by any of the above, a convention-based
69- * conversion strategy will be used to convert the source {@code String} into the
70- * given target type by invoking a static factory method or factory constructor
71- * defined in the target type. The search algorithm used in this strategy is
72- * outlined below.
73- *
74- * <h4>Search Algorithm</h4>
75- *
76- * <ol>
77- * <li>Search for a single, non-private static factory method in the target
78- * type that converts from a String to the target type. Use the factory method
79- * if present.</li>
80- * <li>Search for a single, non-private constructor in the target type that
81- * accepts a String. Use the constructor if present.</li>
82- * </ol>
83- *
84- * <p>If multiple suitable factory methods are discovered they will be ignored.
85- * If neither a single factory method nor a single constructor is found, the
86- * convention-based conversion strategy will not apply.
87- *
8840 * @param source the source {@code String} to convert; may be {@code null}
8941 * but only if the target type is a reference type
9042 * @param targetType the target type the source should be converted into;
@@ -96,49 +48,49 @@ private ConversionSupport() {
9648 * type is a reference type
9749 *
9850 * @since 1.11
51+ * @see DefaultConverter
52+ * @deprecated Use {@link #convert(Object, TypeDescriptor, ClassLoader)} instead.
9953 */
100- @ SuppressWarnings ("unchecked" )
54+ @ Deprecated
55+ @ API (status = DEPRECATED , since = "6.0" )
10156 public static <T > @ Nullable T convert (@ Nullable String source , Class <T > targetType ,
10257 @ Nullable ClassLoader classLoader ) {
103- if (source == null ) {
104- if (targetType .isPrimitive ()) {
105- throw new ConversionException (
106- "Cannot convert null to primitive value of type " + targetType .getTypeName ());
107- }
108- return null ;
109- }
110-
111- if (String .class .equals (targetType )) {
112- return (T ) source ;
113- }
58+ return convert (source , TypeDescriptor .forClass (targetType ), classLoader );
59+ }
11460
115- Class <?> targetTypeToUse = toWrapperType (targetType );
116- Optional <StringToObjectConverter > converter = stringToObjectConverters .stream ().filter (
117- candidate -> candidate .canConvertTo (targetTypeToUse )).findFirst ();
118- if (converter .isPresent ()) {
119- try {
120- ClassLoader classLoaderToUse = classLoader != null ? classLoader
121- : ClassLoaderUtils .getDefaultClassLoader ();
122- return (T ) converter .get ().convert (source , targetTypeToUse , classLoaderToUse );
123- }
124- catch (Exception ex ) {
125- if (ex instanceof ConversionException conversionException ) {
126- // simply rethrow it
127- throw conversionException ;
128- }
129- // else
130- throw new ConversionException (
131- "Failed to convert String \" %s\" to type %s" .formatted (source , targetType .getTypeName ()), ex );
132- }
133- }
61+ /**
62+ * Convert the supplied source object into an instance of the specified
63+ * target type.
64+ *
65+ * @param source the source object to convert; may be {@code null}
66+ * but only if the target type is a reference type
67+ * @param targetType the target type the source should be converted into;
68+ * never {@code null}
69+ * @param classLoader the {@code ClassLoader} to use; may be {@code null} to
70+ * use the default {@code ClassLoader}
71+ * @param <T> the type of the target
72+ * @return the converted object; may be {@code null} but only if the target
73+ * type is a reference type
74+ *
75+ * @since 6.0
76+ */
77+ @ API (status = EXPERIMENTAL , since = "6.0" )
78+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
79+ public static <T > @ Nullable T convert (@ Nullable Object source , TypeDescriptor targetType ,
80+ @ Nullable ClassLoader classLoader ) {
81+ ConversionContext context = new ConversionContext (source , targetType , classLoader );
82+ ServiceLoader <Converter > serviceLoader = ServiceLoader .load (Converter .class , context .classLoader ());
13483
135- throw new ConversionException (
136- "No built-in converter for source type java.lang.String and target type " + targetType .getTypeName ());
137- }
84+ Converter converter = Stream .concat ( //
85+ StreamSupport .stream (serviceLoader .spliterator (), false ), //
86+ Stream .of (DefaultConverter .INSTANCE )) //
87+ .filter (candidate -> candidate .canConvert (context )) //
88+ .findFirst () //
89+ .orElseThrow (() -> new ConversionException (
90+ "No registered or built-in converter for source '%s' and target type %s" .formatted ( //
91+ source , targetType .getTypeName ())));
13892
139- private static Class <?> toWrapperType (Class <?> targetType ) {
140- Class <?> wrapperType = getWrapperType (targetType );
141- return wrapperType != null ? wrapperType : targetType ;
93+ return (T ) converter .convert (source , context );
14294 }
14395
14496}
0 commit comments