Skip to content

Commit 350d27f

Browse files
committed
Polish code to use method references when possible
1 parent 50e2733 commit 350d27f

File tree

15 files changed

+39
-44
lines changed

15 files changed

+39
-44
lines changed

buildSrc/src/main/java/org/springframework/boot/build/AsciidoctorConventions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void upgradeAsciidoctorJVersion(Project project) {
106106
private void createAsciidoctorExtensionsConfiguration(Project project) {
107107
project.getConfigurations().create(EXTENSIONS_CONFIGURATION_NAME, (configuration) -> {
108108
project.getConfigurations().matching((candidate) -> "dependencyManagement".equals(candidate.getName()))
109-
.all((dependencyManagement) -> configuration.extendsFrom(dependencyManagement));
109+
.all(configuration::extendsFrom);
110110
configuration.getDependencies().add(project.getDependencies()
111111
.create("io.spring.asciidoctor.backends:spring-asciidoctor-backends:0.0.3"));
112112
configuration.getDependencies()

buildSrc/src/main/java/org/springframework/boot/build/DeployedPlugin.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -43,18 +43,14 @@ public void apply(Project project) {
4343
project.getPlugins().apply(MavenRepositoryPlugin.class);
4444
PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class);
4545
MavenPublication mavenPublication = publishing.getPublications().create("maven", MavenPublication.class);
46-
project.afterEvaluate((evaluated) -> {
47-
project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> {
48-
if (((Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME)).isEnabled()) {
49-
project.getComponents().matching((component) -> component.getName().equals("java"))
50-
.all((javaComponent) -> mavenPublication.from(javaComponent));
51-
}
52-
});
53-
});
54-
project.getPlugins().withType(JavaPlatformPlugin.class)
55-
.all((javaPlugin) -> project.getComponents()
56-
.matching((component) -> component.getName().equals("javaPlatform"))
57-
.all((javaComponent) -> mavenPublication.from(javaComponent)));
46+
project.afterEvaluate((evaluated) -> project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> {
47+
if (((Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME)).isEnabled()) {
48+
project.getComponents().matching((component) -> component.getName().equals("java"))
49+
.all(mavenPublication::from);
50+
}
51+
}));
52+
project.getPlugins().withType(JavaPlatformPlugin.class).all((javaPlugin) -> project.getComponents()
53+
.matching((component) -> component.getName().equals("javaPlatform")).all(mavenPublication::from));
5854
}
5955

6056
}

buildSrc/src/main/java/org/springframework/boot/build/ExtractResources.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -28,7 +28,6 @@
2828

2929
import org.gradle.api.DefaultTask;
3030
import org.gradle.api.GradleException;
31-
import org.gradle.api.Task;
3231
import org.gradle.api.file.DirectoryProperty;
3332
import org.gradle.api.tasks.Input;
3433
import org.gradle.api.tasks.OutputDirectory;
@@ -87,8 +86,7 @@ void extractResources() throws IOException {
8786
throw new GradleException("Resource '" + resourceName + "' does not exist");
8887
}
8988
String resource = FileCopyUtils.copyToString(new InputStreamReader(resourceStream, StandardCharsets.UTF_8));
90-
resource = this.propertyPlaceholderHelper.replacePlaceholders(resource,
91-
(placeholder) -> this.properties.get(placeholder));
89+
resource = this.propertyPlaceholderHelper.replacePlaceholders(resource, this.properties::get);
9290
FileCopyUtils.copy(resource,
9391
new FileWriter(this.destinationDirectory.file(resourceName).get().getAsFile()));
9492
}

buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -159,8 +159,8 @@ private void configureTestConventions(Project project) {
159159
project.getTasks().withType(Test.class, (test) -> {
160160
test.useJUnitPlatform();
161161
test.setMaxHeapSize("1024M");
162-
project.getTasks().withType(Checkstyle.class, (checkstyle) -> test.mustRunAfter(checkstyle));
163-
project.getTasks().withType(CheckFormat.class, (checkFormat) -> test.mustRunAfter(checkFormat));
162+
project.getTasks().withType(Checkstyle.class, test::mustRunAfter);
163+
project.getTasks().withType(CheckFormat.class, test::mustRunAfter);
164164
});
165165
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> project.getDependencies()
166166
.add(JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME, "org.junit.platform:junit-platform-launcher"));

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Objects;
2728
import java.util.Set;
2829
import java.util.SortedSet;
2930
import java.util.stream.Collectors;
@@ -70,7 +71,7 @@ public List<Upgrade> resolveUpgrades(Collection<Library> libraries) {
7071
librariesByName.put(library.getName(), library);
7172
}
7273
return libraries.stream().filter((library) -> !library.getName().equals("Spring Boot"))
73-
.map((library) -> resolveUpgrade(library, librariesByName)).filter((upgrade) -> upgrade != null)
74+
.map((library) -> resolveUpgrade(library, librariesByName)).filter(Objects::nonNull)
7475
.collect(Collectors.toList());
7576
}
7677

buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForConflicts.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -68,7 +68,7 @@ public void checkForConflicts() throws IOException {
6868
for (File file : this.classpath) {
6969
if (file.isDirectory()) {
7070
Path root = file.toPath();
71-
Files.walk(root).filter((path) -> Files.isRegularFile(path))
71+
Files.walk(root).filter(Files::isRegularFile)
7272
.forEach((entry) -> classpathContents.add(root.relativize(entry).toString(), root.toString()));
7373
}
7474
else {

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -63,7 +63,7 @@ void reset() {
6363

6464
@Test
6565
void autoConfiguredInMemoryConnectionFactoryIsShutdown() throws Exception {
66-
ConfigurableApplicationContext context = getContext(() -> createContext());
66+
ConfigurableApplicationContext context = getContext(this::createContext);
6767
ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
6868
context.close();
6969
assertThat(shutdowns).contains(connectionFactory);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private TaskProvider<BootWar> configureBootWarTask(Project project) {
9191
.convention(resolveMainClassName.flatMap((resolver) -> manifestStartClass.isPresent()
9292
? manifestStartClass : resolveMainClassName.get().readMainClassName()));
9393
});
94-
bootWarProvider.map((bootWar) -> bootWar.getClasspath());
94+
bootWarProvider.map(War::getClasspath);
9595
return bootWarProvider;
9696
}
9797

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -56,7 +56,7 @@ public class EnvironmentPostProcessorApplicationListener implements SmartApplica
5656
* {@link EnvironmentPostProcessor} classes loaded via {@code spring.factories}.
5757
*/
5858
public EnvironmentPostProcessorApplicationListener() {
59-
this((classLoader) -> EnvironmentPostProcessorsFactory.fromSpringFactories(classLoader), new DeferredLogs());
59+
this(EnvironmentPostProcessorsFactory::fromSpringFactories, new DeferredLogs());
6060
}
6161

6262
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -398,8 +398,7 @@ private static <T extends DataSource> MappedDataSourceProperties<T> lookupBasic(
398398
Class<T> dataSourceType) {
399399
MappedDataSourceProperties<T> result = null;
400400
result = lookup(classLoader, dataSourceType, result,
401-
"org.springframework.jdbc.datasource.SimpleDriverDataSource",
402-
() -> new SimpleDataSourceProperties());
401+
"org.springframework.jdbc.datasource.SimpleDriverDataSource", SimpleDataSourceProperties::new);
403402
result = lookup(classLoader, dataSourceType, result, "oracle.jdbc.datasource.OracleDataSource",
404403
OracleDataSourceProperties::new);
405404
result = lookup(classLoader, dataSourceType, result, "org.h2.jdbcx.JdbcDataSource",
@@ -660,7 +659,7 @@ private static class SimpleDataSourceProperties extends MappedDataSourceProperti
660659
SimpleDataSourceProperties() {
661660
add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl);
662661
add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(),
663-
(dataSource, driverClass) -> dataSource.setDriverClass(driverClass));
662+
SimpleDriverDataSource::setDriverClass);
664663
add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername);
665664
add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword);
666665
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -38,7 +38,7 @@ public class SpringBootPropertySource implements PropertySource {
3838

3939
@Override
4040
public void forEach(BiConsumer<String, String> action) {
41-
this.properties.forEach((key, value) -> action.accept(key, value));
41+
this.properties.forEach(action::accept);
4242
}
4343

4444
@Override

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -111,7 +111,7 @@ public List<T> instantiate(ClassLoader classLoader, Collection<String> names) {
111111
*/
112112
public List<T> instantiateTypes(Collection<Class<?>> types) {
113113
Assert.notNull(types, "Types must not be null");
114-
return instantiate(types.stream().map((type) -> TypeSupplier.forType(type)));
114+
return instantiate(types.stream().map(TypeSupplier::forType));
115115
}
116116

117117
private List<T> instantiate(Stream<TypeSupplier> typeSuppliers) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -116,8 +116,8 @@ public void start() throws WebServerException {
116116

117117
private String getStartedOnMessage(DisposableServer server) {
118118
StringBuilder message = new StringBuilder();
119-
tryAppend(message, "port %s", () -> server.port());
120-
tryAppend(message, "path %s", () -> server.path());
119+
tryAppend(message, "port %s", server::port);
120+
tryAppend(message, "path %s", server::path);
121121
return (message.length() > 0) ? " on " + message : "";
122122
}
123123

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -295,7 +295,7 @@ public void shutDownGracefully(GracefulShutdownCallback callback) {
295295
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
296296
this.gracefulShutdownCallback.set(callback);
297297
this.gracefulShutdown.shutdown();
298-
this.gracefulShutdown.addShutdownListener((success) -> notifyGracefulCallback(success));
298+
this.gracefulShutdown.addShutdownListener(this::notifyGracefulCallback);
299299
}
300300

301301
private void notifyGracefulCallback(boolean success) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.junit.jupiter.api.Test;
2222
import org.mockito.Answers;
23+
import org.mockito.invocation.InvocationOnMock;
2324

2425
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
2526

@@ -119,7 +120,7 @@ private Object getValue(ConfigurationPropertySource source, String name) {
119120

120121
private ConfigurationPropertySource mockConfigurationPropertySource() {
121122
ConfigurationPropertySource mock = mock(ConfigurationPropertySource.class);
122-
given(mock.withAliases(any())).willAnswer((invocation) -> invocation.callRealMethod());
123+
given(mock.withAliases(any())).willAnswer(InvocationOnMock::callRealMethod);
123124
return mock;
124125
}
125126

0 commit comments

Comments
 (0)