From 0bc66dc4a935433bab90c12a754a69bc2e506859 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 17 Aug 2018 18:31:44 +0200 Subject: [PATCH] Replace loops with Arrays.setAll where possible See gh-14125 --- .../configurationprocessor/TypeUtils.java | 5 ++-- .../web/servlet/MockServletWebServer.java | 5 ++-- .../JettyReactiveWebServerFactoryTests.java | 4 +--- .../JettyServletWebServerFactoryTests.java | 8 ++----- .../TomcatReactiveWebServerFactoryTests.java | 12 +++------- .../TomcatServletWebServerFactoryTests.java | 24 ++++++++----------- ...UndertowReactiveWebServerFactoryTests.java | 4 +--- .../UndertowServletWebServerFactoryTests.java | 8 ++----- .../AbstractServletWebServerFactoryTests.java | 4 +--- 9 files changed, 24 insertions(+), 50 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java index ce3e6d3275..7b4e3f7393 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java @@ -16,6 +16,7 @@ package org.springframework.boot.configurationprocessor; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.EnumMap; @@ -86,9 +87,7 @@ class TypeUtils { private TypeMirror getDeclaredType(Types types, Class typeClass, int numberOfTypeArgs) { TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs]; - for (int i = 0; i < typeArgs.length; i++) { - typeArgs[i] = types.getWildcardType(null, null); - } + Arrays.setAll(typeArgs, i -> types.getWildcardType(null, null)); TypeElement typeElement = this.env.getElementUtils() .getTypeElement(typeClass.getName()); try { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/web/servlet/MockServletWebServer.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/web/servlet/MockServletWebServer.java index fdfcf883af..6c2f13f9a2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/web/servlet/MockServletWebServer.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/web/servlet/MockServletWebServer.java @@ -17,6 +17,7 @@ package org.springframework.boot.testsupport.web.servlet; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -114,9 +115,7 @@ public abstract class MockServletWebServer { public Servlet[] getServlets() { Servlet[] servlets = new Servlet[this.registeredServlets.size()]; - for (int i = 0; i < servlets.length; i++) { - servlets[i] = this.registeredServlets.get(i).getServlet(); - } + Arrays.setAll(servlets, i -> this.registeredServlets.get(i).getServlet()); return servlets; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactoryTests.java index 067ea60d45..430ca8409f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactoryTests.java @@ -68,9 +68,7 @@ public class JettyReactiveWebServerFactoryTests HttpHandler handler = mock(HttpHandler.class); JettyReactiveWebServerFactory factory = getFactory(); JettyServerCustomizer[] configurations = new JettyServerCustomizer[4]; - for (int i = 0; i < configurations.length; i++) { - configurations[i] = mock(JettyServerCustomizer.class); - } + Arrays.setAll(configurations, i -> mock(JettyServerCustomizer.class)); factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1])); factory.addServerCustomizers(configurations[2], configurations[3]); this.webServer = factory.getWebServer(handler); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java index fc8f04f60b..faf7cd7187 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java @@ -75,9 +75,7 @@ public class JettyServletWebServerFactoryTests public void jettyConfigurations() throws Exception { JettyServletWebServerFactory factory = getFactory(); Configuration[] configurations = new Configuration[4]; - for (int i = 0; i < configurations.length; i++) { - configurations[i] = mock(Configuration.class); - } + Arrays.setAll(configurations, i -> mock(Configuration.class)); factory.setConfigurations(Arrays.asList(configurations[0], configurations[1])); factory.addConfigurations(configurations[2], configurations[3]); this.webServer = factory.getWebServer(); @@ -91,9 +89,7 @@ public class JettyServletWebServerFactoryTests public void jettyCustomizations() { JettyServletWebServerFactory factory = getFactory(); JettyServerCustomizer[] configurations = new JettyServerCustomizer[4]; - for (int i = 0; i < configurations.length; i++) { - configurations[i] = mock(JettyServerCustomizer.class); - } + Arrays.setAll(configurations, i -> mock(JettyServerCustomizer.class)); factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1])); factory.addServerCustomizers(configurations[2], configurations[3]); this.webServer = factory.getWebServer(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java index 3333ae1f18..a30f07e897 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java @@ -54,9 +54,7 @@ public class TomcatReactiveWebServerFactoryTests public void tomcatCustomizers() { TomcatReactiveWebServerFactory factory = getFactory(); TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(TomcatContextCustomizer.class); - } + Arrays.setAll(listeners, i -> mock(TomcatContextCustomizer.class)); factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.addContextCustomizers(listeners[2], listeners[3]); this.webServer = factory.getWebServer(mock(HttpHandler.class)); @@ -88,9 +86,7 @@ public class TomcatReactiveWebServerFactoryTests public void tomcatListeners() { TomcatReactiveWebServerFactory factory = getFactory(); LifecycleListener[] listeners = new LifecycleListener[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(LifecycleListener.class); - } + Arrays.setAll(listeners, i -> mock(LifecycleListener.class)); factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1])); factory.addContextLifecycleListeners(listeners[2], listeners[3]); this.webServer = factory.getWebServer(mock(HttpHandler.class)); @@ -121,9 +117,7 @@ public class TomcatReactiveWebServerFactoryTests TomcatReactiveWebServerFactory factory = getFactory(); HttpHandler handler = mock(HttpHandler.class); TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(TomcatConnectorCustomizer.class); - } + Arrays.setAll(listeners, i -> mock(TomcatConnectorCustomizer.class)); factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.addConnectorCustomizers(listeners[2], listeners[3]); this.webServer = factory.getWebServer(handler); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index 4f23db2264..c0de25acdf 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -121,9 +121,7 @@ public class TomcatServletWebServerFactoryTests public void tomcatListeners() { TomcatServletWebServerFactory factory = getFactory(); LifecycleListener[] listeners = new LifecycleListener[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(LifecycleListener.class); - } + Arrays.setAll(listeners, i -> mock(LifecycleListener.class)); factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1])); factory.addContextLifecycleListeners(listeners[2], listeners[3]); this.webServer = factory.getWebServer(); @@ -137,9 +135,7 @@ public class TomcatServletWebServerFactoryTests public void tomcatCustomizers() { TomcatServletWebServerFactory factory = getFactory(); TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(TomcatContextCustomizer.class); - } + Arrays.setAll(listeners, i -> mock(TomcatContextCustomizer.class)); factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.addContextCustomizers(listeners[2], listeners[3]); this.webServer = factory.getWebServer(); @@ -164,9 +160,7 @@ public class TomcatServletWebServerFactoryTests public void tomcatConnectorCustomizers() { TomcatServletWebServerFactory factory = getFactory(); TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4]; - for (int i = 0; i < listeners.length; i++) { - listeners[i] = mock(TomcatConnectorCustomizer.class); - } + Arrays.setAll(listeners, i -> mock(TomcatConnectorCustomizer.class)); factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.addConnectorCustomizers(listeners[2], listeners[3]); this.webServer = factory.getWebServer(); @@ -180,11 +174,7 @@ public class TomcatServletWebServerFactoryTests public void tomcatAdditionalConnectors() { TomcatServletWebServerFactory factory = getFactory(); Connector[] listeners = new Connector[4]; - for (int i = 0; i < listeners.length; i++) { - Connector connector = mock(Connector.class); - given(connector.getState()).willReturn(LifecycleState.STOPPED); - listeners[i] = connector; - } + Arrays.setAll(listeners, i -> stoppedConnector()); factory.addAdditionalTomcatConnectors(listeners); this.webServer = factory.getWebServer(); Map connectors = ((TomcatWebServer) this.webServer) @@ -193,6 +183,12 @@ public class TomcatServletWebServerFactoryTests .isEqualTo(listeners.length + 1); } + private Connector stoppedConnector() { + Connector connector = mock(Connector.class); + given(connector.getState()).willReturn(LifecycleState.STOPPED); + return connector; + } + @Test public void addNullAdditionalConnectorThrows() { TomcatServletWebServerFactory factory = getFactory(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowReactiveWebServerFactoryTests.java index 995208debb..fc7b3541e3 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowReactiveWebServerFactoryTests.java @@ -77,9 +77,7 @@ public class UndertowReactiveWebServerFactoryTests UndertowReactiveWebServerFactory factory = getFactory(); HttpHandler handler = mock(HttpHandler.class); UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4]; - for (int i = 0; i < customizers.length; i++) { - customizers[i] = mock(UndertowBuilderCustomizer.class); - } + Arrays.setAll(customizers, i -> mock(UndertowBuilderCustomizer.class)); factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1])); factory.addBuilderCustomizers(customizers[2], customizers[3]); this.webServer = factory.getWebServer(handler); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java index 6f10cc9118..32896ca9e8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactoryTests.java @@ -102,9 +102,7 @@ public class UndertowServletWebServerFactoryTests public void builderCustomizers() { UndertowServletWebServerFactory factory = getFactory(); UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4]; - for (int i = 0; i < customizers.length; i++) { - customizers[i] = mock(UndertowBuilderCustomizer.class); - } + Arrays.setAll(customizers, i -> mock(UndertowBuilderCustomizer.class)); factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1])); factory.addBuilderCustomizers(customizers[2], customizers[3]); this.webServer = factory.getWebServer(); @@ -134,9 +132,7 @@ public class UndertowServletWebServerFactoryTests public void deploymentInfo() { UndertowServletWebServerFactory factory = getFactory(); UndertowDeploymentInfoCustomizer[] customizers = new UndertowDeploymentInfoCustomizer[4]; - for (int i = 0; i < customizers.length; i++) { - customizers[i] = mock(UndertowDeploymentInfoCustomizer.class); - } + Arrays.setAll(customizers, i -> mock(UndertowDeploymentInfoCustomizer.class)); factory.setDeploymentInfoCustomizers( Arrays.asList(customizers[0], customizers[1])); factory.addDeploymentInfoCustomizers(customizers[2], customizers[3]); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index ee07ede7c0..e07158444e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -305,9 +305,7 @@ public abstract class AbstractServletWebServerFactoryTests { public void multipleConfigurations() throws Exception { AbstractServletWebServerFactory factory = getFactory(); ServletContextInitializer[] initializers = new ServletContextInitializer[6]; - for (int i = 0; i < initializers.length; i++) { - initializers[i] = mock(ServletContextInitializer.class); - } + Arrays.setAll(initializers, i -> mock(ServletContextInitializer.class)); factory.setInitializers(Arrays.asList(initializers[2], initializers[3])); factory.addInitializers(initializers[4], initializers[5]); this.webServer = factory.getWebServer(initializers[0], initializers[1]);