11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -67,12 +67,7 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
6767
6868 @ Override
6969 public void addFormatter (Formatter <?> formatter ) {
70- Class <?> fieldType = GenericTypeResolver .resolveTypeArgument (formatter .getClass (), Formatter .class );
71- if (fieldType == null ) {
72- throw new IllegalArgumentException ("Unable to extract parameterized field type argument from Formatter [" +
73- formatter .getClass ().getName () + "]; does the formatter parameterize the <T> generic type?" );
74- }
75- addFormatterForFieldType (fieldType , formatter );
70+ addFormatterForFieldType (getFieldType (formatter ), formatter );
7671 }
7772
7873 @ Override
@@ -88,14 +83,8 @@ public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Par
8883 }
8984
9085 @ Override
91- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
92- public void addFormatterForFieldAnnotation (AnnotationFormatterFactory annotationFormatterFactory ) {
93- Class <? extends Annotation > annotationType = (Class <? extends Annotation >)
94- GenericTypeResolver .resolveTypeArgument (annotationFormatterFactory .getClass (), AnnotationFormatterFactory .class );
95- if (annotationType == null ) {
96- throw new IllegalArgumentException ("Unable to extract parameterized Annotation type argument from AnnotationFormatterFactory [" +
97- annotationFormatterFactory .getClass ().getName () + "]; does the factory parameterize the <A extends Annotation> generic type?" );
98- }
86+ public void addFormatterForFieldAnnotation (AnnotationFormatterFactory <? extends Annotation > annotationFormatterFactory ) {
87+ Class <? extends Annotation > annotationType = getAnnotationType (annotationFormatterFactory );
9988 if (this .embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware ) {
10089 ((EmbeddedValueResolverAware ) annotationFormatterFactory ).setEmbeddedValueResolver (this .embeddedValueResolver );
10190 }
@@ -107,6 +96,28 @@ public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotation
10796 }
10897
10998
99+ static Class <?> getFieldType (Formatter <?> formatter ) {
100+ Class <?> fieldType = GenericTypeResolver .resolveTypeArgument (formatter .getClass (), Formatter .class );
101+ if (fieldType == null ) {
102+ throw new IllegalArgumentException ("Unable to extract parameterized field type argument from Formatter [" +
103+ formatter .getClass ().getName () + "]; does the formatter parameterize the <T> generic type?" );
104+ }
105+ return fieldType ;
106+ }
107+
108+ @ SuppressWarnings ("unchecked" )
109+ static Class <? extends Annotation > getAnnotationType (AnnotationFormatterFactory <? extends Annotation > factory ) {
110+ Class <? extends Annotation > annotationType = (Class <? extends Annotation >)
111+ GenericTypeResolver .resolveTypeArgument (factory .getClass (), AnnotationFormatterFactory .class );
112+ if (annotationType == null ) {
113+ throw new IllegalArgumentException ("Unable to extract parameterized Annotation type argument from " +
114+ "AnnotationFormatterFactory [" + factory .getClass ().getName () +
115+ "]; does the factory parameterize the <A extends Annotation> generic type?" );
116+ }
117+ return annotationType ;
118+ }
119+
120+
110121 private static class PrinterConverter implements GenericConverter {
111122
112123 private final Class <?> fieldType ;
@@ -148,7 +159,7 @@ private Class<?> resolvePrinterObjectType(Printer<?> printer) {
148159
149160 @ Override
150161 public String toString () {
151- return this .fieldType .getName () + " -> " + String .class .getName () + " : " + this .printer ;
162+ return ( this .fieldType .getName () + " -> " + String .class .getName () + " : " + this .printer ) ;
152163 }
153164 }
154165
@@ -197,7 +208,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
197208
198209 @ Override
199210 public String toString () {
200- return String .class .getName () + " -> " + this .fieldType .getName () + ": " + this .parser ;
211+ return ( String .class .getName () + " -> " + this .fieldType .getName () + ": " + this .parser ) ;
201212 }
202213 }
203214
@@ -249,8 +260,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
249260
250261 @ Override
251262 public String toString () {
252- return "@" + this .annotationType .getName () + " " + this .fieldType .getName () + " -> " +
253- String .class .getName () + ": " + this .annotationFormatterFactory ;
263+ return ( "@" + this .annotationType .getName () + " " + this .fieldType .getName () + " -> " +
264+ String .class .getName () + ": " + this .annotationFormatterFactory ) ;
254265 }
255266 }
256267
@@ -302,8 +313,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
302313
303314 @ Override
304315 public String toString () {
305- return String .class .getName () + " -> @" + this .annotationType .getName () + " " +
306- this .fieldType .getName () + ": " + this .annotationFormatterFactory ;
316+ return ( String .class .getName () + " -> @" + this .annotationType .getName () + " " +
317+ this .fieldType .getName () + ": " + this .annotationFormatterFactory ) ;
307318 }
308319 }
309320
0 commit comments