Skip to content

Commit 11fc086

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 (cherry picked from commit 9c08a48)
1 parent ed54895 commit 11fc086

File tree

27 files changed

+119
-125
lines changed

27 files changed

+119
-125
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: 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.
@@ -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: 4 additions & 4 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();
@@ -517,7 +517,7 @@ private void addStrippedPropertyPaths(List<String> strippedPaths, String nestedP
517517
* Holder for a registered custom editor with property name.
518518
* Keeps the PropertyEditor itself plus the type it was registered for.
519519
*/
520-
private static class CustomEditorHolder {
520+
private static final class CustomEditorHolder {
521521

522522
private final PropertyEditor propertyEditor;
523523

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;
@@ -135,7 +135,7 @@ public String getBeanName() {
135135
*/
136136
public void addRelatedCause(Throwable ex) {
137137
if (this.relatedCauses == null) {
138-
this.relatedCauses = new LinkedList<>();
138+
this.relatedCauses = new ArrayList<>();
139139
}
140140
this.relatedCauses.add(ex);
141141
}

0 commit comments

Comments
 (0)