@ -37,6 +37,8 @@ import org.springframework.format.FormatterRegistry;
@@ -37,6 +37,8 @@ import org.springframework.format.FormatterRegistry;
import org.springframework.format.Parser ;
import org.springframework.format.Printer ;
import org.springframework.lang.Nullable ;
import org.springframework.util.Assert ;
import org.springframework.util.ClassUtils ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringValueResolver ;
@ -65,6 +67,18 @@ public class FormattingConversionService extends GenericConversionService
@@ -65,6 +67,18 @@ public class FormattingConversionService extends GenericConversionService
}
@Override
public void addPrinter ( Printer < ? > printer ) {
Class < ? > fieldType = getFieldType ( printer , Printer . class ) ;
addConverter ( new PrinterConverter ( fieldType , printer , this ) ) ;
}
@Override
public void addParser ( Parser < ? > parser ) {
Class < ? > fieldType = getFieldType ( parser , Parser . class ) ;
addConverter ( new ParserConverter ( fieldType , parser , this ) ) ;
}
@Override
public void addFormatter ( Formatter < ? > formatter ) {
addFormatterForFieldType ( getFieldType ( formatter ) , formatter ) ;
@ -97,15 +111,18 @@ public class FormattingConversionService extends GenericConversionService
@@ -97,15 +111,18 @@ public class FormattingConversionService extends GenericConversionService
static Class < ? > getFieldType ( Formatter < ? > formatter ) {
Class < ? > fieldType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
if ( fieldType = = null & & formatter instanceof DecoratingProxy ) {
return getFieldType ( formatter , Formatter . class ) ;
}
private static < T > Class < ? > getFieldType ( T instance , Class < T > genericInterface ) {
Class < ? > fieldType = GenericTypeResolver . resolveTypeArgument ( instance . getClass ( ) , genericInterface ) ;
if ( fieldType = = null & & instance instanceof DecoratingProxy ) {
fieldType = GenericTypeResolver . resolveTypeArgument (
( ( DecoratingProxy ) formatter ) . getDecoratedClass ( ) , Formatter . class ) ;
}
if ( fieldType = = null ) {
throw new IllegalArgumentException ( "Unable to extract the parameterized field type from Formatter [" +
formatter . getClass ( ) . getName ( ) + "]; does the class parameterize the <T> generic type?" ) ;
( ( DecoratingProxy ) instance ) . getDecoratedClass ( ) , genericInterface ) ;
}
Assert . notNull ( fieldType , ( ) - > "Unable to extract the parameterized field type from " +
ClassUtils . getShortName ( genericInterface ) + " [" + instance . getClass ( ) . getName ( ) +
"]; does the class parameterize the <T> generic type?" ) ;
return fieldType ;
}