Merge pull request #14125 from dreis2211:arrays-setall

* pr/14125:
  Polish "Replace loops with Arrays.setAll where possible"
  Replace loops with Arrays.setAll where possible
pull/14085/merge
Stephane Nicoll 6 years ago
commit e90c8e48b6

@ -16,6 +16,7 @@
package org.springframework.boot.configurationprocessor; package org.springframework.boot.configurationprocessor;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
@ -86,9 +87,7 @@ class TypeUtils {
private TypeMirror getDeclaredType(Types types, Class<?> typeClass, private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
int numberOfTypeArgs) { int numberOfTypeArgs) {
TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs]; TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs];
for (int i = 0; i < typeArgs.length; i++) { Arrays.setAll(typeArgs, i -> types.getWildcardType(null, null));
typeArgs[i] = types.getWildcardType(null, null);
}
TypeElement typeElement = this.env.getElementUtils() TypeElement typeElement = this.env.getElementUtils()
.getTypeElement(typeClass.getName()); .getTypeElement(typeClass.getName());
try { try {

@ -17,6 +17,7 @@
package org.springframework.boot.testsupport.web.servlet; package org.springframework.boot.testsupport.web.servlet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -114,9 +115,7 @@ public abstract class MockServletWebServer {
public Servlet[] getServlets() { public Servlet[] getServlets() {
Servlet[] servlets = new Servlet[this.registeredServlets.size()]; Servlet[] servlets = new Servlet[this.registeredServlets.size()];
for (int i = 0; i < servlets.length; i++) { Arrays.setAll(servlets, (i) -> this.registeredServlets.get(i).getServlet());
servlets[i] = this.registeredServlets.get(i).getServlet();
}
return servlets; return servlets;
} }

@ -68,9 +68,7 @@ public class JettyReactiveWebServerFactoryTests
HttpHandler handler = mock(HttpHandler.class); HttpHandler handler = mock(HttpHandler.class);
JettyReactiveWebServerFactory factory = getFactory(); JettyReactiveWebServerFactory factory = getFactory();
JettyServerCustomizer[] configurations = new JettyServerCustomizer[4]; JettyServerCustomizer[] configurations = new JettyServerCustomizer[4];
for (int i = 0; i < configurations.length; i++) { Arrays.setAll(configurations, i -> mock(JettyServerCustomizer.class));
configurations[i] = mock(JettyServerCustomizer.class);
}
factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1])); factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1]));
factory.addServerCustomizers(configurations[2], configurations[3]); factory.addServerCustomizers(configurations[2], configurations[3]);
this.webServer = factory.getWebServer(handler); this.webServer = factory.getWebServer(handler);

