@@ -81,8 +81,10 @@ public class GenericConversionService implements ConfigurableConversionService {
81
81
82
82
public void addConverter (Converter <?, ?> converter ) {
83
83
GenericConverter .ConvertiblePair typeInfo = getRequiredTypeInfo (converter , Converter .class );
84
- Assert .notNull (typeInfo , "Unable to the determine sourceType <S> and targetType " +
85
- "<T> which your Converter<S, T> converts between; declare these generic types." );
84
+ if (typeInfo == null ) {
85
+ throw new IllegalArgumentException ("Unable to determine source type <S> and target type <T> for your " +
86
+ "Converter [" + converter .getClass ().getName () + "]; does the class parameterize those types?" );
87
+ }
86
88
addConverter (new ConverterAdapter (converter , typeInfo ));
87
89
}
88
90
@@ -96,14 +98,13 @@ public void addConverter(GenericConverter converter) {
96
98
invalidateCache ();
97
99
}
98
100
99
- public void addConverterFactory (ConverterFactory <?, ?> converterFactory ) {
100
- GenericConverter .ConvertiblePair typeInfo = getRequiredTypeInfo (converterFactory , ConverterFactory .class );
101
+ public void addConverterFactory (ConverterFactory <?, ?> factory ) {
102
+ GenericConverter .ConvertiblePair typeInfo = getRequiredTypeInfo (factory , ConverterFactory .class );
101
103
if (typeInfo == null ) {
102
- throw new IllegalArgumentException ("Unable to the determine sourceType <S> and " +
103
- "targetRangeType R which your ConverterFactory<S, R> converts between; " +
104
- "declare these generic types." );
104
+ throw new IllegalArgumentException ("Unable to determine source type <S> and target type <T> for your " +
105
+ "ConverterFactory [" + factory .getClass ().getName () + "]; does the class parameterize those types?" );
105
106
}
106
- addConverter (new ConverterFactoryAdapter (converterFactory , typeInfo ));
107
+ addConverter (new ConverterFactoryAdapter (factory , typeInfo ));
107
108
}
108
109
109
110
public void removeConvertible (Class <?> sourceType , Class <?> targetType ) {
@@ -129,17 +130,18 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
129
130
}
130
131
131
132
/**
132
- * Returns true if conversion between the sourceType and targetType can be bypassed.
133
- * More precisely this method will return true if objects of sourceType can be
133
+ * Return whether conversion between the sourceType and targetType can be bypassed.
134
+ * <p> More precisely, this method will return true if objects of sourceType can be
134
135
* converted to the targetType by returning the source object unchanged.
135
- * @param sourceType context about the source type to convert from (may be null if source is null)
136
+ * @param sourceType context about the source type to convert from
137
+ * (may be {@code null} if source is {@code null})
136
138
* @param targetType context about the target type to convert to (required)
137
- * @return true if conversion can be bypassed
138
- * @throws IllegalArgumentException if targetType is null
139
+ * @return {@code true} if conversion can be bypassed; {@code false} otherwise
140
+ * @throws IllegalArgumentException if targetType is {@code null}
139
141
* @since 3.2
140
142
*/
141
143
public boolean canBypassConvert (TypeDescriptor sourceType , TypeDescriptor targetType ) {
142
- Assert .notNull (targetType , "The targetType to convert to cannot be null" );
144
+ Assert .notNull (targetType , "targetType to convert to cannot be null" );
143
145
if (sourceType == null ) {
144
146
return true ;
145
147
}
@@ -149,18 +151,18 @@ public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor target
149
151
150
152
@ SuppressWarnings ("unchecked" )
151
153
public <T > T convert (Object source , Class <T > targetType ) {
152
- Assert .notNull (targetType ,"The targetType to convert to cannot be null" );
154
+ Assert .notNull (targetType , " targetType to convert to cannot be null" );
153
155
return (T ) convert (source , TypeDescriptor .forObject (source ), TypeDescriptor .valueOf (targetType ));
154
156
}
155
157
156
158
public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
157
- Assert .notNull (targetType ,"The targetType to convert to cannot be null" );
159
+ Assert .notNull (targetType , " targetType to convert to cannot be null" );
158
160
if (sourceType == null ) {
159
- Assert .isTrue (source == null , "The source must be [null] if sourceType == [null]" );
160
- return handleResult (sourceType , targetType , convertNullSource (sourceType , targetType ));
161
+ Assert .isTrue (source == null , "source must be [null] if sourceType == [null]" );
162
+ return handleResult (null , targetType , convertNullSource (null , targetType ));
161
163
}
162
164
if (source != null && !sourceType .getObjectType ().isInstance (source )) {
163
- throw new IllegalArgumentException ("The source to convert from must be an instance of " +
165
+ throw new IllegalArgumentException ("source to convert from must be an instance of " +
164
166
sourceType + "; instead it was a " + source .getClass ().getName ());
165
167
}
166
168
GenericConverter converter = getConverter (sourceType , targetType );
@@ -198,7 +200,7 @@ public String toString() {
198
200
199
201
/**
200
202
* Template method to convert a null source.
201
- * <p>Default implementation returns {@code null}.
203
+ * <p>The default implementation returns {@code null}.
202
204
* Subclasses may override to return custom null objects for specific target types.
203
205
* @param sourceType the sourceType to convert from
204
206
* @param targetType the targetType to convert to
@@ -213,11 +215,10 @@ protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor tar
213
215
* First queries this ConversionService's converter cache.
214
216
* On a cache miss, then performs an exhaustive search for a matching converter.
215
217
* If no converter matches, returns the default converter.
216
- * Subclasses may override.
217
218
* @param sourceType the source type to convert from
218
219
* @param targetType the target type to convert to
219
- * @return the generic converter that will perform the conversion, or {@code null} if
220
- * no suitable converter was found
220
+ * @return the generic converter that will perform the conversion,
221
+ * or {@code null} if no suitable converter was found
221
222
* @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
222
223
*/
223
224
protected GenericConverter getConverter (TypeDescriptor sourceType , TypeDescriptor targetType ) {
@@ -243,9 +244,8 @@ protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescripto
243
244
244
245
/**
245
246
* Return the default converter if no converter is found for the given sourceType/targetType pair.
246
- * Returns a NO_OP Converter if the sourceType is assignable to the targetType.
247
+ * <p> Returns a NO_OP Converter if the sourceType is assignable to the targetType.
247
248
* Returns {@code null} otherwise, indicating no suitable converter could be found.
248
- * Subclasses may override.
249
249
* @param sourceType the source type to convert from
250
250
* @param targetType the target type to convert to
251
251
* @return the default generic converter that will perform the conversion
0 commit comments