Skip to content

Commit a5cbf5f

Browse files
committed
Consistent use of Collection.toArray with zero-sized array argument
Includes consistent use of ClassUtils.toClassArray (as non-null variant) Issue: SPR-16523
1 parent 1ab3f88 commit a5cbf5f

File tree

72 files changed

+209
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+209
-234
lines changed

spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -253,8 +253,8 @@ private AspectComponentDefinition createAspectComponentDefinition(
253253
Element aspectElement, String aspectId, List<BeanDefinition> beanDefs,
254254
List<BeanReference> beanRefs, ParserContext parserContext) {
255255

256-
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[beanDefs.size()]);
257-
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[beanRefs.size()]);
256+
BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[0]);
257+
BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[0]);
258258
Object source = parserContext.extractSource(aspectElement);
259259
return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source);
260260
}

spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -228,7 +228,7 @@ public boolean removeInterface(Class<?> intf) {
228228

229229
@Override
230230
public Class<?>[] getProxiedInterfaces() {
231-
return this.interfaces.toArray(new Class<?>[this.interfaces.size()]);
231+
return ClassUtils.toClassArray(this.interfaces);
232232
}
233233

234234
@Override
@@ -372,7 +372,7 @@ private void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigExcept
372372
* Bring the array up to date with the list.
373373
*/
374374
protected final void updateAdvisorArray() {
375-
this.advisorArray = this.advisors.toArray(new Advisor[this.advisors.size()]);
375+
this.advisorArray = this.advisors.toArray(new Advisor[0]);
376376
}
377377

378378
/**

spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,7 @@ public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdvice
9090
if (interceptors.isEmpty()) {
9191
throw new UnknownAdviceTypeException(advisor.getAdvice());
9292
}
93-
return interceptors.toArray(new MethodInterceptor[interceptors.size()]);
93+
return interceptors.toArray(new MethodInterceptor[0]);
9494
}
9595

9696
@Override

spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private Advisor[] resolveInterceptorNames() {
554554
advisors.add(this.advisorAdapterRegistry.wrap(next));
555555
}
556556
}
557-
return advisors.toArray(new Advisor[advisors.size()]);
557+
return advisors.toArray(new Advisor[0]);
558558
}
559559

560560
/**

spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,7 +105,7 @@ public void addInterface(Class<?> intf) {
105105

106106
@Override
107107
public Class<?>[] getInterfaces() {
108-
return this.interfaces.toArray(new Class<?>[this.interfaces.size()]);
108+
return ClassUtils.toClassArray(this.interfaces);
109109
}
110110

111111
@Override

spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void suppressInterface(Class<?> intf) {
6161

6262
@Override
6363
public Class<?>[] getInterfaces() {
64-
return this.publishedInterfaces.toArray(new Class<?>[this.publishedInterfaces.size()]);
64+
return ClassUtils.toClassArray(this.publishedInterfaces);
6565
}
6666

6767
/**

spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -118,8 +118,7 @@ public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean
118118

119119
// If we encountered individual exceptions, throw the composite exception.
120120
if (propertyAccessExceptions != null) {
121-
PropertyAccessException[] paeArray =
122-
propertyAccessExceptions.toArray(new PropertyAccessException[propertyAccessExceptions.size()]);
121+
PropertyAccessException[] paeArray = propertyAccessExceptions.toArray(new PropertyAccessException[0]);
123122
throw new PropertyBatchUpdateException(paeArray);
124123
}
125124
}

spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -220,7 +220,7 @@ private String propertyNameFor(Method method) {
220220
*/
221221
@Override
222222
public PropertyDescriptor[] getPropertyDescriptors() {
223-
return this.propertyDescriptors.toArray(new PropertyDescriptor[this.propertyDescriptors.size()]);
223+
return this.propertyDescriptors.toArray(new PropertyDescriptor[0]);
224224
}
225225

226226
@Override

spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -246,7 +246,7 @@ public void removePropertyValue(String propertyName) {
246246

247247
@Override
248248
public PropertyValue[] getPropertyValues() {
249-
return this.propertyValueList.toArray(new PropertyValue[this.propertyValueList.size()]);
249+
return this.propertyValueList.toArray(new PropertyValue[0]);
250250
}
251251

252252
@Override

spring-beans/src/main/java/org/springframework/beans/factory/BeanCreationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,7 +149,7 @@ public Throwable[] getRelatedCauses() {
149149
if (this.relatedCauses == null) {
150150
return null;
151151
}
152-
return this.relatedCauses.toArray(new Throwable[this.relatedCauses.size()]);
152+
return this.relatedCauses.toArray(new Throwable[0]);
153153
}
154154

155155

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ else if (candidates.size() == 1 && logger.isWarnEnabled()) {
342342
"default constructor to fall back to: " + candidates.get(0));
343343
}
344344
}
345-
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
345+
candidateConstructors = candidates.toArray(new Constructor<?>[0]);
346346
}
347347
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
348348
candidateConstructors = new Constructor<?>[] {rawCandidates[0]};

spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,8 +83,8 @@ else if (value instanceof BeanReference) {
8383
references.add((BeanReference) value);
8484
}
8585
}
86-
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
87-
this.beanReferences = references.toArray(new BeanReference[references.size()]);
86+
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]);
87+
this.beanReferences = references.toArray(new BeanReference[0]);
8888
}
8989

9090

spring-beans/src/main/java/org/springframework/beans/factory/parsing/CompositeComponentDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,7 @@ public void addNestedComponent(ComponentDefinition component) {
7979
* @return the array of nested components, or an empty array if none
8080
*/
8181
public ComponentDefinition[] getNestedComponents() {
82-
return this.nestedComponents.toArray(new ComponentDefinition[this.nestedComponents.size()]);
82+
return this.nestedComponents.toArray(new ComponentDefinition[0]);
8383
}
8484

8585
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929
import java.util.Arrays;
3030
import java.util.Collection;
3131
import java.util.HashSet;
32-
import java.util.Iterator;
3332
import java.util.LinkedHashSet;
3433
import java.util.LinkedList;
3534
import java.util.List;
@@ -1496,15 +1495,9 @@ protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanW
14961495
* @see #isExcludedFromDependencyCheck
14971496
*/
14981497
protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw) {
1499-
List<PropertyDescriptor> pds =
1500-
new LinkedList<>(Arrays.asList(bw.getPropertyDescriptors()));
1501-
for (Iterator<PropertyDescriptor> it = pds.iterator(); it.hasNext();) {
1502-
PropertyDescriptor pd = it.next();
1503-
if (isExcludedFromDependencyCheck(pd)) {
1504-
it.remove();
1505-
}
1506-
}
1507-
return pds.toArray(new PropertyDescriptor[pds.size()]);
1498+
List<PropertyDescriptor> pds = new LinkedList<>(Arrays.asList(bw.getPropertyDescriptors()));
1499+
pds.removeIf(this::isExcludedFromDependencyCheck);
1500+
return pds.toArray(new PropertyDescriptor[0]);
15081501
}
15091502

