Skip to content

Commit f48550a

Browse files
committed
Fix package tangle in binder
Update the `BinderConversionService` so that it no longer references classes from `org.springframework.boot.context.properties.bind`. See gh-10592
1 parent 1bd44d8 commit f48550a

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,8 @@ private <T> T handleBindError(ConfigurationPropertyName name, Bindable<T> target
227227
}
228228

229229
private <T> T convert(Object value, Bindable<T> target) {
230-
if (value == null) {
231-
return null;
232-
}
233-
return this.conversionService.convert(value, target);
230+
return ResolvableTypeDescriptor.forBindable(target)
231+
.convert(this.conversionService, value);
234232
}
235233

236234
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target,
@@ -287,7 +285,7 @@ private <T> Object bindProperty(ConfigurationPropertyName name, Bindable<T> targ
287285
context.setConfigurationProperty(property);
288286
Object result = property.getValue();
289287
result = this.placeholdersResolver.resolvePlaceholders(result);
290-
result = this.conversionService.convert(result, target);
288+
result = convert(result, target);
291289
return result;
292290
}
293291

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/IndexedElementsBinder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,10 @@ private void assertNoUnboundChildren(
122122
}
123123
}
124124

125-
@SuppressWarnings("unchecked")
126125
private <C> C convert(Object value, ResolvableType type) {
127126
value = getContext().getPlaceholdersResolver().resolvePlaceholders(value);
128127
BinderConversionService conversionService = getContext().getConversionService();
129-
return (C) conversionService.convert(value, type);
128+
return ResolvableTypeDescriptor.forType(type).convert(conversionService, value);
130129
}
131130

132131
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Map;
2121
import java.util.Properties;
2222

23-
import org.springframework.boot.context.properties.bind.convert.BinderConversionService;
2423
import org.springframework.boot.context.properties.source.ConfigurationProperty;
2524
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
2625
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.Form;
@@ -102,8 +101,7 @@ public void bindEntries(ConfigurationPropertySource source,
102101
for (ConfigurationPropertyName name : (IterableConfigurationPropertySource) source) {
103102
Bindable<?> valueBindable = getValueBindable(name);
104103
ConfigurationPropertyName entryName = getEntryName(source, name);
105-
Object key = getContext().getConversionService()
106-
.convert(getKeyName(entryName), this.keyType);
104+
Object key = convert(getKeyName(entryName), this.keyType);
107105
map.computeIfAbsent(key,
108106
(k) -> this.elementBinder.bind(entryName, valueBindable));
109107
}
@@ -159,9 +157,17 @@ private boolean isScalarValue(ConfigurationPropertySource source,
159157
}
160158
Object value = property.getValue();
161159
value = getContext().getPlaceholdersResolver().resolvePlaceholders(value);
162-
BinderConversionService conversionService = getContext()
163-
.getConversionService();
164-
return conversionService.canConvert(value, this.valueType);
160+
return canConvert(value, this.valueType);
161+
}
162+
163+
private boolean canConvert(Object source, ResolvableType targetType) {
164+
return ResolvableTypeDescriptor.forType(targetType)
165+
.canConvert(getContext().getConversionService(), source);
166+
}
167+
168+
private Object convert(Object source, ResolvableType targetType) {
169+
return ResolvableTypeDescriptor.forType(targetType)
170+
.convert(getContext().getConversionService(), source);
165171
}
166172

