26
26
import org .springframework .core .MethodParameter ;
27
27
import org .springframework .util .Assert ;
28
28
import org .springframework .util .ClassUtils ;
29
+ import org .springframework .util .ObjectUtils ;
29
30
30
31
/**
31
32
* Context about a type to convert to.
@@ -42,6 +43,7 @@ public class TypeDescriptor {
42
43
*/
43
44
public static final TypeDescriptor NULL = new TypeDescriptor ();
44
45
46
+
45
47
private Class <?> type ;
46
48
47
49
private TypeDescriptor elementType ;
@@ -52,6 +54,7 @@ public class TypeDescriptor {
52
54
53
55
private Annotation [] cachedFieldAnnotations ;
54
56
57
+
55
58
/**
56
59
* Create a new descriptor for the given type.
57
60
* <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) {
99
102
this .elementType = elementType ;
100
103
}
101
104
105
+
102
106
/**
103
107
* Return the wrapped MethodParameter, if any.
104
108
* <p>Note: Either MethodParameter or Field is available.
@@ -124,11 +128,14 @@ public Field getField() {
124
128
public Class <?> getType () {
125
129
if (this .type != null ) {
126
130
return this .type ;
127
- } else if (this .field != null ) {
131
+ }
132
+ else if (this .field != null ) {
128
133
return this .field .getType ();
129
- } else if (this .methodParameter != null ) {
134
+ }
135
+ else if (this .methodParameter != null ) {
130
136
return this .methodParameter .getParameterType ();
131
- } else {
137
+ }
138
+ else {
132
139
return null ;
133
140
}
134
141
}
@@ -139,16 +146,15 @@ public Class<?> getType() {
139
146
*/
140
147
public Class <?> getObjectType () {
141
148
Class <?> type = getType ();
142
- return type != null ? ClassUtils .resolvePrimitiveIfNecessary (type ) : type ;
149
+ return ( type != null ? ClassUtils .resolvePrimitiveIfNecessary (type ) : type ) ;
143
150
}
144
151
145
152
/**
146
153
* Does the underyling declared type equal the type provided?
147
154
* @param type the type to test against
148
155
*/
149
156
public boolean typeEquals (Class <?> type ) {
150
- Class <?> thisType = getType ();
151
- return thisType != null ? thisType .equals (type ) : false ;
157
+ return ObjectUtils .nullSafeEquals (getType (), type );
152
158
}
153
159
154
160
/**
@@ -158,7 +164,8 @@ public String getName() {
158
164
Class <?> type = getType ();
159
165
if (type != null ) {
160
166
return getType ().getName ();
161
- } else {
167
+ }
168
+ else {
162
169
return null ;
163
170
}
164
171
}
@@ -198,14 +205,17 @@ public Class<?> getElementType() {
198
205
* Return the element type as a type descriptor.
199
206
*/
200
207
public TypeDescriptor getElementTypeDescriptor () {
201
- if (elementType != null ) {
202
- return elementType ;
203
- } else {
208
+ if (this .elementType != null ) {
209
+ return this .elementType ;
210
+ }
211
+ else {
204
212
if (isArray ()) {
205
213
return TypeDescriptor .valueOf (getArrayComponentType ());
206
- } else if (isCollection ()) {
214
+ }
215
+ else if (isCollection ()) {
207
216
return TypeDescriptor .valueOf (getCollectionElementType ());
208
- } else {
217
+ }
218
+ else {
209
219
return TypeDescriptor .NULL ;
210
220
}
211
221
}
@@ -232,9 +242,11 @@ public boolean isMapEntryTypeKnown() {
232
242
public Class <?> getMapKeyType () {
233
243
if (this .field != null ) {
234
244
return GenericCollectionTypeResolver .getMapKeyFieldType (field );
235
- } else if (this .methodParameter != null ) {
245
+ }
246
+ else if (this .methodParameter != null ) {
236
247
return GenericCollectionTypeResolver .getMapKeyParameterType (this .methodParameter );
237
- } else {
248
+ }
249
+ else {
238
250
return null ;
239
251
}
240
252
}
@@ -246,9 +258,11 @@ public Class<?> getMapKeyType() {
246
258
public Class <?> getMapValueType () {
247
259
if (this .field != null ) {
248
260
return GenericCollectionTypeResolver .getMapValueFieldType (this .field );
249
- } else if (this .methodParameter != null ) {
261
+ }
262
+ else if (this .methodParameter != null ) {
250
263
return GenericCollectionTypeResolver .getMapValueParameterType (this .methodParameter );
251
- } else {
264
+ }
265
+ else {
252
266
return null ;
253
267
}
254
268
}
@@ -276,9 +290,11 @@ public Annotation[] getAnnotations() {
276
290
this .cachedFieldAnnotations = this .field .getAnnotations ();
277
291
}
278
292
return this .cachedFieldAnnotations ;
279
- } else if (this .methodParameter != null ) {
293
+ }
294
+ else if (this .methodParameter != null ) {
280
295
return this .methodParameter .getParameterAnnotations ();
281
- } else {
296
+ }
297
+ else {
282
298
return new Annotation [0 ];
283
299
}
284
300
}
@@ -324,6 +340,28 @@ public boolean isAssignableTo(TypeDescriptor targetType) {
324
340
return type != null && ClassUtils .isAssignable (targetType .getType (), type );
325
341
}
326
342
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
+
327
365
/**
328
366
* @return a textual representation of the type descriptor (eg. Map<String,Foo>) for use in messages
329
367
*/
@@ -332,7 +370,8 @@ public String asString() {
332
370
if (isArray ()) {
333
371
// TODO should properly handle multi dimensional arrays
334
372
stringValue .append (getArrayComponentType ().getName ()).append ("[]" );
335
- } else {
373
+ }
374
+ else {
336
375
Class <?> clazz = getType ();
337
376
if (clazz == null ) {
338
377
return "null" ;
@@ -355,27 +394,23 @@ public String asString() {
355
394
return stringValue .toString ();
356
395
}
357
396
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 ( );
372
411
}
373
412
}
374
413
375
- private boolean isTypeAssignableTo (Class <?> clazz ) {
376
- Class <?> type = getType ();
377
- return (type != null && ClassUtils .isAssignable (clazz , type ));
378
- }
379
414
380
415
// static factory methods
381
416
@@ -406,21 +441,5 @@ public static TypeDescriptor forObject(Object object) {
406
441
public static TypeDescriptor collection (Class <?> type , TypeDescriptor elementType ) {
407
442
return new TypeDescriptor (type , elementType );
408
443
}
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
+
426
445
}
0 commit comments