Skip to content

Commit cca32a5

Browse files
philwebbjhoeller
authored andcommitted
Use shared zero length array constants
Update code that's often called so that zero length array results use a single shared static constant, rather than a new instance for each call. Closes gh-23340
1 parent 71a5308 commit cca32a5

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
*/
4949
final class AnnotationTypeMapping {
5050

51+
52+
private static final MirrorSet[] EMPTY_MIRROR_SETS = new MirrorSet[0];
53+
54+
5155
@Nullable
5256
private final AnnotationTypeMapping source;
5357

@@ -550,7 +554,7 @@ class MirrorSets {
550554

551555
MirrorSets() {
552556
this.assigned = new MirrorSet[attributes.size()];
553-
this.mirrorSets = new MirrorSet[0];
557+
this.mirrorSets = EMPTY_MIRROR_SETS;
554558
}
555559

556560
void updateFrom(Collection<Method> aliases) {
@@ -575,7 +579,7 @@ void updateFrom(Collection<Method> aliases) {
575579
mirrorSet.update();
576580
Set<MirrorSet> unique = new LinkedHashSet<>(Arrays.asList(this.assigned));
577581
unique.remove(null);
578-
this.mirrorSets = unique.toArray(new MirrorSet[0]);
582+
this.mirrorSets = unique.toArray(EMPTY_MIRROR_SETS);
579583
}
580584
}
581585

spring-core/src/main/java/org/springframework/util/MethodInvoker.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -38,6 +38,9 @@
3838
*/
3939
public class MethodInvoker {
4040

41+
private static final Object[] EMPTY_ARGUMENTS = new Object[0];
42+
43+
4144
@Nullable
4245
protected Class<?> targetClass;
4346

@@ -141,7 +144,7 @@ public void setArguments(Object... arguments) {
141144
* Return the arguments for the method invocation.
142145
*/
143146
public Object[] getArguments() {
144-
return (this.arguments != null ? this.arguments : new Object[0]);
147+
return (this.arguments != null ? this.arguments : EMPTY_ARGUMENTS);
145148
}
146149

147150

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract class ObjectUtils {
5454
private static final String ARRAY_END = "}";
5555
private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
5656
private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
57+
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
5758

5859

5960
/**
@@ -282,14 +283,14 @@ public static Object[] toObjectArray(@Nullable Object source) {
282283
return (Object[]) source;
283284
}
284285
if (source == null) {
285-
return new Object[0];
286+
return EMPTY_OBJECT_ARRAY;
286287
}
287288
if (!source.getClass().isArray()) {
288289
throw new IllegalArgumentException("Source is not an array: " + source);
289290
}
290291
int length = Array.getLength(source);
291292
if (length == 0) {
292-
return new Object[0];
293+
return EMPTY_OBJECT_ARRAY;
293294
}
294295
Class<?> wrapperType = Array.get(source, 0).getClass();
295296
Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
*/
6161
public abstract class StringUtils {
6262

63+
private static final String[] EMPTY_STRING_ARRAY = {};
64+
6365
private static final String FOLDER_SEPARATOR = "/";
6466

6567
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
@@ -898,7 +900,7 @@ public static TimeZone parseTimeZoneString(String timeZoneString) {
898900
* @return the resulting {@code String} array
899901
*/
900902
public static String[] toStringArray(@Nullable Collection<String> collection) {
901-
return (collection != null ? collection.toArray(new String[0]) : new String[0]);
903+
return (collection != null || collection.isEmpty() ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY);
902904
}
903905

904906
/**
@@ -909,7 +911,7 @@ public static String[] toStringArray(@Nullable Collection<String> collection) {
909911
* @return the resulting {@code String} array
910912
*/
911913
public static String[] toStringArray(@Nullable Enumeration<String> enumeration) {
912-
return (enumeration != null ? toStringArray(Collections.list(enumeration)) : new String[0]);
914+
return (enumeration != null ? toStringArray(Collections.list(enumeration)) : EMPTY_STRING_ARRAY);
913915
}
914916

915917
/**
@@ -1151,7 +1153,7 @@ public static String[] tokenizeToStringArray(
11511153
@Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {
11521154

11531155
if (str == null) {
1154-
return new String[0];
1156+
return EMPTY_STRING_ARRAY;
11551157
}
11561158

11571159
StringTokenizer st = new StringTokenizer(str, delimiters);
@@ -1204,7 +1206,7 @@ public static String[] delimitedListToStringArray(
12041206
@Nullable String str, @Nullable String delimiter, @Nullable String charsToDelete) {
12051207

12061208
if (str == null) {
1207-
return new String[0];
1209+
return EMPTY_STRING_ARRAY;
12081210
}
12091211
if (delimiter == null) {
12101212
return new String[] {str};

0 commit comments

Comments
 (0)