167173
private String getKeyName(ConfigurationPropertyName name) {
Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.context.properties.bind.convert;
17+
package org.springframework.boot.context.properties.bind;
1818

1919
import java.lang.annotation.Annotation;
2020

21-
import org.springframework.boot.context.properties.bind.Bindable;
2221
import org.springframework.core.ResolvableType;
22+
import org.springframework.core.convert.ConversionException;
23+
import org.springframework.core.convert.ConversionService;
2324
import org.springframework.core.convert.TypeDescriptor;
2425

2526
/**
@@ -35,12 +36,40 @@ private ResolvableTypeDescriptor(ResolvableType resolvableType,
3536
super(resolvableType, null, annotations);
3637
}
3738

39+
/**
40+
* Determine if the specified source object can be converted to this type.
41+
* @param conversionService the backing conversion service
42+
* @param source the source to check
43+
* @return {@code true} if conversion can be performed
44+
*/
45+
public boolean canConvert(ConversionService conversionService, Object source) {
46+
TypeDescriptor sourceType = TypeDescriptor.forObject(source);
47+
return conversionService.canConvert(sourceType, this);
48+
}
49+
50+
/**
51+
* Convert the given source object into this type.
52+
* @param conversionService the source conversion service
53+
* @param value the value to convert
54+
* @param <T> the target type
55+
* @return the converted value
56+
* @throws ConversionException if a conversion exception occurred
57+
*/
58+
@SuppressWarnings("unchecked")
59+
public <T> T convert(ConversionService conversionService, Object value) {
60+
if (value == null) {
61+
return null;
62+
}
63+
TypeDescriptor sourceType = TypeDescriptor.forObject(value);
64+
return (T) conversionService.convert(value, sourceType, this);
65+
}
66+
3867
/**
3968
* Create a {@link TypeDescriptor} for the specified {@link Bindable}.
4069
* @param bindable the bindable
4170
* @return the type descriptor
4271
*/
43-
public static TypeDescriptor forBindable(Bindable<?> bindable) {
72+
public static ResolvableTypeDescriptor forBindable(Bindable<?> bindable) {
4473
return forType(bindable.getType(), bindable.getAnnotations());
4574
}
4675

@@ -50,7 +79,8 @@ public static TypeDescriptor forBindable(Bindable<?> bindable) {
5079
* @param annotations the annotations to include
5180
* @return the type descriptor
5281
*/
53-
public static TypeDescriptor forType(ResolvableType type, Annotation... annotations) {
82+
public static ResolvableTypeDescriptor forType(ResolvableType type,
83+
Annotation... annotations) {
5484
return new ResolvableTypeDescriptor(type, annotations);
5585
}
5686

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/convert/BinderConversionService.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
import java.util.function.Function;
2020

21-
import org.springframework.boot.context.properties.bind.Bindable;
2221
import org.springframework.boot.context.properties.bind.Binder;
23-
import org.springframework.core.ResolvableType;
2422
import org.springframework.core.convert.ConversionException;
2523
import org.springframework.core.convert.ConversionService;
2624
import org.springframework.core.convert.ConverterNotFoundException;
@@ -54,19 +52,6 @@ public BinderConversionService(ConversionService conversionService) {
5452
this.additionalConversionService = createAdditionalConversionService();
5553
}
5654

57-
/**
58-
* Return {@code true} if the given source object can be converted to the
59-
* {@code targetType}.
60-
* @param source the source object
61-
* @param targetType the target type to convert to (required)
62-
* @return {@code true} if a conversion can be performed, {@code false} if not
63-
* @throws IllegalArgumentException if {@code targetType} is {@code null}
64-
*/
65-
public boolean canConvert(Object source, ResolvableType targetType) {
66-
TypeDescriptor sourceType = TypeDescriptor.forObject(source);
67-
return canConvert(sourceType, ResolvableTypeDescriptor.forType(targetType));
68-
}
69-
7055
@Override
7156
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
7257
return (this.conversionService != null
@@ -81,20 +66,6 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
8166
|| this.additionalConversionService.canConvert(sourceType, targetType);
8267
}
8368

84-
@SuppressWarnings("unchecked")
85-
public <T> T convert(Object value, ResolvableType type) {
86-
TypeDescriptor sourceType = TypeDescriptor.forObject(value);
87-
TypeDescriptor targetType = ResolvableTypeDescriptor.forType(type);
88-
return (T) convert(value, sourceType, targetType);
89-
}
90-
91-
@SuppressWarnings("unchecked")
92-
public <T> T convert(Object value, Bindable<T> bindable) {
93-
TypeDescriptor sourceType = TypeDescriptor.forObject(value);
94-
TypeDescriptor targetType = ResolvableTypeDescriptor.forBindable(bindable);
95-
return (T) convert(value, sourceType, targetType);
96-
}
97-
9869
@Override
9970
public <T> T convert(Object source, Class<T> targetType) {
10071
return callConversionService((c) -> c.convert(source, targetType));
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.context.properties.bind.convert;
17+
package org.springframework.boot.context.properties.bind;
1818

1919
import java.lang.annotation.Annotation;
2020
import java.util.List;
2121

2222
import org.junit.Test;
2323

24-
import org.springframework.boot.context.properties.bind.Bindable;
2524
import org.springframework.core.ResolvableType;
2625
import org.springframework.core.annotation.AnnotationUtils;
2726
import org.springframework.core.convert.TypeDescriptor;

0 commit comments

Comments
 (0)