Class ReflectionStringConverter<T>

java.lang.Object
org.joda.convert.ReflectionStringConverter<T>
Type Parameters:
T - the type of the converter
All Implemented Interfaces:
FromStringConverter<T>, StringConverter<T>, ToStringConverter<T>, TypedFromStringConverter<T>, TypedStringConverter<T>

final class ReflectionStringConverter<T> extends Object implements TypedStringConverter<T>
Conversion to and from a string using reflection.

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

ReflectionStringConverter is abstract, but all known implementations are thread-safe and immutable.

  • Field Details

    • cls

      private final Class<T> cls
      The converted class.
    • toString

      private final Method toString
      Conversion to a string.
    • fromString

      final TypedFromStringConverter<T> fromString
      Conversion from a string, package-scoped for testing.
  • Constructor Details

    • ReflectionStringConverter

      ReflectionStringConverter(Class<T> cls, Method toString, TypedFromStringConverter<T> fromString)
      Creates an instance using two methods.
      Parameters:
      cls - the class this converts for, null creates a from-string converter
      toString - the toString method, not null
      fromString - the fromString converter, not null
      Throws:
      RuntimeException - (or subclass) if the method signatures are invalid
  • Method Details

    • convertToString

      public String convertToString(T object)
      Description copied from interface: ToStringConverter
      Converts the specified object to a String.
      Specified by:
      convertToString in interface ToStringConverter<T>
      Parameters:
      object - the object to convert, not null
      Returns:
      the converted string, may be null but generally not
    • convertFromString

      public T convertFromString(Class<? extends T> cls, String str)
      Description copied from interface: FromStringConverter
      Converts the specified object from a String.
      Specified by:
      convertFromString in interface FromStringConverter<T>
      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 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.

      Specified by:
      getEffectiveType in interface TypedFromStringConverter<T>
      Specified by:
      getEffectiveType in interface TypedStringConverter<T>
      Returns:
      the effective type
    • toString

      public String toString()
      Overrides:
      toString in class Object