Skip to content

Commit 9483670

Browse files
kamilkrzywanskisbrannen
authored andcommitted
Use empty array constants instead of repeatedly creating new ones
Closes gh-35660 Signed-off-by: Kamil Krzywański <kamilkrzywanski01@gmail.com> Signed-off-by: Kamil Krzywanski <kamilkrzywanski01@gmail.com>
1 parent 687bc76 commit 9483670

6 files changed

Lines changed: 19 additions & 8 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
7676
* supplied by the advisors.
7777
*/
7878
public static final TargetSource EMPTY_TARGET_SOURCE = EmptyTargetSource.INSTANCE;
79-
79+
/** Empty advisor array constant. */
80+
public static final Advisor[] EMPTY_ADVISORS = new Advisor[0];
8081

8182
/** Package-protected to allow direct access for efficiency. */
8283
@SuppressWarnings("serial")
@@ -288,7 +289,7 @@ private boolean isAdvisorIntroducedInterface(Class<?> ifc) {
288289

289290
@Override
290291
public final Advisor[] getAdvisors() {
291-
return this.advisors.toArray(new Advisor[0]);
292+
return this.advisors.toArray(EMPTY_ADVISORS);
292293
}
293294

294295
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
@SuppressWarnings("serial")
4141
public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {
42+
private static final MethodInterceptor [] EMPTY_INTERCEPTOR_ARRAY = new MethodInterceptor[0];
4243

4344
private final List<AdvisorAdapter> adapters = new ArrayList<>(3);
4445

@@ -89,7 +90,7 @@ public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdvice
8990
if (interceptors.isEmpty()) {
9091
throw new UnknownAdviceTypeException(advisor.getAdvice());
9192
}
92-
return interceptors.toArray(new MethodInterceptor[0]);
93+
return interceptors.toArray(EMPTY_INTERCEPTOR_ARRAY);
9394
}
9495

9596
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
@SuppressWarnings("serial")
4545
public class MutablePropertyValues implements PropertyValues, Serializable {
4646

47+
private static final PropertyValue[] EMPTY_PROPERTY_VALUES = new PropertyValue[0];
48+
4749
private final List<PropertyValue> propertyValueList;
4850

4951
private @Nullable Set<String> processedProperties;
@@ -264,7 +266,7 @@ public Stream<PropertyValue> stream() {
264266

265267
@Override
266268
public PropertyValue[] getPropertyValues() {
267-
return this.propertyValueList.toArray(new PropertyValue[0]);
269+
return this.propertyValueList.toArray(EMPTY_PROPERTY_VALUES);
268270
}
269271

270272
@Override

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
*/
3838
public class BeanComponentDefinition extends BeanDefinitionHolder implements ComponentDefinition {
3939

40+
private static final BeanDefinition[] EMPTY_BEAN_DEFINITIONS = new BeanDefinition[0];
41+
private static final BeanReference[] EMPTY_BEAN_REFERENCES = new BeanReference[0];
42+
4043
private final BeanDefinition[] innerBeanDefinitions;
4144

4245
private final BeanReference[] beanReferences;
@@ -84,8 +87,8 @@ else if (value instanceof BeanReference beanRef) {
8487
references.add(beanRef);
8588
}
8689
}
87-
this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]);
88-
this.beanReferences = references.toArray(new BeanReference[0]);
90+
this.innerBeanDefinitions = innerBeans.toArray(EMPTY_BEAN_DEFINITIONS);
91+
this.beanReferences = references.toArray(EMPTY_BEAN_REFERENCES);
8992
}
9093

9194

spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationsCollection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@
4040
*/
4141
final class MergedAnnotationsCollection implements MergedAnnotations {
4242

43+
private static final MergedAnnotation<?> [] EMPTY_ANNOTATIONS = new MergedAnnotation<?>[0];
44+
4345
private final MergedAnnotation<?>[] annotations;
4446

4547
private final AnnotationTypeMappings[] mappings;
4648

4749

4850
private MergedAnnotationsCollection(Collection<MergedAnnotation<?>> annotations) {
4951
Assert.notNull(annotations, "Annotations must not be null");
50-
this.annotations = annotations.toArray(new MergedAnnotation<?>[0]);
52+
this.annotations = annotations.toArray(EMPTY_ANNOTATIONS);
5153
this.mappings = new AnnotationTypeMappings[this.annotations.length];
5254
for (int i = 0; i < this.annotations.length; i++) {
5355
MergedAnnotation<?> annotation = this.annotations[i];

spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
*/
4545
public abstract class TemplateAwareExpressionParser implements ExpressionParser {
4646

47+
private static final Expression [] EMPTY_EXPRESSION_ARRAY = new Expression[0];
48+
4749
@Override
4850
public Expression parseExpression(String expressionString) throws ParseException {
4951
return parseExpression(expressionString, null);
@@ -136,7 +138,7 @@ private Expression[] parseExpressions(String expressionString, ParserContext con
136138
}
137139
}
138140

139-
return expressions.toArray(new Expression[0]);
141+
return expressions.toArray(EMPTY_EXPRESSION_ARRAY);
140142
}
141143

142144
/**

0 commit comments

Comments
 (0)