From 350d27fe50d7e78edab2bd195d9a07976ac20ecb Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 17 May 2022 19:03:43 -0700 Subject: [PATCH] Polish code to use method references when possible --- .../boot/build/AsciidoctorConventions.java | 2 +- .../boot/build/DeployedPlugin.java | 22 ++++++++----------- .../boot/build/ExtractResources.java | 6 ++--- .../boot/build/JavaConventions.java | 6 ++--- .../bom/bomr/InteractiveUpgradeResolver.java | 5 +++-- .../classpath/CheckClasspathForConflicts.java | 4 ++-- .../DevToolsR2dbcAutoConfigurationTests.java | 4 ++-- .../boot/gradle/plugin/WarPluginAction.java | 2 +- ...nmentPostProcessorApplicationListener.java | 4 ++-- .../boot/jdbc/DataSourceBuilder.java | 7 +++--- .../log4j2/SpringBootPropertySource.java | 4 ++-- .../boot/util/Instantiator.java | 4 ++-- .../web/embedded/netty/NettyWebServer.java | 6 ++--- .../embedded/undertow/UndertowWebServer.java | 4 ++-- ...iasedConfigurationPropertySourceTests.java | 3 ++- 15 files changed, 39 insertions(+), 44 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/AsciidoctorConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/AsciidoctorConventions.java index e041ae7703..a3b48a7247 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/AsciidoctorConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/AsciidoctorConventions.java @@ -106,7 +106,7 @@ class AsciidoctorConventions { private void createAsciidoctorExtensionsConfiguration(Project project) { project.getConfigurations().create(EXTENSIONS_CONFIGURATION_NAME, (configuration) -> { project.getConfigurations().matching((candidate) -> "dependencyManagement".equals(candidate.getName())) - .all((dependencyManagement) -> configuration.extendsFrom(dependencyManagement)); + .all(configuration::extendsFrom); configuration.getDependencies().add(project.getDependencies() .create("io.spring.asciidoctor.backends:spring-asciidoctor-backends:0.0.3")); configuration.getDependencies() diff --git a/buildSrc/src/main/java/org/springframework/boot/build/DeployedPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/DeployedPlugin.java index 6ce3f72812..583e0dd21b 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/DeployedPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/DeployedPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,18 +43,14 @@ public class DeployedPlugin implements Plugin { project.getPlugins().apply(MavenRepositoryPlugin.class); PublishingExtension publishing = project.getExtensions().getByType(PublishingExtension.class); MavenPublication mavenPublication = publishing.getPublications().create("maven", MavenPublication.class); - project.afterEvaluate((evaluated) -> { - project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> { - if (((Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME)).isEnabled()) { - project.getComponents().matching((component) -> component.getName().equals("java")) - .all((javaComponent) -> mavenPublication.from(javaComponent)); - } - }); - }); - project.getPlugins().withType(JavaPlatformPlugin.class) - .all((javaPlugin) -> project.getComponents() - .matching((component) -> component.getName().equals("javaPlatform")) - .all((javaComponent) -> mavenPublication.from(javaComponent))); + project.afterEvaluate((evaluated) -> project.getPlugins().withType(JavaPlugin.class).all((javaPlugin) -> { + if (((Jar) project.getTasks().getByName(JavaPlugin.JAR_TASK_NAME)).isEnabled()) { + project.getComponents().matching((component) -> component.getName().equals("java")) + .all(mavenPublication::from); + } + })); + project.getPlugins().withType(JavaPlatformPlugin.class).all((javaPlugin) -> project.getComponents() + .matching((component) -> component.getName().equals("javaPlatform")).all(mavenPublication::from)); } } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/ExtractResources.java b/buildSrc/src/main/java/org/springframework/boot/build/ExtractResources.java index 1930013896..09cae37037 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ExtractResources.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ExtractResources.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,6 @@ import java.util.Map; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; -import org.gradle.api.Task; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.OutputDirectory; @@ -87,8 +86,7 @@ public class ExtractResources extends DefaultTask { throw new GradleException("Resource '" + resourceName + "' does not exist"); } String resource = FileCopyUtils.copyToString(new InputStreamReader(resourceStream, StandardCharsets.UTF_8)); - resource = this.propertyPlaceholderHelper.replacePlaceholders(resource, - (placeholder) -> this.properties.get(placeholder)); + resource = this.propertyPlaceholderHelper.replacePlaceholders(resource, this.properties::get); FileCopyUtils.copy(resource, new FileWriter(this.destinationDirectory.file(resourceName).get().getAsFile())); } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java index 6af96fa9e7..9023dc61ca 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -159,8 +159,8 @@ class JavaConventions { project.getTasks().withType(Test.class, (test) -> { test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); - project.getTasks().withType(Checkstyle.class, (checkstyle) -> test.mustRunAfter(checkstyle)); - project.getTasks().withType(CheckFormat.class, (checkFormat) -> test.mustRunAfter(checkFormat)); + project.getTasks().withType(Checkstyle.class, test::mustRunAfter); + project.getTasks().withType(CheckFormat.class, test::mustRunAfter); }); project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> project.getDependencies() .add(JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME, "org.junit.platform:junit-platform-launcher")); diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java index 3f9af772c1..c9e95a3f05 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/InteractiveUpgradeResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.SortedSet; import java.util.stream.Collectors; @@ -70,7 +71,7 @@ public final class InteractiveUpgradeResolver implements UpgradeResolver { librariesByName.put(library.getName(), library); } return libraries.stream().filter((library) -> !library.getName().equals("Spring Boot")) - .map((library) -> resolveUpgrade(library, librariesByName)).filter((upgrade) -> upgrade != null) + .map((library) -> resolveUpgrade(library, librariesByName)).filter(Objects::nonNull) .collect(Collectors.toList()); } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForConflicts.java b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForConflicts.java index b5096ed115..0396522e12 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForConflicts.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForConflicts.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,7 +68,7 @@ public class CheckClasspathForConflicts extends DefaultTask { for (File file : this.classpath) { if (file.isDirectory()) { Path root = file.toPath(); - Files.walk(root).filter((path) -> Files.isRegularFile(path)) + Files.walk(root).filter(Files::isRegularFile) .forEach((entry) -> classpathContents.add(root.relativize(entry).toString(), root.toString())); } else { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java index ac2d53b727..8da0ed983e 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsR2dbcAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,7 @@ class DevToolsR2dbcAutoConfigurationTests { @Test void autoConfiguredInMemoryConnectionFactoryIsShutdown() throws Exception { - ConfigurableApplicationContext context = getContext(() -> createContext()); + ConfigurableApplicationContext context = getContext(this::createContext); ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); context.close(); assertThat(shutdowns).contains(connectionFactory); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java index acc9f19793..95184f8255 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/WarPluginAction.java @@ -91,7 +91,7 @@ class WarPluginAction implements PluginApplicationAction { .convention(resolveMainClassName.flatMap((resolver) -> manifestStartClass.isPresent() ? manifestStartClass : resolveMainClassName.get().readMainClassName())); }); - bootWarProvider.map((bootWar) -> bootWar.getClasspath()); + bootWarProvider.map(War::getClasspath); return bootWarProvider; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java index 366584760c..e43a39fe54 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,7 +56,7 @@ public class EnvironmentPostProcessorApplicationListener implements SmartApplica * {@link EnvironmentPostProcessor} classes loaded via {@code spring.factories}. */ public EnvironmentPostProcessorApplicationListener() { - this((classLoader) -> EnvironmentPostProcessorsFactory.fromSpringFactories(classLoader), new DeferredLogs()); + this(EnvironmentPostProcessorsFactory::fromSpringFactories, new DeferredLogs()); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index 893f789dc7..daeeffd1d2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -398,8 +398,7 @@ public final class DataSourceBuilder { Class dataSourceType) { MappedDataSourceProperties result = null; result = lookup(classLoader, dataSourceType, result, - "org.springframework.jdbc.datasource.SimpleDriverDataSource", - () -> new SimpleDataSourceProperties()); + "org.springframework.jdbc.datasource.SimpleDriverDataSource", SimpleDataSourceProperties::new); result = lookup(classLoader, dataSourceType, result, "oracle.jdbc.datasource.OracleDataSource", OracleDataSourceProperties::new); result = lookup(classLoader, dataSourceType, result, "org.h2.jdbcx.JdbcDataSource", @@ -660,7 +659,7 @@ public final class DataSourceBuilder { SimpleDataSourceProperties() { add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl); add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(), - (dataSource, driverClass) -> dataSource.setDriverClass(driverClass)); + SimpleDriverDataSource::setDriverClass); add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername); add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java index 29c5cc2ec0..b91c1dd9e6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootPropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ public class SpringBootPropertySource implements PropertySource { @Override public void forEach(BiConsumer action) { - this.properties.forEach((key, value) -> action.accept(key, value)); + this.properties.forEach(action::accept); } @Override diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java index aaca052007..ce871c5e3e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,7 +111,7 @@ public class Instantiator { */ public List instantiateTypes(Collection> types) { Assert.notNull(types, "Types must not be null"); - return instantiate(types.stream().map((type) -> TypeSupplier.forType(type))); + return instantiate(types.stream().map(TypeSupplier::forType)); } private List instantiate(Stream typeSuppliers) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index f3c5697e1b..d5f61419ef 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -116,8 +116,8 @@ public class NettyWebServer implements WebServer { private String getStartedOnMessage(DisposableServer server) { StringBuilder message = new StringBuilder(); - tryAppend(message, "port %s", () -> server.port()); - tryAppend(message, "path %s", () -> server.path()); + tryAppend(message, "port %s", server::port); + tryAppend(message, "path %s", server::path); return (message.length() > 0) ? " on " + message : ""; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java index f1e8c28adb..06baea42b5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -295,7 +295,7 @@ public class UndertowWebServer implements WebServer { logger.info("Commencing graceful shutdown. Waiting for active requests to complete"); this.gracefulShutdownCallback.set(callback); this.gracefulShutdown.shutdown(); - this.gracefulShutdown.addShutdownListener((success) -> notifyGracefulCallback(success)); + this.gracefulShutdown.addShutdownListener(this::notifyGracefulCallback); } private void notifyGracefulCallback(boolean success) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java index 772fce96fd..066ba7d3d0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/AliasedConfigurationPropertySourceTests.java @@ -20,6 +20,7 @@ import java.util.Collections; import org.junit.jupiter.api.Test; import org.mockito.Answers; +import org.mockito.invocation.InvocationOnMock; import org.springframework.boot.testsupport.classpath.ClassPathOverrides; @@ -119,7 +120,7 @@ class AliasedConfigurationPropertySourceTests { private ConfigurationPropertySource mockConfigurationPropertySource() { ConfigurationPropertySource mock = mock(ConfigurationPropertySource.class); - given(mock.withAliases(any())).willAnswer((invocation) -> invocation.callRealMethod()); + given(mock.withAliases(any())).willAnswer(InvocationOnMock::callRealMethod); return mock; }