Class ConversionSupport


  • @API(status=MAINTAINED,
         since="1.13.3")
    public final class ConversionSupport
    extends java.lang.Object
    ConversionSupport provides 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> T convert​(java.lang.String source, java.lang.Class<T> targetType, java.lang.ClassLoader classLoader)
      Convert the supplied source String into an instance of the specified target type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • convert

        public static <T> T convert​(java.lang.String source,
                                    java.lang.Class<T> targetType,
                                    java.lang.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, File, Path, Charset, BigDecimal, BigInteger, Currency, Locale, UUID, URI, and 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.

        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 source String to convert; may be null but only if the target type is a reference type
        targetType - the target type the source should be converted into; never null
        classLoader - the ClassLoader to use; may be null to use the default ClassLoader
        Returns:
        the converted object; may be null but only if the target type is a reference type
        Since:
        1.11