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
* Use this constructor when a conversion point is a field.
81
+
* Create a new type descriptor from a {@link Field}.
82
+
* Use this constructor when source or target conversion point is a field.
83
83
* @param field the field
84
84
*/
85
85
publicTypeDescriptor(Fieldfield) {
86
86
this(newFieldDescriptor(field));
87
87
}
88
88
89
89
/**
90
-
* Create a new type descriptor for a bean property.
91
-
* Use this constructor when a target conversion point is a property on a Java class.
90
+
* Create a new type descriptor from a {@link Property}.
91
+
* Use this constructor when a source or target conversion point is a property on a Java class.
92
92
* @param property the property
93
93
*/
94
94
publicTypeDescriptor(Propertyproperty) {
95
95
this(newBeanPropertyDescriptor(property));
96
96
}
97
97
98
98
/**
99
-
* Create a new type descriptor for the given class.
100
-
* Use this to instruct the conversion system to convert to an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context.
101
-
* Generally prefer use of {@link #forObject(Object)} for constructing source type descriptors for source objects.
99
+
* Create a new type descriptor from the given type.
100
+
* Use this to instruct the conversion system to convert an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context.
101
+
* Generally prefer use of {@link #forObject(Object)} for constructing type descriptors from source objects, as it handles the null object case.
102
102
* @param type the class
103
103
* @return the type descriptor
104
104
*/
@@ -108,9 +108,10 @@ public static TypeDescriptor valueOf(Class<?> type) {
108
108
}
109
109
110
110
/**
111
-
* Create a new type descriptor for a java.util.Collection class.
112
-
* Useful for supporting conversion of source Collection objects to other types.
113
-
* Serves as an alternative to {@link #forObject(Object)} to be used when you cannot rely on Collection element introspection to resolve the element type.
111
+
* Create a new type descriptor from a java.util.Collection type.
112
+
* Useful for converting to typed Collections.
113
+
* For example, a List<String> could be converted to a List<EmailAddress> by converting to a targetType built with this method.
114
+
* The method call to construct such a TypeDescriptor would look something like: collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));
114
115
* @param collectionType the collection type, which must implement {@link Collection}.
115
116
* @param elementType the collection's element type, used to convert collection elements
116
117
* @return the collection type descriptor
@@ -123,9 +124,10 @@ public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor
123
124
}
124
125
125
126
/**
126
-
* Create a new type descriptor for a java.util.Map class.
127
-
* Useful for supporting the conversion of source Map objects to other types.
128
-
* Serves as an alternative to {@link #forObject(Object)} to be used when you cannot rely on Map entry introspection to resolve the key and value type.
127
+
* Create a new type descriptor from a java.util.Map type.
128
+
* Useful for Converting to typed Maps.
129
+
* For example, a Map<String, String> could be converted to a Map<Id, EmailAddress> by converting to a targetType built with this method:
130
+
* The method call to construct such a TypeDescriptor would look something like: map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
129
131
* @param mapType the map type, which must implement {@link Map}.
130
132
* @param keyType the map's key type, used to convert map keys
131
133
* @param valueType the map's value type, used to convert map values
@@ -201,16 +203,19 @@ public static TypeDescriptor forObject(Object source) {
201
203
}
202
204
203
205
/**
204
-
* Determine the declared (non-generic) type of the wrapped parameter/field.
205
-
* @return the declared type, or <code>null</code> if this is {@link TypeDescriptor#NULL}
206
+
* The type of the backing class, method parameter, field, or property described by this TypeDescriptor.
207
+
* Returns primitive types as-is.
208
+
* See {@link #getObjectType()} for a variation of this operation that resolves primitive types to their corresponding Object types if necessary.
209
+
* @return the type, or <code>null</code> if this is {@link TypeDescriptor#NULL}
210
+
* @see #getObjectType()
206
211
*/
207
212
publicClass<?> getType() {
208
213
returntype;
209
214
}
210
215
211
216
/**
212
-
* Determine the declared type of the wrapped parameter/field.
213
-
* Returns the Object wrapper type if the underlying type is a primitive.
217
+
* Variation of {@link #getType()} that accounts for a primitive type by returning its object wrapper type.
218
+
* This is useful for conversion service implementations that wish to normalize to object-based types and not work with primitive types directly.
Copy file name to clipboardExpand all lines: org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
thrownewIllegalArgumentException("The targetType to convert to cannot be null");
@@ -190,6 +191,20 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
190
191
}
191
192
}
192
193
194
+
/**
195
+
* Convenience operation for converting a source object to the specified targetType, where the targetType is a descriptor that provides additional conversion context.
196
+
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and encapsulates the construction of the sourceType descriptor using {@link TypeDescriptor#forObject(Object)}.
197
+
* @param source the source object
198
+
* @param targetType the target type
199
+
* @return the converted value
200
+
* @throws ConversionException if a conversion exception occurred
201
+
* @throws IllegalArgumentException if targetType is null
202
+
* @throws IllegalArgumentException if sourceType is null but source is not null
Copy file name to clipboardExpand all lines: org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java
0 commit comments