15101503
/**

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -419,7 +419,7 @@ public BeanWrapper instantiateUsingFactoryMethod(
419419
candidateSet.add(candidate);
420420
}
421421
}
422-
Method[] candidates = candidateSet.toArray(new Method[candidateSet.size()]);
422+
Method[] candidates = candidateSet.toArray(new Method[0]);
423423
AutowireUtils.sortFactoryMethods(candidates);
424424

425425
ConstructorArgumentValues resolvedValues = null;

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ public Object getOrderSource(Object obj) {
17801780
if (targetType != null && targetType != obj.getClass()) {
17811781
sources.add(targetType);
17821782
}
1783-
return sources.toArray(new Object[sources.size()]);
1783+
return sources.toArray();
17841784
}
17851785

17861786
@Nullable

spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -266,7 +266,7 @@ protected void importBeanDefinitionResource(Element ele) {
266266
ele, ex);
267267
}
268268
}
269-
Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]);
269+
Resource[] actResArray = actualResources.toArray(new Resource[0]);
270270
getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
271271
}
272272

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,7 +82,7 @@ public CacheInvocationParameter[] getKeyParameters(Object... values) {
8282
}
8383
result.add(keyParameterDetail.toCacheInvocationParameter(values[parameterPosition]));
8484
}
85-
return result.toArray(new CacheInvocationParameter[result.size()]);
85+
return result.toArray(new CacheInvocationParameter[0]);
8686
}
8787

8888

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ public CacheInvocationParameter[] getAllParameters(Object... values) {
110110
for (int i = 0; i < this.allParameterDetails.size(); i++) {
111111
result.add(this.allParameterDetails.get(i).toCacheInvocationParameter(values[i]));
112112
}
113-
return result.toArray(new CacheInvocationParameter[result.size()]);
113+
return result.toArray(new CacheInvocationParameter[0]);
114114
}
115115

116116
protected ExceptionTypeFilter createExceptionTypeFilter(

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ private static Object doGenerate(KeyGenerator keyGenerator, CacheKeyInvocationCo
104104
parameters.add(value);
105105
}
106106
}
107-
return keyGenerator.generate(context.getTarget(), context.getMethod(),
108-
parameters.toArray(new Object[parameters.size()]));
109-
107+
return keyGenerator.generate(context.getTarget(), context.getMethod(), parameters.toArray());
110108
}
111109

112110

113111
@SuppressWarnings("unchecked")
114-
private CacheKeyInvocationContext<?> createCacheKeyInvocationContext(Object target,
115-
JCacheOperation<?> operation, Object[] params) {
112+
private CacheKeyInvocationContext<?> createCacheKeyInvocationContext(
113+
Object target, JCacheOperation<?> operation, Object[] params) {
114+
116115
AbstractJCacheKeyOperation<Annotation> keyCacheOperation = (AbstractJCacheKeyOperation<Annotation>) operation;
117116
return new DefaultCacheKeyInvocationContext<>(keyCacheOperation, target, params);
118117
}

spring-context-support/src/main/java/org/springframework/mail/MailSendException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public MailSendException(String msg, @Nullable Throwable cause) {
7171
public MailSendException(@Nullable String msg, @Nullable Throwable cause, Map<Object, Exception> failedMessages) {
7272
super(msg, cause);
7373
this.failedMessages = new LinkedHashMap<>(failedMessages);
74-
this.messageExceptions = failedMessages.values().toArray(new Exception[failedMessages.size()]);
74+
this.messageExceptions = failedMessages.values().toArray(new Exception[0]);
7575
}
7676

7777
/**

spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -318,7 +318,7 @@ public void send(SimpleMailMessage... simpleMessages) throws MailException {
318318
simpleMessage.copyTo(message);
319319
mimeMessages.add(message.getMimeMessage());
320320
}
321-
doSend(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), simpleMessages);
321+
doSend(mimeMessages.toArray(new MimeMessage[0]), simpleMessages);
322322
}
323323

324324

@@ -373,7 +373,7 @@ public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailExc
373373
preparator.prepare(mimeMessage);
374374
mimeMessages.add(mimeMessage);
375375
}
376-
send(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]));
376+
send(mimeMessages.toArray(new MimeMessage[0]));
377377
}
378378
catch (MailException ex) {
379379
throw ex;

spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -390,15 +390,14 @@ protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders)
390390
*/
391391
@Nullable
392392
protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templateLoaders) {
393-
int loaderCount = templateLoaders.size();
394-
switch (loaderCount) {
393+
switch (templateLoaders.size()) {
395394
case 0:
396395
logger.info("No FreeMarker TemplateLoaders specified");
397396
return null;
398397
case 1:
399398
return templateLoaders.get(0);
400399
default:
401-
TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[loaderCount]);
400+
TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[0]);
402401
return new MultiTemplateLoader(loaders);
403402
}
404403
}

0 commit comments

Comments
 (0)