Class FallbackStringToObjectConverter
- java.lang.Object
-
- org.junit.jupiter.params.converter.FallbackStringToObjectConverter
-
- All Implemented Interfaces:
StringToObjectConverter
class FallbackStringToObjectConverter extends java.lang.Object implements StringToObjectConverter
FallbackStringToObjectConverter
is aStringToObjectConverter
that provides a fallback conversion strategy for converting from aString
to a given target type by invoking a static factory method or factory constructor defined in the target type.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, this converter acts as a no-op.
- Since:
- 5.1
- See Also:
DefaultArgumentConverter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
FallbackStringToObjectConverter.IsFactoryConstructor
Predicate
that determines if theConstructor
supplied toFallbackStringToObjectConverter.IsFactoryConstructor.test(Constructor)
is a non-private factory constructor for the suppliedFallbackStringToObjectConverter.IsFactoryConstructor.targetType
.(package private) static class
FallbackStringToObjectConverter.IsFactoryMethod
Predicate
that determines if theMethod
supplied toFallbackStringToObjectConverter.IsFactoryMethod.test(Method)
is a non-private static factory method for the suppliedFallbackStringToObjectConverter.IsFactoryMethod.targetType
.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,java.util.function.Function<java.lang.String,java.lang.Object>>
factoryExecutableCache
Cache for factory methods and factory constructors.private static java.util.function.Function<java.lang.String,java.lang.Object>
NULL_EXECUTABLE
Implementation of the NULL Object Pattern.
-
Constructor Summary
Constructors Constructor Description FallbackStringToObjectConverter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canConvert(java.lang.Class<?> targetType)
Determine if this converter can convert from aString
to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example,Integer
instead ofint
).java.lang.Object
convert(java.lang.String source, java.lang.Class<?> targetType)
Convert the suppliedString
to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example,Integer
instead ofint
).private static java.lang.reflect.Constructor<?>
findFactoryConstructor(java.lang.Class<?> targetType)
private static java.util.function.Function<java.lang.String,java.lang.Object>
findFactoryExecutable(java.lang.Class<?> targetType)
private static java.lang.reflect.Method
findFactoryMethod(java.lang.Class<?> targetType)
private static boolean
isNotPrivateAndAcceptsSingleStringArgument(java.lang.reflect.Executable executable)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.junit.jupiter.params.converter.StringToObjectConverter
convert
-
-
-
-
Field Detail
-
NULL_EXECUTABLE
private static final java.util.function.Function<java.lang.String,java.lang.Object> NULL_EXECUTABLE
Implementation of the NULL Object Pattern.
-
factoryExecutableCache
private static final java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,java.util.function.Function<java.lang.String,java.lang.Object>> factoryExecutableCache
Cache for factory methods and factory constructors.Searches that do not find a factory method or constructor are tracked by the presence of a
NULL_EXECUTABLE
object stored in the map. This prevents the framework from repeatedly searching for things which are already known not to exist.
-
-
Method Detail
-
canConvert
public boolean canConvert(java.lang.Class<?> targetType)
Description copied from interface:StringToObjectConverter
Determine if this converter can convert from aString
to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example,Integer
instead ofint
).- Specified by:
canConvert
in interfaceStringToObjectConverter
-
convert
public java.lang.Object convert(java.lang.String source, java.lang.Class<?> targetType) throws java.lang.Exception
Description copied from interface:StringToObjectConverter
Convert the suppliedString
to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example,Integer
instead ofint
).- Specified by:
convert
in interfaceStringToObjectConverter
- Throws:
java.lang.Exception
-
findFactoryExecutable
private static java.util.function.Function<java.lang.String,java.lang.Object> findFactoryExecutable(java.lang.Class<?> targetType)
-
findFactoryMethod
private static java.lang.reflect.Method findFactoryMethod(java.lang.Class<?> targetType)
-
findFactoryConstructor
private static java.lang.reflect.Constructor<?> findFactoryConstructor(java.lang.Class<?> targetType)
-
isNotPrivateAndAcceptsSingleStringArgument
private static boolean isNotPrivateAndAcceptsSingleStringArgument(java.lang.reflect.Executable executable)
-
-