@ -17,8 +17,6 @@
@@ -17,8 +17,6 @@
package org.springframework.ui.format.support ;
import java.lang.annotation.Annotation ;
import java.lang.reflect.Method ;
import java.lang.reflect.Modifier ;
import java.text.ParseException ;
import java.util.LinkedList ;
import java.util.Locale ;
@ -40,7 +38,6 @@ import org.springframework.ui.format.Formatted;
@@ -40,7 +38,6 @@ import org.springframework.ui.format.Formatted;
import org.springframework.ui.format.Formatter ;
import org.springframework.ui.format.FormatterRegistry ;
import org.springframework.util.Assert ;
import org.springframework.util.ReflectionUtils ;
/ * *
* A generic implementation of { @link org . springframework . ui . format . FormatterRegistry }
@ -61,9 +58,9 @@ import org.springframework.util.ReflectionUtils;
@@ -61,9 +58,9 @@ import org.springframework.util.ReflectionUtils;
* /
public class GenericFormatterRegistry implements FormatterRegistry , ApplicationContextAware , Cloneable {
private final Map < Class , FormatterHolder > typeFormatters = new ConcurrentHashMap < Class , FormatterHolder > ( ) ;
private final Map < Class < ? > , FormatterHolder > typeFormatters = new ConcurrentHashMap < Class < ? > , FormatterHolder > ( ) ;
private final Map < Class , AnnotationFormatterFactoryHolder > annotationFormatters = new ConcurrentHashMap < Class , AnnotationFormatterFactoryHolder > ( ) ;
private final Map < Class < ? > , AnnotationFormatterFactoryHolder > annotationFormatters = new ConcurrentHashMap < Class < ? > , AnnotationFormatterFactoryHolder > ( ) ;
private ConversionService conversionService ;
@ -182,47 +179,47 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -182,47 +179,47 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
// implementing FormatterRegistry
public void addFormatterByType ( Formatter < ? > formatter ) {
Class < ? > formatterObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
if ( formatterObjectType = = null ) {
throw new IllegalArgumentException ( "Unable to register Formatter " + formatter
+ "; cannot determine parameterized object type <T>" ) ;
}
this . typeFormatters . put ( formatterObjectType , new FormatterHolder ( formatterObjectType , formatter ) ) ;
}
public void addFormatterByType ( Class < ? > type , Formatter < ? > formatter ) {
Class < ? > formattedObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
if ( formattedObjectType ! = null & & ! type . isAssignableFrom ( formattedObjectType ) ) {
Class < ? > formatter ObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
if ( formatter ObjectType ! = null & & ! type . isAssignableFrom ( formatter ObjectType ) ) {
if ( this . conversionService = = null ) {
throw new IllegalStateException ( "Unable to index Formatter " + formatter + " under type ["
+ type . getName ( ) + "]; not able to convert from a [" + formattedObjectType . getName ( )
+ type . getName ( ) + "]; not able to convert from a [" + formatter ObjectType . getName ( )
+ "] parsed by the Formatter to [" + type . getName ( )
+ "] because this.conversionService is null" ) ;
}
if ( ! this . conversionService . canConvert ( formatted ObjectType , type ) ) {
if ( ! this . conversionService . canConvert ( formatter ObjectType , type ) ) {
throw new IllegalArgumentException ( "Unable to index Formatter " + formatter + " under type ["
+ type . getName ( ) + "]; not able to convert from a [" + formatted ObjectType . getName ( )
+ type . getName ( ) + "]; not able to convert from a [" + formatter ObjectType . getName ( )
+ "] parsed by the Formatter to [" + type . getName ( ) + "]" ) ;
}
if ( ! this . conversionService . canConvert ( type , formatted ObjectType ) ) {
if ( ! this . conversionService . canConvert ( type , formatter ObjectType ) ) {
throw new IllegalArgumentException ( "Unable to index Formatter " + formatter + " under type ["
+ type . getName ( ) + "]; not able to convert to [" + formatted ObjectType . getName ( )
+ type . getName ( ) + "]; not able to convert to [" + formatter ObjectType . getName ( )
+ "] to format a [" + type . getName ( ) + "]" ) ;
}
}
this . typeFormatters . put ( type , new FormatterHolder ( formattedObjectType , formatter ) ) ;
}
public void addFormatterByType ( Formatter < ? > formatter ) {
Class < ? > formattedObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
if ( formattedObjectType = = null ) {
throw new IllegalArgumentException ( "Unable to register Formatter " + formatter
+ "; cannot determine parameterized object type <T>" ) ;
}
this . typeFormatters . put ( formattedObjectType , new FormatterHolder ( formattedObjectType , formatter ) ) ;
this . typeFormatters . put ( type , new FormatterHolder ( formatterObjectType , formatter ) ) ;
}
public void addFormatterByAnnotation ( Class < ? extends Annotation > annotationType , Formatter < ? > formatter ) {
Class < ? > formatted ObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
Class < ? > formatterObjectType = GenericTypeResolver . resolveTypeArgument ( formatter . getClass ( ) , Formatter . class ) ;
SimpleAnnotationFormatterFactory factory = new SimpleAnnotationFormatterFactory ( formatter ) ;
this . annotationFormatters . put ( annotationType ,
new AnnotationFormatterFactoryHolder ( formatted ObjectType , factory ) ) ;
new AnnotationFormatterFactoryHolder ( formatterObjectType , factory ) ) ;
}
public void addFormatterByAnnotation ( AnnotationFormatterFactory < ? , ? > factory ) {
Class [ ] typeArgs = GenericTypeResolver . resolveTypeArguments ( factory . getClass ( ) ,
Class < ? > [ ] typeArgs = GenericTypeResolver . resolveTypeArguments ( factory . getClass ( ) ,
AnnotationFormatterFactory . class ) ;
if ( typeArgs = = null | | typeArgs . length ! = 2 ) {
throw new IllegalArgumentException (
@ -236,17 +233,18 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -236,17 +233,18 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
public Formatter < Object > getFormatter ( TypeDescriptor type ) {
Assert . notNull ( type , "TypeDescriptor is required" ) ;
FormatterHolder holder = findFormatterHolderForAnnotatedProperty ( type . getAnnotations ( ) ) ;
Class < ? > objectType = type . getObjectType ( ) ;
if ( holder = = null ) {
holder = findFormatterHolderForType ( type . getType ( ) ) ;
holder = findFormatterHolderForType ( objectType ) ;
}
if ( holder = = null ) {
holder = getDefaultFormatterHolder ( type ) ;
holder = getDefaultFormatterHolder ( objec tT ype) ;
}
if ( holder = = null ) {
return null ;
}
Class formatted ObjectType = holder . getFormatted ObjectType ( ) ;
if ( formatted ObjectType ! = null & & ! type . getType ( ) . isAssignableFrom ( formatted ObjectType ) ) {
Class < ? > formatter ObjectType = holder . getFormatter ObjectType ( ) ;
if ( formatter ObjectType ! = null & & ! objectType . isAssignableFrom ( formatter ObjectType ) ) {
if ( this . conversionService ! = null ) {
return new ConvertingFormatter ( type , holder ) ;
} else {
@ -278,7 +276,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -278,7 +276,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
Formatted formattedAnnotation = annotationType . getAnnotation ( Formatted . class ) ;
if ( formattedAnnotation ! = null ) {
// property annotation has @Formatted meta-annotation
Formatter formatter = createFormatter ( formattedAnnotation . value ( ) ) ;
Formatter < ? > formatter = createFormatter ( formattedAnnotation . value ( ) ) ;
addFormatterByAnnotation ( annotationType , formatter ) ;
return findFormatterHolderForAnnotation ( annotation ) ;
} else {
@ -287,11 +285,11 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -287,11 +285,11 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
}
}
private FormatterHolder findFormatterHolderForType ( Class type ) {
LinkedList < Class > classQueue = new LinkedList < Class > ( ) ;
private FormatterHolder findFormatterHolderForType ( Class < ? > type ) {
LinkedList < Class < ? > > classQueue = new LinkedList < Class < ? > > ( ) ;
classQueue . addFirst ( type ) ;
while ( ! classQueue . isEmpty ( ) ) {
Class currentClass = classQueue . removeLast ( ) ;
Class < ? > currentClass = classQueue . removeLast ( ) ;
FormatterHolder holder = this . typeFormatters . get ( currentClass ) ;
if ( holder ! = null ) {
return holder ;
@ -299,19 +297,18 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -299,19 +297,18 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
if ( currentClass . getSuperclass ( ) ! = null ) {
classQueue . addFirst ( currentClass . getSuperclass ( ) ) ;
}
Class [ ] interfaces = currentClass . getInterfaces ( ) ;
for ( Class ifc : interfaces ) {
Class < ? > [ ] interfaces = currentClass . getInterfaces ( ) ;
for ( Class < ? > ifc : interfaces ) {
classQueue . addFirst ( ifc ) ;
}
}
return null ;
}
private FormatterHolder getDefaultFormatterHolder ( TypeDescriptor typeDescriptor ) {
Class type = typeDescriptor . getType ( ) ;
private FormatterHolder getDefaultFormatterHolder ( Class < ? > type ) {
Formatted formatted = AnnotationUtils . findAnnotation ( type , Formatted . class ) ;
if ( formatted ! = null ) {
Formatter formatter = createFormatter ( formatted . value ( ) ) ;
Formatter < ? > formatter = createFormatter ( formatted . value ( ) ) ;
addFormatterByType ( type , formatter ) ;
return findFormatterHolderForType ( type ) ;
} else {
@ -319,21 +316,21 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -319,21 +316,21 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
}
}
private Formatter createFormatter ( Class < ? extends Formatter > formatterClass ) {
private Formatter < ? > createFormatter ( Class < ? extends Formatter > formatterClass ) {
return ( this . applicationContext ! = null ? this . applicationContext . getAutowireCapableBeanFactory ( ) . createBean (
formatterClass ) : BeanUtils . instantiate ( formatterClass ) ) ;
}
private abstract static class AbstractFormatterHolder {
private Class formatted ObjectType ;
private Class < ? > formatter ObjectType;
public AbstractFormatterHolder ( Class formatted ObjectType ) {
this . formattedObjectType = formatted ObjectType ;
public AbstractFormatterHolder ( Class < ? > formatter ObjectType) {
this . formatterObjectType = formatter ObjectType ;
}
public Class < ? > getFormatted ObjectType ( ) {
return formatted ObjectType ;
public Class < ? > getFormatter ObjectType ( ) {
return formatter ObjectType ;
}
}
@ -342,8 +339,8 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -342,8 +339,8 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
private Formatter formatter ;
public FormatterHolder ( Class formatted ObjectType , Formatter formatter ) {
super ( formatted ObjectType ) ;
public FormatterHolder ( Class < ? > formatter ObjectType, Formatter < ? > formatter ) {
super ( formatter ObjectType ) ;
this . formatter = formatter ;
}
@ -357,13 +354,13 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -357,13 +354,13 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
private AnnotationFormatterFactory factory ;
public AnnotationFormatterFactoryHolder ( Class formatted ObjectType , AnnotationFormatterFactory factory ) {
super ( formatted ObjectType ) ;
public AnnotationFormatterFactoryHolder ( Class < ? > formatter ObjectType, AnnotationFormatterFactory < ? , ? > factory ) {
super ( formatter ObjectType ) ;
this . factory = factory ;
}
public FormatterHolder getFormatterHolder ( Annotation annotation ) {
return new FormatterHolder ( getFormatted ObjectType ( ) , this . factory . getFormatter ( annotation ) ) ;
return new FormatterHolder ( getFormatter ObjectType ( ) , this . factory . getFormatter ( annotation ) ) ;
}
}
@ -372,7 +369,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -372,7 +369,7 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
private final Formatter instance ;
public SimpleAnnotationFormatterFactory ( Formatter instance ) {
public SimpleAnnotationFormatterFactory ( Formatter < ? > instance ) {
this . instance = instance ;
}
@ -381,28 +378,6 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -381,28 +378,6 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
}
}
private static class ValueOfMethodFormatter implements Formatter {
private Method valueOfMethod ;
public ValueOfMethodFormatter ( Method valueOfMethod ) {
this . valueOfMethod = valueOfMethod ;
}
public String format ( Object object , Locale locale ) {
if ( object = = null ) {
return "" ;
} else {
return object . toString ( ) ;
}
}
public Object parse ( String formatted , Locale locale ) throws ParseException {
return ReflectionUtils . invokeMethod ( valueOfMethod , null , formatted ) ;
}
}
private class ConvertingFormatter implements Formatter {
private final TypeDescriptor type ;
@ -416,14 +391,14 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
@@ -416,14 +391,14 @@ public class GenericFormatterRegistry implements FormatterRegistry, ApplicationC
public String format ( Object object , Locale locale ) {
object = GenericFormatterRegistry . this . conversionService . convert ( object , this . formatterHolder
. getFormatted ObjectType ( ) ) ;
. getFormatter ObjectType ( ) ) ;
return this . formatterHolder . getFormatter ( ) . format ( object , locale ) ;
}
public Object parse ( String formatted , Locale locale ) throws ParseException {
Object parsed = this . formatterHolder . getFormatter ( ) . parse ( formatted , locale ) ;
parsed = GenericFormatterRegistry . this . conversionService . convert ( parsed , TypeDescriptor
. valueOf ( this . formatterHolder . getFormatted ObjectType ( ) ) , this . type ) ;
. valueOf ( this . formatterHolder . getFormatter ObjectType ( ) ) , this . type ) ;
return parsed ;
}
}