Skip to content

Commit 80e31e0

Browse files
committed
Merge pull request #9781 from emacampolo/refactor-and-polish-with-new-idioms
* pr/9781: Simplify comparator implementation Use lambdas when possible Replace try with try-with-resources Collapse catch clauses Replace Collections.sort() with direct sort call Replace explicit generics with diamond operator
2 parents eb35fc9 + 687464e commit 80e31e0

File tree

214 files changed

+1082
-2772
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+1082
-2772
lines changed

spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import org.springframework.test.context.junit4.SpringRunner;
5353
import org.springframework.test.context.web.WebAppConfiguration;
5454
import org.springframework.test.web.servlet.MockMvc;
55-
import org.springframework.test.web.servlet.MvcResult;
56-
import org.springframework.test.web.servlet.ResultHandler;
5755
import org.springframework.util.FileCopyUtils;
5856
import org.springframework.util.StringUtils;
5957

@@ -161,32 +159,9 @@ public void auditEventsByPrincipalAndType() throws Exception {
161159

162160
@Test
163161
public void endpoints() throws Exception {
164-
final File docs = new File("src/main/asciidoc");
165-
final Map<String, Object> model = new LinkedHashMap<>();
166-
final List<EndpointDoc> endpoints = new ArrayList<>();
167-
model.put("endpoints", endpoints);
168-
for (MvcEndpoint endpoint : getEndpoints()) {
169-
final String endpointPath = (StringUtils.hasText(endpoint.getPath())
170-
? endpoint.getPath() : "/");
171-
if (!SKIPPED.contains(endpointPath)) {
172-
String output = endpointPath.substring(1);
173-
output = output.length() > 0 ? output : "./";
174-
this.mockMvc
175-
.perform(get("/application" + endpointPath)
176-
.accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON))
177-
.andExpect(status().isOk()).andDo(document(output))
178-
.andDo(new ResultHandler() {
179-
180-
@Override
181-
public void handle(MvcResult mvcResult) throws Exception {
182-
EndpointDoc endpoint = new EndpointDoc(docs,
183-
endpointPath);
184-
endpoints.add(endpoint);
185-
}
186-
187-
});
188-
}
189-
}
162+
File docs = new File("src/main/asciidoc");
163+
Map<String, Object> model = new LinkedHashMap<>();
164+
model.put("endpoints", getEndpointDocs(docs));
190165
File file = new File(RESTDOCS_OUTPUT_DIR + "/endpoints.adoc");
191166
file.getParentFile().mkdirs();
192167
try (PrintWriter writer = new PrintWriter(file, "UTF-8")) {
@@ -196,18 +171,36 @@ public void handle(MvcResult mvcResult) throws Exception {
196171
}
197172
}
198173

174+
private List<EndpointDoc> getEndpointDocs(File docs) throws Exception {
175+
final List<EndpointDoc> endpoints = new ArrayList<>();
176+
for (MvcEndpoint endpoint : getEndpoints()) {
177+
String path = endpoint.getPath();
178+
path = (StringUtils.hasText(path) ? path : "/");
179+
if (!SKIPPED.contains(path)) {
180+
documentEndpoint(path);
181+
endpoints.add(new EndpointDoc(docs, path));
182+
}
183+
}
184+
return endpoints;
185+
}
186+
199187
private Collection<? extends MvcEndpoint> getEndpoints() {
200188
List<? extends MvcEndpoint> endpoints = new ArrayList<>(
201189
this.mvcEndpoints.getEndpoints());
202-
Collections.sort(endpoints, new Comparator<MvcEndpoint>() {
203-
@Override
204-
public int compare(MvcEndpoint o1, MvcEndpoint o2) {
205-
return o1.getPath().compareTo(o2.getPath());
206-
}
207-
});
190+
endpoints.sort(Comparator.comparing(MvcEndpoint::getPath));
208191
return endpoints;
209192
}
210193

194+
private String documentEndpoint(final String endpointPath) throws Exception {
195+
String output = endpointPath.substring(1);
196+
output = (output.length() > 0 ? output : "./");
197+
this.mockMvc
198+
.perform(get("/application" + endpointPath)
199+
.accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V2_JSON))
200+
.andExpect(status().isOk()).andDo(document(output));
201+
return output;
202+
}
203+
211204
public static class EndpointDoc {
212205

213206
private String path;

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -137,15 +137,11 @@ static class NoOpCacheStatisticsConfiguration {
137137

138138
@Bean
139139
public CacheStatisticsProvider<Cache> noOpCacheStatisticsProvider() {
140-
return new CacheStatisticsProvider<Cache>() {
141-
@Override
142-
public CacheStatistics getCacheStatistics(CacheManager cacheManager,
143-
Cache cache) {
144-
if (cacheManager instanceof NoOpCacheManager) {
145-
return NO_OP_STATS;
146-
}
147-
return null;
140+
return (cacheManager, cache) -> {
141+
if (cacheManager instanceof NoOpCacheManager) {
142+
return NO_OP_STATS;
148143
}
144+
return null;
149145
};
150146
}
151147

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public MetricsEndpoint metricsEndpoint() {
149149
if (this.publicMetrics != null) {
150150
publicMetrics.addAll(this.publicMetrics);
151151
}
152-
Collections.sort(publicMetrics, AnnotationAwareOrderComparator.INSTANCE);
152+
publicMetrics.sort(AnnotationAwareOrderComparator.INSTANCE);
153153
return new MetricsEndpoint(publicMetrics);
154154
}
155155

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,7 @@ public ManagementContextResolver managementContextResolver() {
120120
@Bean
121121
public ManagementServletContext managementServletContext(
122122
final ManagementServerProperties properties) {
123-
return new ManagementServletContext() {
124-
125-
@Override
126-
public String getContextPath() {
127-
return properties.getContextPath();
128-
}
129-
130-
};
123+
return properties::getContextPath;
131124
}
132125

133126
@Override

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
101101
@Bean
102102
public ManagementServletContext managementServletContext(
103103
final ManagementServerProperties properties) {
104-
return new ManagementServletContext() {
105-
106-
@Override
107-
public String getContextPath() {
108-
return properties.getContextPath();
109-
}
110-
111-
};
104+
return properties::getContextPath;
112105
}
113106

114107
@ConditionalOnEnabledEndpoint("actuator")

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static class Security {
169169
/**
170170
* Comma-separated list of roles that can access the management endpoint.
171171
*/
172-
private List<String> roles = new ArrayList<String>(
172+
private List<String> roles = new ArrayList<>(
173173
Collections.singletonList("ACTUATOR"));
174174

175175
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cache/AbstractJmxCacheStatisticsProvider.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,13 @@ protected <T> T getAttribute(ObjectName objectName, String attributeName,
107107
Object attribute = getMBeanServer().getAttribute(objectName, attributeName);
108108
return type.cast(attribute);
109109
}
110-
catch (MBeanException ex) {
110+
catch (MBeanException | ReflectionException ex) {
111111
throw new IllegalStateException(ex);
112112
}
113113
catch (AttributeNotFoundException ex) {
114114
throw new IllegalStateException("Unexpected: MBean with name '" + objectName
115115
+ "' " + "does not expose attribute with name " + attributeName, ex);
116116
}
117-
catch (ReflectionException ex) {
118-
throw new IllegalStateException(ex);
119-
}
120117
catch (InstanceNotFoundException ex) {
121118
logger.warn("Cache statistics are no longer available", ex);
122119
return null;

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public PropertyResolver getResolver() {
8989
}
9090

9191
private Map<String, PropertySource<?>> getPropertySourcesAsMap() {
92-
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
92+
Map<String, PropertySource<?>> map = new LinkedHashMap<>();
9393
for (PropertySource<?> source : getPropertySources()) {
9494
extract("", map, source);
9595
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -62,23 +62,22 @@ public Map<String, Object> invoke() {
6262
return SHUTDOWN_MESSAGE;
6363
}
6464
finally {
65-
Thread thread = new Thread(new Runnable() {
66-
@Override
67-
public void run() {
68-
try {
69-
Thread.sleep(500L);
70-
}
71-
catch (InterruptedException ex) {
72-
Thread.currentThread().interrupt();
73-
}
74-
ShutdownEndpoint.this.context.close();
75-
}
76-
});
65+
Thread thread = new Thread(this::performShutdown);
7766
thread.setContextClassLoader(getClass().getClassLoader());
7867
thread.start();
7968
}
8069
}
8170

71+
private void performShutdown() {
72+
try {
73+
Thread.sleep(500L);
74+
}
75+
catch (InterruptedException ex) {
76+
Thread.currentThread().interrupt();
77+
}
78+
this.context.close();
79+
}
80+
8281
@Override
8382
public void setApplicationContext(ApplicationContext context) throws BeansException {
8483
if (context instanceof ConfigurableApplicationContext) {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/CompositeHealthIndicator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class CompositeHealthIndicator implements HealthIndicator {
4040
* @param healthAggregator the health aggregator
4141
*/
4242
public CompositeHealthIndicator(HealthAggregator healthAggregator) {
43-
this(healthAggregator, new LinkedHashMap<String, HealthIndicator>());
43+
this(healthAggregator, new LinkedHashMap<>());
4444
}
4545

4646
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -26,7 +26,6 @@
2626

2727
import org.springframework.beans.factory.InitializingBean;
2828
import org.springframework.boot.jdbc.DatabaseDriver;
29-
import org.springframework.dao.DataAccessException;
3029
import org.springframework.dao.support.DataAccessUtils;
3130
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
3231
import org.springframework.jdbc.core.ConnectionCallback;
@@ -120,13 +119,11 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
120119
}
121120

122121
private String getProduct() {
123-
return this.jdbcTemplate.execute(new ConnectionCallback<String>() {
124-
@Override
125-
public String doInConnection(Connection connection)
126-
throws SQLException, DataAccessException {
127-
return connection.getMetaData().getDatabaseProductName();
128-
}
129-
});
122+
return this.jdbcTemplate.execute((ConnectionCallback<String>) this::getProduct);
123+
}
124+
125+
private String getProduct(Connection connection) throws SQLException {
126+
return connection.getMetaData().getDatabaseProductName();
130127
}
131128

132129
protected String getValidationQuery(String product) {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/OrderedHealthAggregator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21-
import java.util.Collections;
2221
import java.util.Comparator;
2322
import java.util.List;
2423

@@ -80,7 +79,7 @@ protected Status aggregateStatus(List<Status> candidates) {
8079
return Status.UNKNOWN;
8180
}
8281
// Sort given Status instances by configured order
83-
Collections.sort(filteredCandidates, new StatusComparator(this.statusOrder));
82+
filteredCandidates.sort(new StatusComparator(this.statusOrder));
8483
return filteredCandidates.get(0);
8584
}
8685

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.boot.actuate.health;
1818

19-
import java.util.Map;
20-
21-
import com.rabbitmq.client.Channel;
22-
23-
import org.springframework.amqp.rabbit.core.ChannelCallback;
2419
import org.springframework.amqp.rabbit.core.RabbitTemplate;
2520
import org.springframework.util.Assert;
2621

@@ -46,14 +41,8 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
4641
}
4742

4843
private String getVersion() {
49-
return this.rabbitTemplate.execute(new ChannelCallback<String>() {
50-
@Override
51-
public String doInRabbit(Channel channel) throws Exception {
52-
Map<String, Object> serverProperties = channel.getConnection()
53-
.getServerProperties();
54-
return serverProperties.get("version").toString();
55-
}
56-
});
44+
return this.rabbitTemplate.execute((channel) -> channel.getConnection()
45+
.getServerProperties().get("version").toString());
5746
}
5847

5948
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferMetricReader.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Date;
2121
import java.util.List;
22-
import java.util.function.BiConsumer;
2322
import java.util.function.Predicate;
2423
import java.util.regex.Pattern;
2524

@@ -48,7 +47,7 @@ public BufferMetricReader(CounterBuffers counterBuffers, GaugeBuffers gaugeBuffe
4847
}
4948

5049
@Override
51-
public Metric<?> findOne(final String name) {
50+
public Metric<?> findOne(String name) {
5251
Buffer<?> buffer = this.counterBuffers.find(name);
5352
if (buffer == null) {
5453
buffer = this.gaugeBuffers.find(name);
@@ -81,17 +80,10 @@ private Iterable<Metric<?>> findAll(Predicate<String> predicate) {
8180
private <T extends Number, B extends Buffer<T>> void collectMetrics(
8281
Buffers<B> buffers, Predicate<String> predicate,
8382
final List<Metric<?>> metrics) {
84-
buffers.forEach(predicate, new BiConsumer<String, B>() {
85-
86-
@Override
87-
public void accept(String name, B value) {
88-
metrics.add(asMetric(name, value));
89-
}
90-
91-
});
83+
buffers.forEach(predicate, (name, value) -> metrics.add(asMetric(name, value)));
9284
}
9385

94-
private <T extends Number> Metric<T> asMetric(final String name, Buffer<T> buffer) {
86+
private <T extends Number> Metric<T> asMetric(String name, Buffer<T> buffer) {
9587
return new Metric<>(name, buffer.getValue(), new Date(buffer.getTimestamp()));
9688
}
9789

0 commit comments

Comments
 (0)