Class MethodConstructorStringConverter<T>

  • Type Parameters:
    T - the type of the converter
    All Implemented Interfaces:
    FromStringConverter<T>, StringConverter<T>, ToStringConverter<T>, TypedStringConverter<T>

    final class MethodConstructorStringConverter<T>
    extends ReflectionStringConverter<T>
    Conversion to and from a string using a toString method and a fromString constructor.

    The toString method must meet the following signature:
    String anyName() on Class T.

    The fromString constructor must take a single String parameter.

    MethodConstructorStringConverter is thread-safe and immutable.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.reflect.Constructor<T> fromString
      Conversion from a string.
    • Constructor Summary

      Constructors 
      Constructor Description
      MethodConstructorStringConverter​(java.lang.Class<T> cls, java.lang.reflect.Method toString, java.lang.reflect.Constructor<T> fromString)
      Creates an instance using a method and a constructor.
    • Field Detail

      • fromString

        private final java.lang.reflect.Constructor<T> fromString
        Conversion from a string.
    • Constructor Detail

      • MethodConstructorStringConverter

        MethodConstructorStringConverter​(java.lang.Class<T> cls,
                                         java.lang.reflect.Method toString,
                                         java.lang.reflect.Constructor<T> fromString)
        Creates an instance using a method and a constructor.
        Parameters:
        cls - the class this converts for, not null
        toString - the toString method, not null
        fromString - the fromString method, not null
        Throws:
        java.lang.RuntimeException - (or subclass) if the method signatures are invalid
    • Method Detail

      • convertFromString

        public T convertFromString​(java.lang.Class<? extends T> cls,
                                   java.lang.String str)
        Converts the String to an object.
        Parameters:
        cls - the class to convert to, not null
        str - the string to convert, not null
        Returns:
        the converted object, may be null but generally not
      • getEffectiveType

        public java.lang.Class<?> getEffectiveType()
        Description copied from interface: TypedStringConverter
        Gets the effective type that the converter works on.

        For example, if a class declares the FromString and ToString then the effective type of the converter is that class. If a subclass is queried for a converter, then the effective type is that of the superclass.

        Returns:
        the effective type