@ -75,9 +75,7 @@ public class JettyServletWebServerFactoryTests
public void jettyConfigurations() throws Exception { public void jettyConfigurations() throws Exception {
JettyServletWebServerFactory factory = getFactory(); JettyServletWebServerFactory factory = getFactory();
Configuration[] configurations = new Configuration[4]; Configuration[] configurations = new Configuration[4];
for (int i = 0; i < configurations.length; i++) { Arrays.setAll(configurations, i -> mock(Configuration.class));
configurations[i] = mock(Configuration.class);
}
factory.setConfigurations(Arrays.asList(configurations[0], configurations[1])); factory.setConfigurations(Arrays.asList(configurations[0], configurations[1]));
factory.addConfigurations(configurations[2], configurations[3]); factory.addConfigurations(configurations[2], configurations[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -91,9 +89,7 @@ public class JettyServletWebServerFactoryTests
public void jettyCustomizations() { public void jettyCustomizations() {
JettyServletWebServerFactory factory = getFactory(); JettyServletWebServerFactory factory = getFactory();
JettyServerCustomizer[] configurations = new JettyServerCustomizer[4]; JettyServerCustomizer[] configurations = new JettyServerCustomizer[4];
for (int i = 0; i < configurations.length; i++) { Arrays.setAll(configurations, i -> mock(JettyServerCustomizer.class));
configurations[i] = mock(JettyServerCustomizer.class);
}
factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1])); factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1]));
factory.addServerCustomizers(configurations[2], configurations[3]); factory.addServerCustomizers(configurations[2], configurations[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();

@ -54,9 +54,7 @@ public class TomcatReactiveWebServerFactoryTests
public void tomcatCustomizers() { public void tomcatCustomizers() {
TomcatReactiveWebServerFactory factory = getFactory(); TomcatReactiveWebServerFactory factory = getFactory();
TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4]; TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(TomcatContextCustomizer.class));
listeners[i] = mock(TomcatContextCustomizer.class);
}
factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1]));
factory.addContextCustomizers(listeners[2], listeners[3]); factory.addContextCustomizers(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(mock(HttpHandler.class)); this.webServer = factory.getWebServer(mock(HttpHandler.class));
@ -88,9 +86,7 @@ public class TomcatReactiveWebServerFactoryTests
public void tomcatListeners() { public void tomcatListeners() {
TomcatReactiveWebServerFactory factory = getFactory(); TomcatReactiveWebServerFactory factory = getFactory();
LifecycleListener[] listeners = new LifecycleListener[4]; LifecycleListener[] listeners = new LifecycleListener[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(LifecycleListener.class));
listeners[i] = mock(LifecycleListener.class);
}
factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1])); factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1]));
factory.addContextLifecycleListeners(listeners[2], listeners[3]); factory.addContextLifecycleListeners(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(mock(HttpHandler.class)); this.webServer = factory.getWebServer(mock(HttpHandler.class));
@ -121,9 +117,7 @@ public class TomcatReactiveWebServerFactoryTests
TomcatReactiveWebServerFactory factory = getFactory(); TomcatReactiveWebServerFactory factory = getFactory();
HttpHandler handler = mock(HttpHandler.class); HttpHandler handler = mock(HttpHandler.class);
TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4]; TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(TomcatConnectorCustomizer.class));
listeners[i] = mock(TomcatConnectorCustomizer.class);
}
factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1]));
factory.addConnectorCustomizers(listeners[2], listeners[3]); factory.addConnectorCustomizers(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(handler); this.webServer = factory.getWebServer(handler);

@ -121,9 +121,7 @@ public class TomcatServletWebServerFactoryTests
public void tomcatListeners() { public void tomcatListeners() {
TomcatServletWebServerFactory factory = getFactory(); TomcatServletWebServerFactory factory = getFactory();
LifecycleListener[] listeners = new LifecycleListener[4]; LifecycleListener[] listeners = new LifecycleListener[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(LifecycleListener.class));
listeners[i] = mock(LifecycleListener.class);
}
factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1])); factory.setContextLifecycleListeners(Arrays.asList(listeners[0], listeners[1]));
factory.addContextLifecycleListeners(listeners[2], listeners[3]); factory.addContextLifecycleListeners(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -137,9 +135,7 @@ public class TomcatServletWebServerFactoryTests
public void tomcatCustomizers() { public void tomcatCustomizers() {
TomcatServletWebServerFactory factory = getFactory(); TomcatServletWebServerFactory factory = getFactory();
TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4]; TomcatContextCustomizer[] listeners = new TomcatContextCustomizer[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(TomcatContextCustomizer.class));
listeners[i] = mock(TomcatContextCustomizer.class);
}
factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.setTomcatContextCustomizers(Arrays.asList(listeners[0], listeners[1]));
factory.addContextCustomizers(listeners[2], listeners[3]); factory.addContextCustomizers(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -164,9 +160,7 @@ public class TomcatServletWebServerFactoryTests
public void tomcatConnectorCustomizers() { public void tomcatConnectorCustomizers() {
TomcatServletWebServerFactory factory = getFactory(); TomcatServletWebServerFactory factory = getFactory();
TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4]; TomcatConnectorCustomizer[] listeners = new TomcatConnectorCustomizer[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> mock(TomcatConnectorCustomizer.class));
listeners[i] = mock(TomcatConnectorCustomizer.class);
}
factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1])); factory.setTomcatConnectorCustomizers(Arrays.asList(listeners[0], listeners[1]));
factory.addConnectorCustomizers(listeners[2], listeners[3]); factory.addConnectorCustomizers(listeners[2], listeners[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -180,11 +174,7 @@ public class TomcatServletWebServerFactoryTests
public void tomcatAdditionalConnectors() { public void tomcatAdditionalConnectors() {
TomcatServletWebServerFactory factory = getFactory(); TomcatServletWebServerFactory factory = getFactory();
Connector[] listeners = new Connector[4]; Connector[] listeners = new Connector[4];
for (int i = 0; i < listeners.length; i++) { Arrays.setAll(listeners, i -> stoppedConnector());
Connector connector = mock(Connector.class);
given(connector.getState()).willReturn(LifecycleState.STOPPED);
listeners[i] = connector;
}
factory.addAdditionalTomcatConnectors(listeners); factory.addAdditionalTomcatConnectors(listeners);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
Map<Service, Connector[]> connectors = ((TomcatWebServer) this.webServer) Map<Service, Connector[]> connectors = ((TomcatWebServer) this.webServer)
@ -193,6 +183,12 @@ public class TomcatServletWebServerFactoryTests
.isEqualTo(listeners.length + 1); .isEqualTo(listeners.length + 1);
} }
private Connector stoppedConnector() {
Connector connector = mock(Connector.class);
given(connector.getState()).willReturn(LifecycleState.STOPPED);
return connector;
}
@Test @Test
public void addNullAdditionalConnectorThrows() { public void addNullAdditionalConnectorThrows() {
TomcatServletWebServerFactory factory = getFactory(); TomcatServletWebServerFactory factory = getFactory();

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -77,9 +77,7 @@ public class UndertowReactiveWebServerFactoryTests
UndertowReactiveWebServerFactory factory = getFactory(); UndertowReactiveWebServerFactory factory = getFactory();
HttpHandler handler = mock(HttpHandler.class); HttpHandler handler = mock(HttpHandler.class);
UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4]; UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4];
for (int i = 0; i < customizers.length; i++) { Arrays.setAll(customizers, i -> mock(UndertowBuilderCustomizer.class));
customizers[i] = mock(UndertowBuilderCustomizer.class);
}
factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1])); factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1]));
factory.addBuilderCustomizers(customizers[2], customizers[3]); factory.addBuilderCustomizers(customizers[2], customizers[3]);
this.webServer = factory.getWebServer(handler); this.webServer = factory.getWebServer(handler);

