Skip to content

Commit d24c131

Browse files
committed
Expose convert(Object, TypeDescriptor) in ConversionService interface
Closes gh-25394
1 parent 0ab9447 commit d24c131

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

spring-core/src/main/java/org/springframework/core/convert/ConversionService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,6 +75,23 @@ public interface ConversionService {
7575
@Nullable
7676
<T> T convert(@Nullable Object source, Class<T> targetType);
7777

78+
/**
79+
* Convert the given {@code source} to the specified {@code targetType}.
80+
* <p>Delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)}
81+
* and encapsulates the construction of the source type descriptor using
82+
* {@link TypeDescriptor#forObject(Object)}.
83+
* @param source the source object
84+
* @param targetType the target type
85+
* @return the converted value
86+
* @throws ConversionException if a conversion exception occurred
87+
* @throws IllegalArgumentException if targetType is {@code null}
88+
* @since 6.1
89+
*/
90+
@Nullable
91+
default Object convert(@Nullable Object source, TypeDescriptor targetType) {
92+
return convert(source, TypeDescriptor.forObject(source), targetType);
93+
}
94+
7895
/**
7996
* Convert the given {@code source} to the specified {@code targetType}.
8097
* The TypeDescriptors provide additional context about the source and target locations

spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
3131

3232
import org.springframework.core.DecoratingProxy;
3333
import org.springframework.core.ResolvableType;
34-
import org.springframework.core.convert.ConversionException;
3534
import org.springframework.core.convert.ConversionFailedException;
3635
import org.springframework.core.convert.ConversionService;
3736
import org.springframework.core.convert.ConverterNotFoundException;
@@ -140,11 +139,7 @@ public boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType) {
140139
@Override
141140
public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
142141
Assert.notNull(targetType, "Target type to convert to cannot be null");
143-
if (sourceType == null) {
144-
return true;
145-
}
146-
GenericConverter converter = getConverter(sourceType, targetType);
147-
return (converter != null);
142+
return (sourceType == null || getConverter(sourceType, targetType) != null);
148143
}
149144

150145
/**
@@ -160,15 +155,11 @@ public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor ta
160155
*/
161156
public boolean canBypassConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
162157
Assert.notNull(targetType, "Target type to convert to cannot be null");
163-
if (sourceType == null) {
164-
return true;
165-
}
166-
GenericConverter converter = getConverter(sourceType, targetType);
167-
return (converter == NO_OP_CONVERTER);
158+
return (sourceType == null || getConverter(sourceType, targetType) == NO_OP_CONVERTER);
168159
}
169160

170-
@Override
171161
@SuppressWarnings("unchecked")
162+
@Override
172163
@Nullable
173164
public <T> T convert(@Nullable Object source, Class<T> targetType) {
174165
Assert.notNull(targetType, "Target type to convert to cannot be null");
@@ -195,24 +186,6 @@ public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceTy
195186
return handleConverterNotFound(source, sourceType, targetType);
196187
}
197188

198-
/**
199-
* Convenience operation for converting a source object to the specified targetType,
200-
* where the target type is a descriptor that provides additional conversion context.
201-
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and
202-
* encapsulates the construction of the source type descriptor using
203-
* {@link TypeDescriptor#forObject(Object)}.
204-
* @param source the source object
205-
* @param targetType the target type
206-
* @return the converted value
207-
* @throws ConversionException if a conversion exception occurred
208-
* @throws IllegalArgumentException if targetType is {@code null},
209-
* or sourceType is {@code null} but source is not {@code null}
210-
*/
211-
@Nullable
212-
public Object convert(@Nullable Object source, TypeDescriptor targetType) {
213-
return convert(source, TypeDescriptor.forObject(source), targetType);
214-
}
215-
216189
@Override
217190
public String toString() {
218191
return this.converters.toString();

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
106106
}
107107

108108
if (parameter != nestedParameter || !ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
109-
arg = this.conversionService.convert(arg, TypeDescriptor.forObject(arg), new TypeDescriptor(parameter));
109+
arg = this.conversionService.convert(arg, new TypeDescriptor(parameter));
110110
// Check for null value after conversion of incoming argument value
111111
if (arg == null) {
112112
if (namedValueInfo.defaultValue != null) {

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
114114
}
115115

116116
if (parameter != nestedParameter || !ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
117-
arg = this.conversionService.convert(arg, TypeDescriptor.forObject(arg), new TypeDescriptor(parameter));
117+
arg = this.conversionService.convert(arg, new TypeDescriptor(parameter));
118118
// Check for null value after conversion of incoming argument value
119119
if (arg == null) {
120120
if (namedValueInfo.defaultValue != null) {

0 commit comments

Comments
 (0)