2626import org .springframework .core .MethodParameter ;
2727import org .springframework .util .Assert ;
2828import org .springframework .util .ClassUtils ;
29+ import org .springframework .util .ObjectUtils ;
2930
3031/**
3132 * Context about a type to convert to.
@@ -42,6 +43,7 @@ public class TypeDescriptor {
4243 */
4344 public static final TypeDescriptor NULL = new TypeDescriptor ();
4445
46+
4547 private Class <?> type ;
4648
4749 private TypeDescriptor elementType ;
@@ -52,6 +54,7 @@ public class TypeDescriptor {
5254
5355 private Annotation [] cachedFieldAnnotations ;
5456
57+
5558 /**
5659 * Create a new descriptor for the given type.
5760 * <p>Use this constructor when a conversion point comes from a source such as a Map
@@ -99,6 +102,7 @@ private TypeDescriptor(Class<?> collectionType, TypeDescriptor elementType) {
99102 this .elementType = elementType ;
100103 }
101104
105+
102106 /**
103107 * Return the wrapped MethodParameter, if any.
104108 * <p>Note: Either MethodParameter or Field is available.
@@ -124,11 +128,14 @@ public Field getField() {
124128 public Class <?> getType () {
125129 if (this .type != null ) {
126130 return this .type ;
127- } else if (this .field != null ) {
131+ }
132+ else if (this .field != null ) {
128133 return this .field .getType ();
129- } else if (this .methodParameter != null ) {
134+ }
135+ else if (this .methodParameter != null ) {
130136 return this .methodParameter .getParameterType ();
131- } else {
137+ }
138+ else {
132139 return null ;
133140 }
134141 }
@@ -139,16 +146,15 @@ public Class<?> getType() {
139146 */
140147 public Class <?> getObjectType () {
141148 Class <?> type = getType ();
142- return type != null ? ClassUtils .resolvePrimitiveIfNecessary (type ) : type ;
149+ return ( type != null ? ClassUtils .resolvePrimitiveIfNecessary (type ) : type ) ;
143150 }
144151
145152 /**
146153 * Does the underyling declared type equal the type provided?
147154 * @param type the type to test against
148155 */
149156 public boolean typeEquals (Class <?> type ) {
150- Class <?> thisType = getType ();
151- return thisType != null ? thisType .equals (type ) : false ;
157+ return ObjectUtils .nullSafeEquals (getType (), type );
152158 }
153159
154160 /**
@@ -158,7 +164,8 @@ public String getName() {
158164 Class <?> type = getType ();
159165 if (type != null ) {
160166 return getType ().getName ();
161- } else {
167+ }
168+ else {
162169 return null ;
163170 }
164171 }
@@ -198,14 +205,17 @@ public Class<?> getElementType() {
198205 * Return the element type as a type descriptor.
199206 */
200207 public TypeDescriptor getElementTypeDescriptor () {
201- if (elementType != null ) {
202- return elementType ;
203- } else {
208+ if (this .elementType != null ) {
209+ return this .elementType ;
210+ }
211+ else {
204212 if (isArray ()) {
205213 return TypeDescriptor .valueOf (getArrayComponentType ());
206- } else if (isCollection ()) {
214+ }
215+ else if (isCollection ()) {
207216 return TypeDescriptor .valueOf (getCollectionElementType ());
208- } else {
217+ }
218+ else {
209219 return TypeDescriptor .NULL ;
210220 }
211221 }
@@ -232,9 +242,11 @@ public boolean isMapEntryTypeKnown() {
232242 public Class <?> getMapKeyType () {
233243 if (this .field != null ) {
234244 return GenericCollectionTypeResolver .getMapKeyFieldType (field );
235- } else if (this .methodParameter != null ) {
245+ }
246+ else if (this .methodParameter != null ) {
236247 return GenericCollectionTypeResolver .getMapKeyParameterType (this .methodParameter );
237- } else {
248+ }
249+ else {
238250 return null ;
239251 }
240252 }
@@ -246,9 +258,11 @@ public Class<?> getMapKeyType() {
246258 public Class <?> getMapValueType () {
247259 if (this .field != null ) {
248260 return GenericCollectionTypeResolver .getMapValueFieldType (this .field );
249- } else if (this .methodParameter != null ) {
261+ }
262+ else if (this .methodParameter != null ) {
250263 return GenericCollectionTypeResolver .getMapValueParameterType (this .methodParameter );
251- } else {
264+ }
265+ else {
252266 return null ;
253267 }
254268 }
@@ -276,9 +290,11 @@ public Annotation[] getAnnotations() {
276290 this .cachedFieldAnnotations = this .field .getAnnotations ();
277291 }
278292 return this .cachedFieldAnnotations ;
279- } else if (this .methodParameter != null ) {
293+ }
294+ else if (this .methodParameter != null ) {
280295 return this .methodParameter .getParameterAnnotations ();
281- } else {
296+ }
297+ else {
282298 return new Annotation [0 ];
283299 }
284300 }
@@ -324,6 +340,28 @@ public boolean isAssignableTo(TypeDescriptor targetType) {
324340 return type != null && ClassUtils .isAssignable (targetType .getType (), type );
325341 }
326342
343+ private Class <?> getArrayComponentType () {
344+ return getType ().getComponentType ();
345+ }
346+
347+ @ SuppressWarnings ("unchecked" )
348+ private Class <?> getCollectionElementType () {
349+ if (this .type != null ) {
350+ return GenericCollectionTypeResolver .getCollectionType ((Class <? extends Collection >) this .type );
351+ }
352+ else if (this .field != null ) {
353+ return GenericCollectionTypeResolver .getCollectionFieldType (this .field );
354+ }
355+ else {
356+ return GenericCollectionTypeResolver .getCollectionParameterType (this .methodParameter );
357+ }
358+ }
359+
360+ private boolean isTypeAssignableTo (Class <?> clazz ) {
361+ Class <?> type = getType ();
362+ return (type != null && ClassUtils .isAssignable (clazz , type ));
363+ }
364+
327365 /**
328366 * @return a textual representation of the type descriptor (eg. Map<String,Foo>) for use in messages
329367 */
@@ -332,7 +370,8 @@ public String asString() {
332370 if (isArray ()) {
333371 // TODO should properly handle multi dimensional arrays
334372 stringValue .append (getArrayComponentType ().getName ()).append ("[]" );
335- } else {
373+ }
374+ else {
336375 Class <?> clazz = getType ();
337376 if (clazz == null ) {
338377 return "null" ;
@@ -355,27 +394,23 @@ public String asString() {
355394 return stringValue .toString ();
356395 }
357396
358- // internal helpers
359-
360- private Class <?> getArrayComponentType () {
361- return getType (). getComponentType ();
362- }
363-
364- @ SuppressWarnings ( "unchecked" )
365- private Class <?> getCollectionElementType () {
366- if ( this . type != null ) {
367- return GenericCollectionTypeResolver . getCollectionType (( Class <? extends Collection >) this . type );
368- } else if ( this . field != null ) {
369- return GenericCollectionTypeResolver . getCollectionFieldType ( this . field );
370- } else {
371- return GenericCollectionTypeResolver . getCollectionParameterType ( this . methodParameter );
397+ public String toString () {
398+ if ( this == TypeDescriptor . NULL ) {
399+ return "[TypeDescriptor.NULL]" ;
400+ }
401+ else {
402+ StringBuilder builder = new StringBuilder ();
403+ builder . append ( "[TypeDescriptor " );
404+ Annotation [] anns = getAnnotations ();
405+ for ( Annotation ann : anns ) {
406+ builder . append ( "@" ). append ( ann . annotationType (). getName ()). append ( ' ' );
407+ }
408+ builder . append ( getType (). getName () );
409+ builder . append ( "]" );
410+ return builder . toString ( );
372411 }
373412 }
374413
375- private boolean isTypeAssignableTo (Class <?> clazz ) {
376- Class <?> type = getType ();
377- return (type != null && ClassUtils .isAssignable (clazz , type ));
378- }
379414
380415 // static factory methods
381416
@@ -406,21 +441,5 @@ public static TypeDescriptor forObject(Object object) {
406441 public static TypeDescriptor collection (Class <?> type , TypeDescriptor elementType ) {
407442 return new TypeDescriptor (type , elementType );
408443 }
409-
410- public String toString () {
411- if (this == TypeDescriptor .NULL ) {
412- return "[TypeDescriptor.NULL]" ;
413- } else {
414- StringBuilder builder = new StringBuilder ();
415- builder .append ("[TypeDescriptor " );
416- Annotation [] anns = getAnnotations ();
417- for (Annotation ann : anns ) {
418- builder .append ("@" + ann .annotationType ().getName ()).append (' ' );
419- }
420- builder .append (getType ().getName ());
421- builder .append ("]" );
422- return builder .toString ();
423- }
424- }
425-
444+
426445}
0 commit comments