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

pack-priv Class FallbackStringToObjectConverter

extends Object
implements StringToObjectConverter
Class Inheritance
All Implemented Interfaces
org.junit.platform.commons.support.conversion.StringToObjectConverter
Static Imports
org.junit.platform.commons.support.HierarchyTraversalMode.BOTTOM_UP, .ModifierSupport.isNotPrivate, .ModifierSupport.isNotStatic, .ReflectionSupport.findMethods, .ReflectionSupport.invokeMethod, org.junit.platform.commons.util.ReflectionUtils.findConstructors, .ReflectionUtils.newInstance

FallbackStringToObjectConverter is a StringToObjectConverter that provides a fallback conversion strategy for converting from a String to a given target type by invoking a static factory method or factory constructor defined in the target type.

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, this converter acts as a no-op.

Since
1.11
See Also
ConversionSupport

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static record
FallbackStringToObjectConverter.IsFactoryConstructor

Predicate that determines if the Constructor supplied to test(Constructor) is a non-private factory constructor for the supplied targetType and parameterType.

pack-priv static record
FallbackStringToObjectConverter.IsFactoryMethod

Predicate that determines if the Method supplied to test(Method) is a non-private static factory method for the supplied targetType and parameterType.

Field Summary

Modifier and TypeField and Description
private static final ConcurrentHashMap<Class<?>, Function<String, @Nullable Object>>
factoryExecutableCache

Cache for factory methods and factory constructors.

private static final Function<String, Object>
NULL_EXECUTABLE

Implementation of the NULL Object Pattern.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
canConvertTo(Class<?> targetType)

Implements org.junit.platform.commons.support.conversion.StringToObjectConverter.canConvertTo.

Determine if this converter can convert from a String to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example, Integer instead of int).

public @Nullable Object
convert(String source, Class<?> targetType)

Implements org.junit.platform.commons.support.conversion.StringToObjectConverter.convert.

Convert the supplied String to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example, Integer instead of int).

private static @Nullable Constructor<?>
findFactoryConstructor(Class<?> targetType, Class<?> parameterType)

private static Function<String, @Nullable Object>
private static @Nullable Function<String, @Nullable Object>
findFactoryExecutable(Class<?> targetType, Class<?> parameterType)

private static @Nullable Method
findFactoryMethod(Class<?> targetType, Class<?> parameterType)

private static boolean
isFactoryCandidate(Executable executable, Class<?> parameterType)

Determine if the supplied Executable is not private and accepts a single argument of the supplied parameter type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

factoryExecutableCacheback to summary
private static final ConcurrentHashMap<Class<?>, Function<String, @Nullable 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.

NULL_EXECUTABLEback to summary
private static final Function<String, Object> NULL_EXECUTABLE

Implementation of the NULL Object Pattern.

Constructor Detail

FallbackStringToObjectConverterback to summary
pack-priv FallbackStringToObjectConverter()

Method Detail

canConvertToback to summary
public boolean canConvertTo(Class<?> targetType)

Implements org.junit.platform.commons.support.conversion.StringToObjectConverter.canConvertTo.

Doc from org.junit.platform.commons.support.conversion.StringToObjectConverter.canConvertTo.

Determine if this converter can convert from a String to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example, Integer instead of int).

Annotations
@Override
convertback to summary
public @Nullable Object convert(String source, Class<?> targetType) throws Exception

Implements org.junit.platform.commons.support.conversion.StringToObjectConverter.convert.

Doc from org.junit.platform.commons.support.conversion.StringToObjectConverter.convert.

Convert the supplied String to the supplied target type (which is guaranteed to be a wrapper type for primitives — for example, Integer instead of int).

This method will only be invoked if canConvertTo(Class) returns true for the same target type.

