@@ -64,7 +64,8 @@ public class TypeDescriptor {
64
64
typeDescriptorCache .put (Double .class , new TypeDescriptor (Double .class ));
65
65
typeDescriptorCache .put (String .class , new TypeDescriptor (String .class ));
66
66
}
67
-
67
+
68
+
68
69
private Class <?> type ;
69
70
70
71
private MethodParameter methodParameter ;
@@ -81,6 +82,7 @@ public class TypeDescriptor {
81
82
82
83
private Annotation [] annotations ;
83
84
85
+
84
86
/**
85
87
* Create a new type descriptor from a method or constructor parameter.
86
88
* <p>Use this constructor when a target conversion point originates from a method parameter,
@@ -127,38 +129,36 @@ public TypeDescriptor(Field field, Class<?> type) {
127
129
this .type = type ;
128
130
}
129
131
130
- // static factory methods
132
+ /**
133
+ * Internal constructor for a NULL descriptor.
134
+ */
135
+ private TypeDescriptor () {
136
+ }
131
137
132
138
/**
133
- * Create a new type descriptor for the class of the given object.
134
- * @param object the object
135
- * @return the type descriptor
139
+ * Create a new descriptor for the type of the given value.
140
+ * <p>Use this constructor when a conversion point comes from a source such as a Map or
141
+ * Collection, where no additional context is available but elements can be introspected.
142
+ * @param type the actual type to wrap
136
143
*/
137
- public static TypeDescriptor forObject (Object object ) {
138
- if (object == null ) {
139
- return NULL ;
140
- }
141
- else if (object instanceof Collection <?> || object instanceof Map <?, ?>) {
142
- return new TypeDescriptor (object );
143
- }
144
- else {
145
- return valueOf (object .getClass ());
146
- }
144
+ private TypeDescriptor (Object value ) {
145
+ Assert .notNull (value , "Value must not be null" );
146
+ this .value = value ;
147
+ this .type = value .getClass ();
147
148
}
148
-
149
+
149
150
/**
150
- * Create a new type descriptor for the given class.
151
- * @param type the class
152
- * @return the type descriptor
151
+ * Create a new descriptor for the given type.
152
+ * <p>Use this constructor when a conversion point comes from a plain source type,
153
+ * where no additional context is available.
154
+ * @param type the actual type to wrap
153
155
*/
154
- public static TypeDescriptor valueOf (Class <?> type ) {
155
- if (type == null ) {
156
- return TypeDescriptor .NULL ;
157
- }
158
- TypeDescriptor desc = typeDescriptorCache .get (type );
159
- return (desc != null ? desc : new TypeDescriptor (type ));
156
+ private TypeDescriptor (Class <?> type ) {
157
+ Assert .notNull (type , "Type must not be null" );
158
+ this .type = type ;
160
159
}
161
160
161
+
162
162
/**
163
163
* Return the wrapped MethodParameter, if any.
164
164
* <p>Note: Either MethodParameter or Field is available.
@@ -255,13 +255,14 @@ public synchronized TypeDescriptor getElementTypeDescriptor() {
255
255
}
256
256
257
257
/**
258
- * Return the element type as a type descriptor; if the element type is null (cannot be determined), the type descriptor is derived from the element argument.
258
+ * Return the element type as a type descriptor; if the element type is null (cannot be determined),
259
+ * the type descriptor is derived from the element argument.
259
260
* @param element the element
260
261
* @return the element type descriptor
261
262
*/
262
263
public TypeDescriptor getElementTypeDescriptor (Object element ) {
263
264
TypeDescriptor elementType = getElementTypeDescriptor ();
264
- return elementType != TypeDescriptor .NULL ? elementType : TypeDescriptor . forObject (element );
265
+ return ( elementType != TypeDescriptor .NULL ? elementType : forObject (element ));
265
266
}
266
267
267
268
/**
@@ -318,20 +319,21 @@ public Class<?> getMapValueType() {
318
319
* Returns map value type as a type descriptor.
319
320
*/
320
321
public synchronized TypeDescriptor getMapValueTypeDescriptor () {
321
- if (mapValueType == null ) {
322
+ if (this . mapValueType == null ) {
322
323
mapValueType = forElementType (resolveMapValueType ());
323
324
}
324
- return mapValueType ;
325
+ return this . mapValueType ;
325
326
}
326
327
327
328
/**
328
- * Return the map value type as a type descriptor; if the value type is null (cannot be determined), the type descriptor is derived from the value argument.
329
+ * Return the map value type as a type descriptor; if the value type is null
330
+ * (cannot be determined), the type descriptor is derived from the value argument.
329
331
* @param value the value
330
332
* @return the map value type descriptor
331
333
*/
332
334
public TypeDescriptor getMapValueTypeDescriptor (Object value ) {
333
335
TypeDescriptor valueType = getMapValueTypeDescriptor ();
334
- return valueType != TypeDescriptor .NULL ? valueType : TypeDescriptor .forObject (value );
336
+ return ( valueType != TypeDescriptor .NULL ? valueType : TypeDescriptor .forObject (value ) );
335
337
}
336
338
337
339
/**
@@ -366,10 +368,15 @@ public boolean isAssignableTo(TypeDescriptor targetType) {
366
368
return true ;
367
369
}
368
370
if (isCollection () && targetType .isCollection () || isArray () && targetType .isArray ()) {
369
- return targetType .getType ().isAssignableFrom (getType ()) && getElementTypeDescriptor ().isAssignableTo (targetType .getElementTypeDescriptor ());
370
- } else if (isMap () && targetType .isMap ()) {
371
- return targetType .getType ().isAssignableFrom (getType ()) && getMapKeyTypeDescriptor ().isAssignableTo (targetType .getMapKeyTypeDescriptor ()) && getMapValueTypeDescriptor ().isAssignableTo (targetType .getMapValueTypeDescriptor ());
372
- } else {
371
+ return targetType .getType ().isAssignableFrom (getType ()) &&
372
+ getElementTypeDescriptor ().isAssignableTo (targetType .getElementTypeDescriptor ());
373
+ }
374
+ else if (isMap () && targetType .isMap ()) {
375
+ return targetType .getType ().isAssignableFrom (getType ()) &&
376
+ getMapKeyTypeDescriptor ().isAssignableTo (targetType .getMapKeyTypeDescriptor ()) &&
377
+ getMapValueTypeDescriptor ().isAssignableTo (targetType .getMapValueTypeDescriptor ());
378
+ }
379
+ else {
373
380
return targetType .getObjectType ().isAssignableFrom (getObjectType ());
374
381
}
375
382
}
@@ -383,14 +390,17 @@ public boolean isAssignableTo(TypeDescriptor targetType) {
383
390
public TypeDescriptor forElementType (Class <?> elementType ) {
384
391
if (getType ().equals (elementType )) {
385
392
return this ;
386
- } else if (elementType == null ) {
393
+ }
394
+ else if (elementType == null ) {
387
395
return TypeDescriptor .NULL ;
388
- } else if (this .methodParameter != null ) {
396
+ }
397
+ else if (this .methodParameter != null ) {
389
398
return new TypeDescriptor (this .methodParameter , elementType );
390
399
}
391
400
else if (this .field != null ) {
392
401
return new TypeDescriptor (this .field , elementType );
393
- } else {
402
+ }
403
+ else {
394
404
return TypeDescriptor .valueOf (elementType );
395
405
}
396
406
}
@@ -403,12 +413,16 @@ public boolean equals(Object obj) {
403
413
if (this == td ) {
404
414
return true ;
405
415
}
406
- boolean annotatedTypeEquals = getType ().equals (td .getType ()) && ObjectUtils .nullSafeEquals (getAnnotations (), td .getAnnotations ());
416
+ boolean annotatedTypeEquals =
417
+ getType ().equals (td .getType ()) && ObjectUtils .nullSafeEquals (getAnnotations (), td .getAnnotations ());
407
418
if (isCollection ()) {
408
419
return annotatedTypeEquals && ObjectUtils .nullSafeEquals (getElementType (), td .getElementType ());
409
- } else if (isMap ()) {
410
- return annotatedTypeEquals && ObjectUtils .nullSafeEquals (getMapKeyType (), td .getMapKeyType ()) && ObjectUtils .nullSafeEquals (getMapValueType (), td .getMapValueType ());
411
- } else {
420
+ }
421
+ else if (isMap ()) {
422
+ return annotatedTypeEquals && ObjectUtils .nullSafeEquals (getMapKeyType (), td .getMapKeyType ()) &&
423
+ ObjectUtils .nullSafeEquals (getMapValueType (), td .getMapValueType ());
424
+ }
425
+ else {
412
426
return annotatedTypeEquals ;
413
427
}
414
428
}
@@ -439,8 +453,11 @@ public String toString() {
439
453
if (isMap ()) {
440
454
Class <?> mapKeyType = getMapKeyType ();
441
455
Class <?> valueKeyType = getMapValueType ();
442
- builder .append ("<" ).append (mapKeyType != null ? ClassUtils .getQualifiedName (mapKeyType ) : "?" ).append (", " ).append (valueKeyType != null ? ClassUtils .getQualifiedName (valueKeyType ) : "?" ).append (">" );
443
- } else if (isCollection ()) {
456
+ builder .append ("<" ).append (mapKeyType != null ? ClassUtils .getQualifiedName (mapKeyType ) : "?" );
457
+ builder .append (", " ).append (valueKeyType != null ? ClassUtils .getQualifiedName (valueKeyType ) : "?" );
458
+ builder .append (">" );
459
+ }
460
+ else if (isCollection ()) {
444
461
Class <?> elementType = getElementType ();
445
462
builder .append ("<" ).append (elementType != null ? ClassUtils .getQualifiedName (elementType ) : "?" ).append (">" );
446
463
}
@@ -481,7 +498,7 @@ else if (this.value instanceof Collection) {
481
498
}
482
499
}
483
500
}
484
- return type != null ? GenericCollectionTypeResolver .getCollectionType ((Class <? extends Collection >) this .type ) : null ;
501
+ return ( this . type != null ? GenericCollectionTypeResolver .getCollectionType ((Class <? extends Collection >) this .type ) : null ) ;
485
502
}
486
503
487
504
@ SuppressWarnings ("unchecked" )
@@ -501,7 +518,7 @@ else if (this.value instanceof Map<?, ?>) {
501
518
}
502
519
}
503
520
}
504
- return type != null && isMap () ? GenericCollectionTypeResolver .getMapKeyType ((Class <? extends Map >) this .type ) : null ;
521
+ return ( this . type != null && isMap () ? GenericCollectionTypeResolver .getMapKeyType ((Class <? extends Map >) this .type ) : null ) ;
505
522
}
506
523
507
524
@ SuppressWarnings ("unchecked" )
@@ -521,7 +538,7 @@ else if (this.value instanceof Map<?, ?>) {
521
538
}
522
539
}
523
540
}
524
- return isMap () && type != null ? GenericCollectionTypeResolver .getMapValueType ((Class <? extends Map >) this .type ) : null ;
541
+ return ( isMap () && this . type != null ? GenericCollectionTypeResolver .getMapValueType ((Class <? extends Map >) this .type ) : null ) ;
525
542
}
526
543
527
544
private Annotation [] resolveAnnotations () {
@@ -540,34 +557,38 @@ else if (this.methodParameter != null) {
540
557
return EMPTY_ANNOTATION_ARRAY ;
541
558
}
542
559
}
543
-
544
- /**
545
- * Internal constructor for a NULL descriptor.
546
- */
547
- private TypeDescriptor () {
548
- }
560
+
561
+
562
+ // static factory methods
549
563
550
564
/**
551
- * Create a new descriptor for the type of the given value.
552
- * <p>Use this constructor when a conversion point comes from a source such as a Map or
553
- * Collection, where no additional context is available but elements can be introspected.
554
- * @param type the actual type to wrap
565
+ * Create a new type descriptor for the class of the given object.
566
+ * @param object the object
567
+ * @return the type descriptor
555
568
*/
556
- private TypeDescriptor (Object value ) {
557
- Assert .notNull (value , "Value must not be null" );
558
- this .value = value ;
559
- this .type = value .getClass ();
569
+ public static TypeDescriptor forObject (Object object ) {
570
+ if (object == null ) {
571
+ return NULL ;
572
+ }
573
+ else if (object instanceof Collection <?> || object instanceof Map <?, ?>) {
574
+ return new TypeDescriptor (object );
575
+ }
576
+ else {
577
+ return valueOf (object .getClass ());
578
+ }
560
579
}
561
580
562
581
/**
563
- * Create a new descriptor for the given type.
564
- * <p>Use this constructor when a conversion point comes from a plain source type,
565
- * where no additional context is available.
566
- * @param type the actual type to wrap
582
+ * Create a new type descriptor for the given class.
583
+ * @param type the class
584
+ * @return the type descriptor
567
585
*/
568
- private TypeDescriptor (Class <?> type ) {
569
- Assert .notNull (type , "Type must not be null" );
570
- this .type = type ;
586
+ public static TypeDescriptor valueOf (Class <?> type ) {
587
+ if (type == null ) {
588
+ return TypeDescriptor .NULL ;
589
+ }
590
+ TypeDescriptor desc = typeDescriptorCache .get (type );
591
+ return (desc != null ? desc : new TypeDescriptor (type ));
571
592
}
572
593
573
594
}
0 commit comments