Polish Collection.toArray

Consistently use `StringUtils.toStringArray`, `ClassUtils.toClassArray`
or zero length when converting collections to arrays.

Fixes gh-12160
This commit is contained in:
Phillip Webb
2018-02-22 21:10:02 -08:00
parent 358adcd6e3
commit 4b9c3c137e
84 changed files with 210 additions and 189 deletions

View File

@@ -173,7 +173,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
if (!isEmbeddedWebEnvironment(config) && !hasCustomServerPort(properties)) {
properties.add("server.port=-1");
}
return properties.toArray(new String[properties.size()]);
return StringUtils.toStringArray(properties);
}
private void disableJmx(List<String> properties) {
@@ -187,9 +187,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
private ConfigurationPropertySource convertToConfigurationPropertySource(
List<String> properties) {
String[] array = properties.toArray(new String[properties.size()]);
return new MapConfigurationPropertySource(
TestPropertySourceUtils.convertInlinedPropertiesToMap(array));
return new MapConfigurationPropertySource(TestPropertySourceUtils
.convertInlinedPropertiesToMap(StringUtils.toStringArray(properties)));
}
/**

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ import org.springframework.test.context.web.WebMergedContextConfiguration;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@link TestContextBootstrapper} for Spring Boot. Provides support for
@@ -137,7 +138,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
if (configAttributes.getClasses() != null) {
combined.addAll(Arrays.asList(configAttributes.getClasses()));
}
configAttributes.setClasses(combined.toArray(new Class<?>[combined.size()]));
configAttributes.setClasses(ClassUtils.toClassArray(combined));
}
@Override
@@ -153,8 +154,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
List<String> propertySourceProperties = getAndProcessPropertySourceProperties(
mergedConfig);
mergedConfig = createModifiedConfig(mergedConfig, classes,
propertySourceProperties
.toArray(new String[propertySourceProperties.size()]));
StringUtils.toStringArray(propertySourceProperties));
WebEnvironment webEnvironment = getWebEnvironment(mergedConfig.getTestClass());
if (webEnvironment != null && isWebEnvironmentSupported(mergedConfig)) {
WebApplicationType webApplicationType = getWebApplicationType(mergedConfig);

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.mockito.Mockito;
import org.springframework.core.ResolvableType;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -147,7 +148,7 @@ class MockDefinition extends Definition {
settings.name(name);
}
if (!this.extraInterfaces.isEmpty()) {
settings.extraInterfaces(this.extraInterfaces.toArray(new Class<?>[] {}));
settings.extraInterfaces(ClassUtils.toClassArray(this.extraInterfaces));
}
settings.defaultAnswer(this.answer);
if (this.serializable) {

View File

@@ -279,7 +279,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
}
}
beans.removeIf(this::isScopedTarget);
return beans.toArray(new String[beans.size()]);
return StringUtils.toStringArray(beans);
}
private boolean isScopedTarget(String beanName) {