Package org.joda.convert
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 java.lang.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 Summary
Fields Modifier and Type Field Description private java.lang.Class<T>clsThe converted class.(package private) TypedFromStringConverter<T>fromStringConversion from a string, package-scoped for testing.private java.lang.reflect.MethodtoStringConversion to a string.
-
Constructor Summary
Constructors Constructor Description ReflectionStringConverter(java.lang.Class<T> cls, java.lang.reflect.Method toString, TypedFromStringConverter<T> fromString)Creates an instance using two methods.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TconvertFromString(java.lang.Class<? extends T> cls, java.lang.String str)Converts the specified object from aString.java.lang.StringconvertToString(T object)Converts the specified object to aString.java.lang.Class<?>getEffectiveType()Gets the effective type that the converter works on.java.lang.StringtoString()
-
-
-
Field Detail
-
cls
private final java.lang.Class<T> cls
The converted class.
-
toString
private final java.lang.reflect.Method toString
Conversion to a string.
-
fromString
final TypedFromStringConverter<T> fromString
Conversion from a string, package-scoped for testing.
-
-
Constructor Detail
-
ReflectionStringConverter
ReflectionStringConverter(java.lang.Class<T> cls, java.lang.reflect.Method toString, TypedFromStringConverter<T> fromString)
Creates an instance using two methods.- Parameters:
cls- the class this converts for, null creates a from-string convertertoString- the toString method, not nullfromString- the fromString converter, not null- Throws:
java.lang.RuntimeException- (or subclass) if the method signatures are invalid
-
-
Method Detail
-
convertToString
public java.lang.String convertToString(T object)
Description copied from interface:ToStringConverterConverts the specified object to aString.- Specified by:
convertToStringin interfaceToStringConverter<T>- Parameters:
object- the object to convert, not null- Returns:
- the converted string, may be null but generally not
-
convertFromString
public T convertFromString(java.lang.Class<? extends T> cls, java.lang.String str)
Description copied from interface:FromStringConverterConverts the specified object from aString.- Specified by:
convertFromStringin interfaceFromStringConverter<T>- Parameters:
cls- the class to convert to, not nullstr- 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:TypedStringConverterGets the effective type that the converter works on.For example, if a class declares the
FromStringandToStringthen 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:
getEffectiveTypein interfaceTypedFromStringConverter<T>- Specified by:
getEffectiveTypein interfaceTypedStringConverter<T>- Returns:
- the effective type
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-