Skip to content

Commit 9c08a48

Browse files
committed
Prefer ArrayList/ArrayDeque over LinkedList for multi-element holders
LinkedList remains in place where a List is likely to remain empty or single-element (in order to avoid unused capacity). Issue: SPR-17037
1 parent 833aee9 commit 9c08a48

File tree

27 files changed

+77
-82
lines changed

27 files changed

+77
-82
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java

Lines changed: 5 additions & 5 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.
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.aop.aspectj.annotation;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collections;
20-
import java.util.LinkedList;
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
@@ -87,8 +87,8 @@ public List<Advisor> buildAspectJAdvisors() {
8787
synchronized (this) {
8888
aspectNames = this.aspectBeanNames;
8989
if (aspectNames == null) {
90-
List<Advisor> advisors = new LinkedList<>();
91-
aspectNames = new LinkedList<>();
90+
List<Advisor> advisors = new ArrayList<>();
91+
aspectNames = new ArrayList<>();
9292
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
9393
this.beanFactory, Object.class, true, false);
9494
for (String beanName : beanNames) {
@@ -138,7 +138,7 @@ public List<Advisor> buildAspectJAdvisors() {
138138
if (aspectNames.isEmpty()) {
139139
return Collections.emptyList();
140140
}
141-
List<Advisor> advisors = new LinkedList<>();
141+
List<Advisor> advisors = new ArrayList<>();
142142
for (String aspectName : aspectNames) {
143143
List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName);
144144
if (cachedAdvisors != null) {

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.lang.annotation.Annotation;
2121
import java.lang.reflect.Field;
2222
import java.lang.reflect.Method;
23+
import java.util.ArrayList;
2324
import java.util.Comparator;
24-
import java.util.LinkedList;
2525
import java.util.List;
2626

2727
import org.aopalliance.aop.Advice;
@@ -121,7 +121,7 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
121121
MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
122122
new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
123123

124-
List<Advisor> advisors = new LinkedList<>();
124+
List<Advisor> advisors = new ArrayList<>();
125125
for (Method method : getAdvisorMethods(aspectClass)) {
126126
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
127127
if (advisor != null) {
@@ -147,7 +147,7 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
147147
}
148148

149149
private List<Method> getAdvisorMethods(Class<?> aspectClass) {
150-
final List<Method> methods = new LinkedList<>();
150+
final List<Method> methods = new ArrayList<>();
151151
ReflectionUtils.doWithMethods(aspectClass, method -> {
152152
// Exclude pointcuts
153153
if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ProxyCreatorSupport extends AdvisedSupport {
3434

3535
private AopProxyFactory aopProxyFactory;
3636

37-
private List<AdvisedSupportListener> listeners = new LinkedList<>();
37+
private final List<AdvisedSupportListener> listeners = new LinkedList<>();
3838

3939
/** Set to true when the first AOP proxy has been created. */
4040
private boolean active = false;

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

Lines changed: 4 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.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.aop.framework.autoproxy;
1818

19-
import java.util.LinkedList;
19+
import java.util.ArrayList;
2020
import java.util.List;
2121

2222
import org.apache.commons.logging.Log;
@@ -78,10 +78,10 @@ public List<Advisor> findAdvisorBeans() {
7878
}
7979
}
8080
if (advisorNames.length == 0) {
81-
return new LinkedList<>();
81+
return new ArrayList<>();
8282
}
8383

84-
List<Advisor> advisors = new LinkedList<>();
84+
List<Advisor> advisors = new ArrayList<>();
8585
for (String name : advisorNames) {
8686
if (isEligibleBean(name)) {
8787
if (this.beanFactory.isCurrentlyInCreation(name)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.lang.reflect.Method;
2121
import java.lang.reflect.Modifier;
2222
import java.lang.reflect.Proxy;
23+
import java.util.ArrayList;
2324
import java.util.LinkedHashSet;
24-
import java.util.LinkedList;
2525
import java.util.List;
2626
import java.util.Set;
2727

@@ -305,7 +305,7 @@ public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvi
305305
if (candidateAdvisors.isEmpty()) {
306306
return candidateAdvisors;
307307
}
308-
List<Advisor> eligibleAdvisors = new LinkedList<>();
308+
List<Advisor> eligibleAdvisors = new ArrayList<>();
309309
for (Advisor candidate : candidateAdvisors) {
310310
if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
311311
eligibleAdvisors.add(candidate);

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

Lines changed: 5 additions & 8 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.
@@ -18,8 +18,8 @@
1818

1919
import java.io.Serializable;
2020
import java.lang.reflect.Method;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
22-
import java.util.LinkedList;
2323
import java.util.List;
2424

2525
import org.springframework.lang.Nullable;
@@ -38,7 +38,7 @@
3838
@SuppressWarnings("serial")
3939
public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut implements Serializable {
4040

41-
private List<String> mappedNames = new LinkedList<>();
41+
private List<String> mappedNames = new ArrayList<>();
4242

4343

4444
/**
@@ -55,11 +55,8 @@ public void setMappedName(String mappedName) {
5555
* Matching will be the union of all these; if any match,
5656
* the pointcut matches.
5757
*/
58-
public void setMappedNames(@Nullable String... mappedNames) {
59-
this.mappedNames = new LinkedList<>();
60-
if (mappedNames != null) {
61-
this.mappedNames.addAll(Arrays.asList(mappedNames));
62-
}
58+
public void setMappedNames(String... mappedNames) {
59+
this.mappedNames = new ArrayList<>(Arrays.asList(mappedNames));
6360
}
6461

6562
/**

spring-aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 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.
@@ -41,18 +41,20 @@ public class NameMatchMethodPointcutTests {
4141

4242
protected SerializableNopInterceptor nop;
4343

44+
4445
/**
4546
* Create an empty pointcut, populating instance variables.
4647
*/
4748
@Before
48-
public void setUp() {
49+
public void setup() {
4950
ProxyFactory pf = new ProxyFactory(new SerializablePerson());
5051
nop = new SerializableNopInterceptor();
5152
pc = new NameMatchMethodPointcut();
5253
pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
5354
proxied = (Person) pf.getProxy();
5455
}
5556

57+
5658
@Test
5759
public void testMatchingOnly() {
5860
// Can't do exact matching through isMatch
@@ -94,7 +96,7 @@ public void testMatchOneMethod() throws Throwable {
9496

9597
@Test
9698
public void testSets() throws Throwable {
97-
pc.setMappedNames(new String[] { "set*", "echo" });
99+
pc.setMappedNames("set*", "echo");
98100
assertEquals(0, nop.getCount());
99101
proxied.getName();
100102
proxied.setName("");
@@ -116,7 +118,7 @@ public void testSerializable() throws Throwable {
116118
}
117119

118120
@Test
119-
public void testEqualsAndHashCode() throws Exception {
121+
public void testEqualsAndHashCode() {
120122
NameMatchMethodPointcut pc1 = new NameMatchMethodPointcut();
121123
NameMatchMethodPointcut pc2 = new NameMatchMethodPointcut();
122124

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.beans;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
20-
import java.util.LinkedList;
2121
import java.util.List;
2222
import java.util.Map;
2323

@@ -110,7 +110,7 @@ public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean
110110
}
111111
catch (PropertyAccessException ex) {
112112
if (propertyAccessExceptions == null) {
113-
propertyAccessExceptions = new LinkedList<>();
113+
propertyAccessExceptions = new ArrayList<>();
114114
}
115115
propertyAccessExceptions.add(ex);
116116
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import java.nio.charset.Charset;
2828
import java.nio.file.Path;
2929
import java.time.ZoneId;
30+
import java.util.ArrayList;
3031
import java.util.Collection;
3132
import java.util.Currency;
3233
import java.util.HashMap;
3334
import java.util.Iterator;
3435
import java.util.LinkedHashMap;
35-
import java.util.LinkedList;
3636
import java.util.List;
3737
import java.util.Locale;
3838
import java.util.Map;
@@ -318,7 +318,7 @@ public PropertyEditor findCustomEditor(@Nullable Class<?> requiredType, @Nullabl
318318
// Check property-specific editor first.
319319
PropertyEditor editor = getCustomEditor(propertyPath, requiredType);
320320
if (editor == null) {
321-
List<String> strippedPaths = new LinkedList<>();
321+
List<String> strippedPaths = new ArrayList<>();
322322
addStrippedPropertyPaths(strippedPaths, "", propertyPath);
323323
for (Iterator<String> it = strippedPaths.iterator(); it.hasNext() && editor == null;) {
324324
String strippedPath = it.next();
@@ -438,7 +438,7 @@ protected Class<?> guessPropertyTypeFromEditors(String propertyName) {
438438
if (this.customEditorsForPath != null) {
439439
CustomEditorHolder editorHolder = this.customEditorsForPath.get(propertyName);
440440
if (editorHolder == null) {
441-
List<String> strippedPaths = new LinkedList<>();
441+
List<String> strippedPaths = new ArrayList<>();
442442
addStrippedPropertyPaths(strippedPaths, "", propertyName);
443443
for (Iterator<String> it = strippedPaths.iterator(); it.hasNext() && editorHolder == null;) {
444444
String strippedName = it.next();

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
@@ -18,7 +18,7 @@
1818

1919
import java.io.PrintStream;
2020
import java.io.PrintWriter;
21-
import java.util.LinkedList;
21+
import java.util.ArrayList;
2222
import java.util.List;
2323

2424
import org.springframework.beans.FatalBeanException;
@@ -141,7 +141,7 @@ public String getBeanName() {
141141
*/
142142
public void addRelatedCause(Throwable ex) {
143143
if (this.relatedCauses == null) {
144-
this.relatedCauses = new LinkedList<>();
144+
this.relatedCauses = new ArrayList<>();
145145
}
146146
this.relatedCauses.add(ex);
147147
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Collections;
2929
import java.util.Iterator;
3030
import java.util.LinkedHashSet;
31-
import java.util.LinkedList;
3231
import java.util.List;
3332
import java.util.Map;
3433
import java.util.Set;
@@ -430,11 +429,11 @@ private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz
430429
}
431430

432431
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
433-
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
432+
List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
434433
Class<?> targetClass = clazz;
435434

436435
do {
437-
final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
436+
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
438437

439438
ReflectionUtils.doWithLocalFields(targetClass, field -> {
440439
AnnotationAttributes ann = findAutowiredAnnotation(field);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import java.lang.reflect.InvocationTargetException;
2424
import java.lang.reflect.Method;
2525
import java.lang.reflect.Modifier;
26+
import java.util.ArrayList;
2627
import java.util.Collection;
2728
import java.util.LinkedHashSet;
28-
import java.util.LinkedList;
29+
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Set;
3132
import java.util.concurrent.ConcurrentHashMap;
@@ -196,13 +197,13 @@ private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {
196197

197198
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
198199
final boolean debug = logger.isDebugEnabled();
199-
LinkedList<LifecycleElement> initMethods = new LinkedList<>();
200-
LinkedList<LifecycleElement> destroyMethods = new LinkedList<>();
200+
List<LifecycleElement> initMethods = new ArrayList<>();
201+
List<LifecycleElement> destroyMethods = new ArrayList<>();
201202
Class<?> targetClass = clazz;
202203

203204
do {
204-
final LinkedList<LifecycleElement> currInitMethods = new LinkedList<>();
205-
final LinkedList<LifecycleElement> currDestroyMethods = new LinkedList<>();
205+
final List<LifecycleElement> currInitMethods = new ArrayList<>();
206+
final List<LifecycleElement> currDestroyMethods = new ArrayList<>();
206207

207208
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
208209
if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {

spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.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.
@@ -16,10 +16,10 @@
1616

1717
package org.springframework.beans.factory.config;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collections;
2021
import java.util.Iterator;
2122
import java.util.LinkedHashMap;
22-
import java.util.LinkedList;
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Set;
@@ -45,7 +45,7 @@ public class ConstructorArgumentValues {
4545

4646
private final Map<Integer, ValueHolder> indexedArgumentValues = new LinkedHashMap<>(0);
4747

48-
private final List<ValueHolder> genericArgumentValues = new LinkedList<>();
48+
private final List<ValueHolder> genericArgumentValues = new ArrayList<>();
4949

5050

5151
/**

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
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.beans.factory.parsing;
1818

19-
import java.util.LinkedList;
19+
import java.util.ArrayList;
2020
import java.util.List;
2121

2222
import org.springframework.lang.Nullable;
@@ -38,7 +38,7 @@ public class CompositeComponentDefinition extends AbstractComponentDefinition {
3838
@Nullable
3939
private final Object source;
4040

41-
private final List<ComponentDefinition> nestedComponents = new LinkedList<>();
41+
private final List<ComponentDefinition> nestedComponents = new ArrayList<>();
4242

4343

4444
/**

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Collection;
3131
import java.util.HashSet;
3232
import java.util.LinkedHashSet;
33-
import java.util.LinkedList;
3433
import java.util.List;
3534
import java.util.Map;
3635
import java.util.Set;
@@ -1518,7 +1517,7 @@ protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanW
15181517
* @see #isExcludedFromDependencyCheck
15191518
*/
15201519
protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw) {
1521-
List<PropertyDescriptor> pds = new LinkedList<>(Arrays.asList(bw.getPropertyDescriptors()));
1520+
List<PropertyDescriptor> pds = new ArrayList<>(Arrays.asList(bw.getPropertyDescriptors()));
15221521
pds.removeIf(this::isExcludedFromDependencyCheck);
15231522
return pds.toArray(new PropertyDescriptor[0]);
15241523
}

0 commit comments

Comments
 (0)