Consistent use of Collection.toArray with zero-sized array argument

Includes consistent use of ClassUtils.toClassArray (as non-null variant)

Issue: SPR-16523
This commit is contained in:
Juergen Hoeller
2018-02-22 11:29:46 +01:00
parent 1ab3f88e82
commit a5cbf5fe24
72 changed files with 208 additions and 233 deletions

View File

@@ -357,7 +357,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
public Cookie[] getCookies() {
return this.cookies.toArray(new Cookie[this.cookies.size()]);
return this.cookies.toArray(new Cookie[0]);
}
@Nullable

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -149,7 +149,7 @@ public class TestContextManager {
* @see #registerTestExecutionListeners(TestExecutionListener...)
*/
public void registerTestExecutionListeners(List<TestExecutionListener> testExecutionListeners) {
registerTestExecutionListeners(testExecutionListeners.toArray(new TestExecutionListener[testExecutionListeners.size()]));
registerTestExecutionListeners(testExecutionListeners.toArray(new TestExecutionListener[0]));
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -185,7 +185,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
scriptResources.add(new ByteArrayResource(stmt.getBytes(), "from inlined SQL statement: " + stmt));
}
}
populator.setScripts(scriptResources.toArray(new Resource[scriptResources.size()]));
populator.setScripts(scriptResources.toArray(new Resource[0]));
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources));
}

View File

@@ -385,8 +385,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
MergedTestPropertySources mergedTestPropertySources =
TestPropertySourceUtils.buildMergedTestPropertySources(testClass);
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass,
StringUtils.toStringArray(locations),
ClassUtils.toClassArray(classes),
StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes),
ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList),
ActiveProfilesUtils.resolveActiveProfiles(testClass),
mergedTestPropertySources.getLocations(),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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,6 +28,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal
@@ -85,7 +86,7 @@ public abstract class AnnotationConfigContextLoaderUtils {
}
}
return configClasses.toArray(new Class<?>[configClasses.size()]);
return ClassUtils.toClassArray(configClasses);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -29,6 +29,7 @@ import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Default implementation of the {@link TestContext} interface.
@@ -195,7 +196,7 @@ public class DefaultTestContext implements TestContext {
@Override
public String[] attributeNames() {
synchronized (this.attributes) {
return this.attributes.keySet().stream().toArray(String[]::new);
return StringUtils.toStringArray(this.attributes.keySet());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
}
if (!ObjectUtils.isEmpty(cookies)) {
request.setCookies(cookies.toArray(new Cookie[cookies.size()]));
request.setCookies(cookies.toArray(new Cookie[0]));
}
}

View File

@@ -652,7 +652,7 @@ public class MockHttpServletRequestBuilder
}
if (!ObjectUtils.isEmpty(this.cookies)) {
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
request.setCookies(this.cookies.toArray(new Cookie[0]));
}
if (!ObjectUtils.isEmpty(this.locales)) {
request.setPreferredLocales(this.locales);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -25,6 +25,7 @@ import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MockMvcBuilderSupport;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultHandler;
@@ -34,7 +35,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.test.web.servlet.MockMvcBuilder;
/**
* Abstract implementation of {@link MockMvcBuilder} with common methods for
@@ -144,7 +144,7 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
}
}
Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]);
Filter[] filterArray = this.filters.toArray(new Filter[0]);
return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder,
this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers);