Skip to content

Commit 3e4da3c

Browse files
committed
Polish
1 parent c7ed5c3 commit 3e4da3c

File tree

9 files changed

+139
-134
lines changed

9 files changed

+139
-134
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchHealthIndicatorAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/redis/RedisReactiveHealthIndicatorConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.

spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/context/properties/bind/AppSystemPropertiesTests.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,46 @@ public class AppSystemPropertiesTests {
4141

4242
@Test
4343
public void bindWithDefaultUnit() {
44-
this.contextRunner.withPropertyValues("app.system.session-timeout=40",
45-
"app.system.read-timeout=5000").run(assertBinding(p -> {
46-
assertThat(p.getSessionTimeout()).isEqualTo(Duration.ofSeconds(40));
47-
assertThat(p.getReadTimeout()).isEqualTo(Duration.ofMillis(5000));
48-
}));
44+
this.contextRunner
45+
.withPropertyValues("app.system.session-timeout=40",
46+
"app.system.read-timeout=5000")
47+
.run(assertBinding((properties) -> {
48+
assertThat(properties.getSessionTimeout())
49+
.isEqualTo(Duration.ofSeconds(40));
50+
assertThat(properties.getReadTimeout())
51+
.isEqualTo(Duration.ofMillis(5000));
52+
}));
4953
}
5054

5155
@Test
5256
public void bindWithExplicitUnit() {
5357
this.contextRunner.withPropertyValues("app.system.session-timeout=1h",
54-
"app.system.read-timeout=5s").run(assertBinding(p -> {
55-
assertThat(p.getSessionTimeout()).isEqualTo(Duration.ofMinutes(60));
56-
assertThat(p.getReadTimeout()).isEqualTo(Duration.ofMillis(5000));
57-
}));
58+
"app.system.read-timeout=5s").run(assertBinding((properties) -> {
59+
assertThat(properties.getSessionTimeout())
60+
.isEqualTo(Duration.ofMinutes(60));
61+
assertThat(properties.getReadTimeout())
62+
.isEqualTo(Duration.ofMillis(5000));
63+
}));
5864
}
5965

6066
@Test
6167
public void bindWithIso8601Format() {
62-
this.contextRunner.withPropertyValues("app.system.session-timeout=PT15S",
63-
"app.system.read-timeout=PT0.5S").run(assertBinding(p -> {
64-
assertThat(p.getSessionTimeout()).isEqualTo(Duration.ofSeconds(15));
65-
assertThat(p.getReadTimeout()).isEqualTo(Duration.ofMillis(500));
66-
}));
68+
this.contextRunner
69+
.withPropertyValues("app.system.session-timeout=PT15S",
70+
"app.system.read-timeout=PT0.5S")
71+
.run(assertBinding((properties) -> {
72+
assertThat(properties.getSessionTimeout())
73+
.isEqualTo(Duration.ofSeconds(15));
74+
assertThat(properties.getReadTimeout())
75+
.isEqualTo(Duration.ofMillis(500));
76+
}));
6777
}
6878

6979
private ContextConsumer<AssertableApplicationContext> assertBinding(
70-
Consumer<AppSystemProperties> appSystemProperties) {
80+
Consumer<AppSystemProperties> properties) {
7181
return (context) -> {
7282
assertThat(context).hasSingleBean(AppSystemProperties.class);
73-
appSystemProperties.accept(context.getBean(AppSystemProperties.class));
83+
properties.accept(context.getBean(AppSystemProperties.class));
7484
};
7585
}
7686

spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/web/client/SampleWebClientConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
*/
3636
@SpringBootConfiguration
3737
@ImportAutoConfiguration({ ServletWebServerFactoryAutoConfiguration.class,
38-
DispatcherServletAutoConfiguration.class,
39-
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
38+
DispatcherServletAutoConfiguration.class, JacksonAutoConfiguration.class,
39+
HttpMessageConvertersAutoConfiguration.class })
4040
class SampleWebClientConfiguration {
4141

42-
4342
@RestController
4443
private static class ExampleController {
4544

spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/web/client/SampleWebClientTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ static class Config {
5656

5757
@Bean
5858
public RestTemplateBuilder restTemplateBuilder() {
59-
return new RestTemplateBuilder()
60-
.setConnectTimeout(1000)
61-
.setReadTimeout(1000);
59+
return new RestTemplateBuilder().setConnectTimeout(1000).setReadTimeout(1000);
6260
}
6361

6462
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.

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

Lines changed: 92 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import java.util.Map;
2727
import java.util.Set;
2828
import java.util.function.Consumer;
29-
import java.util.function.Function;
30-
import java.util.function.Predicate;
3129

3230
import org.springframework.beans.BeanUtils;
3331
import org.springframework.beans.PropertyEditorRegistry;
3432
import org.springframework.beans.SimpleTypeConverter;
3533
import org.springframework.beans.propertyeditors.FileEditor;
3634
import org.springframework.boot.convert.ApplicationConversionService;
3735
import org.springframework.core.ResolvableType;
36+
import org.springframework.core.convert.ConversionException;
3837
import org.springframework.core.convert.ConversionService;
3938
import org.springframework.core.convert.TypeDescriptor;
4039
import org.springframework.core.convert.converter.ConditionalGenericConverter;
@@ -62,16 +61,27 @@ class BindConverter {
6261
BindConverter(ConversionService conversionService,
6362
Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
6463
Assert.notNull(conversionService, "ConversionService must not be null");
65-
this.conversionService = new CompositeConversionService(
66-
new TypeConverterConversionService(propertyEditorInitializer),
67-
conversionService);
64+
List<ConversionService> conversionServices = getConversionServices(
65+
conversionService, propertyEditorInitializer);
66+
this.conversionService = new CompositeConversionService(conversionServices);
67+
}
68+
69+
private List<ConversionService> getConversionServices(
70+
ConversionService conversionService,
71+
Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
72+
List<ConversionService> services = new ArrayList<>();
73+
services.add(new TypeConverterConversionService(propertyEditorInitializer));
74+
services.add(conversionService);
75+
if (!(conversionService instanceof ApplicationConversionService)) {
76+
services.add(ApplicationConversionService.getSharedInstance());
77+
}
78+
return services;
6879
}
6980

7081
public boolean canConvert(Object value, ResolvableType type,
7182
Annotation... annotations) {
72-
TypeDescriptor sourceType = TypeDescriptor.forObject(value);
73-
TypeDescriptor targetType = new ResolvableTypeDescriptor(type, annotations);
74-
return this.conversionService.canConvert(sourceType, targetType);
83+
return this.conversionService.canConvert(TypeDescriptor.forObject(value),
84+
new ResolvableTypeDescriptor(type, annotations));
7585
}
7686

7787
public <T> T convert(Object result, Bindable<T> target) {
@@ -83,9 +93,8 @@ public <T> T convert(Object value, ResolvableType type, Annotation... annotation
8393
if (value == null) {
8494
return null;
8595
}
86-
TypeDescriptor sourceType = TypeDescriptor.forObject(value);
87-
TypeDescriptor targetType = new ResolvableTypeDescriptor(type, annotations);
88-
return (T) this.conversionService.convert(value, sourceType, targetType);
96+
return (T) this.conversionService.convert(value, TypeDescriptor.forObject(value),
97+
new ResolvableTypeDescriptor(type, annotations));
8998
}
9099

91100
/**
@@ -100,22 +109,81 @@ private static class ResolvableTypeDescriptor extends TypeDescriptor {
100109

101110
}
102111

112+
/**
113+
* Composite {@link ConversionService} used to call multiple services
114+
*/
115+
static class CompositeConversionService implements ConversionService {
116+
117+
private final List<ConversionService> delegates;
118+
119+
CompositeConversionService(List<ConversionService> delegates) {
120+
this.delegates = delegates;
121+
}
122+
123+
@Override
124+
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
125+
Assert.notNull(targetType, "Target type to convert to cannot be null");
126+
return canConvert(
127+
(sourceType != null ? TypeDescriptor.valueOf(sourceType) : null),
128+
TypeDescriptor.valueOf(targetType));
129+
}
130+
131+
@Override
132+
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
133+
for (ConversionService service : this.delegates) {
134+
if (service.canConvert(sourceType, targetType)) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
140+
141+
@Override
142+
@SuppressWarnings("unchecked")
143+
public <T> T convert(Object source, Class<T> targetType) {
144+
Assert.notNull(targetType, "Target type to convert to cannot be null");
145+
return (T) convert(source, TypeDescriptor.forObject(source),
146+
TypeDescriptor.valueOf(targetType));
147+
}
148+
149+
@Override
150+
public Object convert(Object source, TypeDescriptor sourceType,
151+
TypeDescriptor targetType) {
152+
for (int i = 0; i < this.delegates.size() - 1; i++) {
153+
try {
154+
ConversionService delegate = this.delegates.get(i);
155+
if (delegate.canConvert(sourceType, targetType)) {
156+
return delegate.convert(source, sourceType, targetType);
157+
}
158+
}
159+
catch (ConversionException ex) {
160+
}
161+
}
162+
return this.delegates.get(this.delegates.size() - 1).convert(source,
163+
sourceType, targetType);
164+
}
165+
166+
}
167+
103168
/**
104169
* A {@link ConversionService} implementation that delegates to a
105170
* {@link SimpleTypeConverter}. Allows {@link PropertyEditor} based conversion for
106171
* simple types, arrays and collections.
107172
*/
108173
private static class TypeConverterConversionService extends GenericConversionService {
109174

110-
private SimpleTypeConverter typeConverter;
111-
112175
TypeConverterConversionService(Consumer<PropertyEditorRegistry> initializer) {
113-
this.typeConverter = new SimpleTypeConverter();
176+
addConverter(new TypeConverterConverter(createTypeConverter(initializer)));
177+
ApplicationConversionService.addDelimitedStringConverters(this);
178+
}
179+
180+
private SimpleTypeConverter createTypeConverter(
181+
Consumer<PropertyEditorRegistry> initializer) {
182+
SimpleTypeConverter typeConverter = new SimpleTypeConverter();
114183
if (initializer != null) {
115-
initializer.accept(this.typeConverter);
184+
initializer.accept(typeConverter);
116185
}
117-
addConverter(new TypeConverterConverter(this.typeConverter));
118-
ApplicationConversionService.addDelimitedStringConverters(this);
186+
return typeConverter;
119187
}
120188

121189
@Override
@@ -135,9 +203,9 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
135203
*/
136204
private static class TypeConverterConverter implements ConditionalGenericConverter {
137205

138-
private SimpleTypeConverter typeConverter;
206+
private final SimpleTypeConverter typeConverter;
139207

140-
TypeConverterConverter(SimpleTypeConverter typeConverter) {
208+
public TypeConverterConverter(SimpleTypeConverter typeConverter) {
141209
this.typeConverter = typeConverter;
142210
}
143211

@@ -154,18 +222,20 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
154222
@Override
155223
public Object convert(Object source, TypeDescriptor sourceType,
156224
TypeDescriptor targetType) {
157-
return this.typeConverter.convertIfNecessary(source, targetType.getType());
225+
SimpleTypeConverter typeConverter = this.typeConverter;
226+
return typeConverter.convertIfNecessary(source, targetType.getType());
158227
}
159228

160229
private PropertyEditor getPropertyEditor(Class<?> type) {
230+
SimpleTypeConverter typeConverter = this.typeConverter;
161231
if (type == null || type == Object.class
162232
|| Collection.class.isAssignableFrom(type)
163233
|| Map.class.isAssignableFrom(type)) {
164234
return null;
165235
}
166-
PropertyEditor editor = this.typeConverter.getDefaultEditor(type);
236+
PropertyEditor editor = typeConverter.getDefaultEditor(type);
167237
if (editor == null) {
168-
editor = this.typeConverter.findCustomEditor(type, null);
238+
editor = typeConverter.findCustomEditor(type, null);
169239
}
170240
if (editor == null && String.class != type) {
171241
editor = BeanUtils.findEditorByConvention(type);
@@ -178,66 +248,4 @@ private PropertyEditor getPropertyEditor(Class<?> type) {
178248

179249
}
180250

181-
private static final class CompositeConversionService implements ConversionService {
182-
183-
private final List<ConversionService> delegates;
184-
185-
private CompositeConversionService(
186-
TypeConverterConversionService typeConverterConversionService,
187-
ConversionService conversionService) {
188-
List<ConversionService> delegates = new ArrayList<ConversionService>();
189-
delegates.add(typeConverterConversionService);
190-
delegates.add(conversionService);
191-
if (!(conversionService instanceof ApplicationConversionService)) {
192-
delegates.add(ApplicationConversionService.getSharedInstance());
193-
}
194-
this.delegates = delegates;
195-
}
196-
197-
@Override
198-
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
199-
return canConvert((delegate) -> delegate.canConvert(sourceType, targetType));
200-
}
201-
202-
@Override
203-
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
204-
return canConvert((delegate) -> delegate.canConvert(sourceType, targetType));
205-
}
206-
207-
private boolean canConvert(Predicate<ConversionService> canConvert) {
208-
for (ConversionService delegate : this.delegates) {
209-
if (canConvert.test(delegate)) {
210-
return true;
211-
}
212-
}
213-
return false;
214-
}
215-
216-
@Override
217-
public <T> T convert(Object source, Class<T> targetType) {
218-
Class<?> sourceType = source.getClass();
219-
return convert((delegate) -> delegate.canConvert(sourceType, targetType),
220-
(delegate) -> delegate.convert(source, targetType));
221-
}
222-
223-
@Override
224-
public Object convert(Object source, TypeDescriptor sourceType,
225-
TypeDescriptor targetType) {
226-
return convert((delegate) -> delegate.canConvert(sourceType, targetType),
227-
(delegate) -> delegate.convert(source, sourceType, targetType));
228-
}
229-
230-
public <T> T convert(Predicate<ConversionService> canConvert,
231-
Function<ConversionService, T> convert) {
232-
for (int i = 0; i < this.delegates.size() - 1; i++) {
233-
ConversionService delegate = this.delegates.get(i);
234-
if (canConvert.test(delegate)) {
235-
return convert.apply(delegate);
236-
}
237-
}
238-
return convert.apply(this.delegates.get(this.delegates.size() - 1));
239-
}
240-
241-
}
242-
243251
}

0 commit comments

Comments
 (0)