You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* If this type is a {@link Collection} or array, returns the underlying element type.
319
-
* Returns <code>null</code> if this type is neither an array or collection.
320
319
* Returns Object.class if this type is a collection and the element type was not explicitly declared.
321
320
* @return the map element type, or <code>null</code> if not a collection or array.
321
+
* @throws IllegalStateException if this descriptor is not for a java.util.Collection or Array
322
322
*/
323
323
publicClass<?> getElementType() {
324
324
returngetElementTypeDescriptor().getType();
325
325
}
326
326
327
327
/**
328
328
* The collection or array element type as a type descriptor.
329
-
* Returns {@link TypeDescriptor#NULL} if this type is not a collection or an array.
330
329
* Returns TypeDescriptor.valueOf(Object.class) if this type is a collection and the element type is not explicitly declared.
330
+
* @throws IllegalStateException if this descriptor is not for a java.util.Collection or Array
331
331
*/
332
332
publicTypeDescriptorgetElementTypeDescriptor() {
333
+
if (!isCollection() && !isArray()) {
334
+
thrownewIllegalStateException("Not a java.util.Collection or Array");
335
+
}
333
336
returnthis.elementType;
334
337
}
335
338
339
+
/**
340
+
* Returns a copy of this type descriptor that has its elementType populated from the specified Collection.
341
+
* This property will be set by calculating the "common element type" of the specified Collection.
342
+
* For example, if the collection contains String elements, the returned TypeDescriptor will have its elementType set to String.
343
+
* This method is designed to be used when converting values read from Collection fields or method return values that are not parameterized e.g. Collection vs. Collection<String>
344
+
* In this scenario the elementType will be Object.class before invoking this method.
345
+
* @param colection the collection to derive the elementType from
346
+
* @return a new TypeDescriptor with the resolved elementType property
347
+
* @throws IllegalArgumentException if this is not a type descriptor for a java.util.Collection.
* If this type is a {@link Map}, returns the underlying key type.
347
-
* Returns <code>null</code> if this type is not map.
348
367
* Returns Object.class if this type is a map and its key type was not explicitly declared.
349
368
* @return the map key type, or <code>null</code> if not a map.
369
+
* @throws IllegalStateException if this descriptor is not for a java.util.Map
350
370
*/
351
371
publicClass<?> getMapKeyType() {
352
372
returngetMapKeyTypeDescriptor().getType();
353
373
}
354
374
355
375
/**
356
376
* The map key type as a type descriptor.
357
-
* Returns {@link TypeDescriptor#NULL} if this type is not a map.
358
377
* Returns TypeDescriptor.valueOf(Object.class) if this type is a map and the key type is not explicitly declared.
378
+
* @throws IllegalStateException if this descriptor is not for a java.util.Map
359
379
*/
360
380
publicTypeDescriptorgetMapKeyTypeDescriptor() {
381
+
if (!isMap()) {
382
+
thrownewIllegalStateException("Not a map");
383
+
}
361
384
returnthis.mapKeyType;
362
385
}
363
386
@@ -366,40 +389,63 @@ public TypeDescriptor getMapKeyTypeDescriptor() {
366
389
* Returns <code>null</code> if this type is not map.
367
390
* Returns Object.class if this type is a map and its value type was not explicitly declared.
368
391
* @return the map value type, or <code>null</code> if not a map.
392
+
* @throws IllegalStateException if this descriptor is not for a java.util.Map
369
393
*/
370
394
publicClass<?> getMapValueType() {
371
395
returngetMapValueTypeDescriptor().getType();
372
396
}
373
397
374
398
/**
375
399
* The map value type as a type descriptor.
376
-
* Returns {@link TypeDescriptor#NULL} if this type is not a map.
377
400
* Returns TypeDescriptor.valueOf(Object.class) if this type is a map and the value type is not explicitly declared.
401
+
* @throws IllegalStateException if this descriptor is not for a java.util.Map
378
402
*/
379
403
publicTypeDescriptorgetMapValueTypeDescriptor() {
404
+
if (!isMap()) {
405
+
thrownewIllegalStateException("Not a map");
406
+
}
380
407
returnthis.mapValueType;
381
408
}
409
+
410
+
/**
411
+
* Returns a copy of this type descriptor that has its mapKeyType and mapValueType properties populated from the specified Map.
412
+
* These properties will be set by calculating the "common element type" of the specified Map's keySet and values collection.
413
+
* For example, if the Map contains String keys and Integer values, the returned TypeDescriptor will have its mapKeyType set to String and its mapValueType to Integer.
414
+
* This method is designed to be used when converting values read from Map fields or method return values that are not parameterized e.g. Map vs. Map<String, Integer>.
415
+
* In this scenario the key and value types will be Object.class before invoking this method.
416
+
* @param map the map to derive key and value types from
417
+
* @return a new TypeDescriptor with the resolved mapKeyType and mapValueType properties
418
+
* @throws IllegalArgumentException if this is not a type descriptor for a java.util.Map.
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -57,10 +57,11 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
+19-22Lines changed: 19 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -167,36 +167,24 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
167
167
logger.debug("Converting value " + StylerUtils.style(source) + " of " + sourceType + " to " + targetType);
168
168
}
169
169
if (sourceType == TypeDescriptor.NULL) {
170
-
Assert.isTrue(source == null, "The value must be null if sourceType == TypeDescriptor.NULL");
if (source != null && !sourceType.getObjectType().isInstance(source)) {
178
+
thrownewIllegalArgumentException("The source to convert from must be an instance of " + sourceType + "; instead it was a " + source.getClass().getName());
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java
+6-7Lines changed: 6 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -48,14 +48,14 @@ public Set<ConvertiblePair> getConvertibleTypes() {
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToArrayConverter.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -56,8 +56,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -32,8 +32,7 @@ public Character convert(String source) {
32
32
}
33
33
if (source.length() > 1) {
34
34
thrownewIllegalArgumentException(
35
-
"Can only convert a [String] with length of 1 to a [Character]; string value '" + source
36
-
+ "' has length of " + source.length());
35
+
"Can only convert a [String] with length of 1 to a [Character]; string value '" + source + "' has length of " + source.length());
0 commit comments