@ -102,9 +102,7 @@ public class UndertowServletWebServerFactoryTests
public void builderCustomizers() { public void builderCustomizers() {
UndertowServletWebServerFactory factory = getFactory(); UndertowServletWebServerFactory factory = getFactory();
UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4]; UndertowBuilderCustomizer[] customizers = new UndertowBuilderCustomizer[4];
for (int i = 0; i < customizers.length; i++) { Arrays.setAll(customizers, i -> mock(UndertowBuilderCustomizer.class));
customizers[i] = mock(UndertowBuilderCustomizer.class);
}
factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1])); factory.setBuilderCustomizers(Arrays.asList(customizers[0], customizers[1]));
factory.addBuilderCustomizers(customizers[2], customizers[3]); factory.addBuilderCustomizers(customizers[2], customizers[3]);
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -134,9 +132,7 @@ public class UndertowServletWebServerFactoryTests
public void deploymentInfo() { public void deploymentInfo() {
UndertowServletWebServerFactory factory = getFactory(); UndertowServletWebServerFactory factory = getFactory();
UndertowDeploymentInfoCustomizer[] customizers = new UndertowDeploymentInfoCustomizer[4]; UndertowDeploymentInfoCustomizer[] customizers = new UndertowDeploymentInfoCustomizer[4];
for (int i = 0; i < customizers.length; i++) { Arrays.setAll(customizers, i -> mock(UndertowDeploymentInfoCustomizer.class));
customizers[i] = mock(UndertowDeploymentInfoCustomizer.class);
}
factory.setDeploymentInfoCustomizers( factory.setDeploymentInfoCustomizers(
Arrays.asList(customizers[0], customizers[1])); Arrays.asList(customizers[0], customizers[1]));
factory.addDeploymentInfoCustomizers(customizers[2], customizers[3]); factory.addDeploymentInfoCustomizers(customizers[2], customizers[3]);

@ -305,9 +305,7 @@ public abstract class AbstractServletWebServerFactoryTests {
public void multipleConfigurations() throws Exception { public void multipleConfigurations() throws Exception {
AbstractServletWebServerFactory factory = getFactory(); AbstractServletWebServerFactory factory = getFactory();
ServletContextInitializer[] initializers = new ServletContextInitializer[6]; ServletContextInitializer[] initializers = new ServletContextInitializer[6];
for (int i = 0; i < initializers.length; i++) { Arrays.setAll(initializers, i -> mock(ServletContextInitializer.class));
initializers[i] = mock(ServletContextInitializer.class);
}
factory.setInitializers(Arrays.asList(initializers[2], initializers[3])); factory.setInitializers(Arrays.asList(initializers[2], initializers[3]));
factory.addInitializers(initializers[4], initializers[5]); factory.addInitializers(initializers[4], initializers[5]);
this.webServer = factory.getWebServer(initializers[0], initializers[1]); this.webServer = factory.getWebServer(initializers[0], initializers[1]);

Loading…
Cancel
Save