Annotations
@Override
findFactoryConstructorback to summary
private static @Nullable Constructor<?> findFactoryConstructor(Class<?> targetType, Class<?> parameterType)
findFactoryExecutableback to summary
private static Function<String, @Nullable Object> findFactoryExecutable(Class<?> targetType)
findFactoryExecutableback to summary
private static @Nullable Function<String, @Nullable Object> findFactoryExecutable(Class<?> targetType, Class<?> parameterType)
findFactoryMethodback to summary
private static @Nullable Method findFactoryMethod(Class<?> targetType, Class<?> parameterType)
isFactoryCandidateback to summary
private static boolean isFactoryCandidate(Executable executable, Class<?> parameterType)

Determine if the supplied Executable is not private and accepts a single argument of the supplied parameter type.

org.junit.platform.commons.support.conversion back to summary

pack-priv final Record FallbackStringToObjectConverter.IsFactoryConstructor

extends Record
implements Predicate<Constructor<?>>
Class Inheritance
All Implemented Interfaces
java.util.function.Predicate
Record Components
targetType:Class<?> 
parameterType:Class<?> 

Predicate that determines if the Constructor supplied to test(Constructor) is a non-private factory constructor for the supplied targetType and parameterType.

Field Summary

Modifier and TypeField and Description
private final Class<?>
parameterType

Record Component accessed by parameterType().

private final Class<?>
targetType

Record Component accessed by targetType().

Constructor Summary

AccessConstructor and Description
pack-priv
IsFactoryConstructor(Class<?> targetType, Class<?> parameterType)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Class<?>
parameterType()

Record Component getter of parameterType.

public Class<?>
targetType()

Record Component getter of targetType.

public boolean
test(Constructor<?> constructor)

Implements java.util.function.Predicate.test.

Evaluates this predicate on the given argument.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

parameterTypeback to summary
private final Class<?> parameterType

Record Component accessed by parameterType().

targetTypeback to summary
private final Class<?> targetType

Record Component accessed by targetType().

Constructor Detail

IsFactoryConstructorback to summary
pack-priv IsFactoryConstructor(Class<?> targetType, Class<?> parameterType)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

parameterTypeback to summary
public Class<?> parameterType()

Record Component getter of parameterType.

targetTypeback to summary
public Class<?> targetType()

Record Component getter of targetType.

testback to summary
public boolean test(Constructor<?> constructor)

Implements java.util.function.Predicate.test.

Doc from java.util.function.Predicate.test.

Evaluates this predicate on the given argument.

Annotations
@Override
toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

org.junit.platform.commons.support.conversion back to summary

pack-priv final Record FallbackStringToObjectConverter.IsFactoryMethod

extends Record
implements Predicate<Method>
Class Inheritance
All Implemented Interfaces
java.util.function.Predicate
Record Components
targetType:Class<?> 
parameterType:Class<?> 

Predicate that determines if the Method supplied to test(Method) is a non-private static factory method for the supplied targetType and parameterType.

Field Summary

Modifier and TypeField and Description
private final Class<?>
parameterType

Record Component accessed by parameterType().

private final Class<?>
targetType

Record Component accessed by targetType().

Constructor Summary

AccessConstructor and Description
pack-priv
IsFactoryMethod(Class<?> targetType, Class<?> parameterType)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Class<?>
parameterType()

Record Component getter of parameterType.

public Class<?>
targetType()

Record Component getter of targetType.

public boolean
test(Method method)

Implements java.util.function.Predicate.test.

Evaluates this predicate on the given argument.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

parameterTypeback to summary
private final Class<?> parameterType

Record Component accessed by parameterType().

targetTypeback to summary
private final Class<?> targetType

Record Component accessed by targetType().

Constructor Detail

IsFactoryMethodback to summary
pack-priv IsFactoryMethod(Class<?> targetType, Class<?> parameterType)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

parameterTypeback to summary
public Class<?> parameterType()

Record Component getter of parameterType.

targetTypeback to summary
public Class<?> targetType()

Record Component getter of targetType.

testback to summary
public boolean test(Method method)

Implements java.util.function.Predicate.test.

Doc from java.util.function.Predicate.test.

Evaluates this predicate on the given argument.

Annotations
@Override
toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.