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
This commit is contained in:
Phillip Webb 2019-07-20 20:50:02 +01:00 committed by Juergen Hoeller
parent 71a5308c78
commit cca32a56a4
4 changed files with 20 additions and 10 deletions

View File

@ -48,6 +48,10 @@ import org.springframework.util.StringUtils;
*/ */
final class AnnotationTypeMapping { final class AnnotationTypeMapping {
private static final MirrorSet[] EMPTY_MIRROR_SETS = new MirrorSet[0];
@Nullable @Nullable
private final AnnotationTypeMapping source; private final AnnotationTypeMapping source;
@ -550,7 +554,7 @@ final class AnnotationTypeMapping {
MirrorSets() { MirrorSets() {
this.assigned = new MirrorSet[attributes.size()]; this.assigned = new MirrorSet[attributes.size()];
this.mirrorSets = new MirrorSet[0]; this.mirrorSets = EMPTY_MIRROR_SETS;
} }
void updateFrom(Collection<Method> aliases) { void updateFrom(Collection<Method> aliases) {
@ -575,7 +579,7 @@ final class AnnotationTypeMapping {
mirrorSet.update(); mirrorSet.update();
Set<MirrorSet> unique = new LinkedHashSet<>(Arrays.asList(this.assigned)); Set<MirrorSet> unique = new LinkedHashSet<>(Arrays.asList(this.assigned));
unique.remove(null); unique.remove(null);
this.mirrorSets = unique.toArray(new MirrorSet[0]); this.mirrorSets = unique.toArray(EMPTY_MIRROR_SETS);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,6 +38,9 @@ import org.springframework.lang.Nullable;
*/ */
public class MethodInvoker { public class MethodInvoker {
private static final Object[] EMPTY_ARGUMENTS = new Object[0];
@Nullable @Nullable
protected Class<?> targetClass; protected Class<?> targetClass;
@ -141,7 +144,7 @@ public class MethodInvoker {
* Return the arguments for the method invocation. * Return the arguments for the method invocation.
*/ */
public Object[] getArguments() { public Object[] getArguments() {
return (this.arguments != null ? this.arguments : new Object[0]); return (this.arguments != null ? this.arguments : EMPTY_ARGUMENTS);
} }

View File

@ -54,6 +54,7 @@ public abstract class ObjectUtils {
private static final String ARRAY_END = "}"; private static final String ARRAY_END = "}";
private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END; private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
private static final String ARRAY_ELEMENT_SEPARATOR = ", "; private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
/** /**
@ -282,14 +283,14 @@ public abstract class ObjectUtils {
return (Object[]) source; return (Object[]) source;
} }
if (source == null) { if (source == null) {
return new Object[0]; return EMPTY_OBJECT_ARRAY;
} }
if (!source.getClass().isArray()) { if (!source.getClass().isArray()) {
throw new IllegalArgumentException("Source is not an array: " + source); throw new IllegalArgumentException("Source is not an array: " + source);
} }
int length = Array.getLength(source); int length = Array.getLength(source);
if (length == 0) { if (length == 0) {
return new Object[0]; return EMPTY_OBJECT_ARRAY;
} }
Class<?> wrapperType = Array.get(source, 0).getClass(); Class<?> wrapperType = Array.get(source, 0).getClass();
Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);

View File

@ -60,6 +60,8 @@ import org.springframework.lang.Nullable;
*/ */
public abstract class StringUtils { public abstract class StringUtils {
private static final String[] EMPTY_STRING_ARRAY = {};
private static final String FOLDER_SEPARATOR = "/"; private static final String FOLDER_SEPARATOR = "/";
private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
@ -898,7 +900,7 @@ public abstract class StringUtils {
* @return the resulting {@code String} array * @return the resulting {@code String} array
*/ */
public static String[] toStringArray(@Nullable Collection<String> collection) { public static String[] toStringArray(@Nullable Collection<String> collection) {
return (collection != null ? collection.toArray(new String[0]) : new String[0]); return (collection != null || collection.isEmpty() ? collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY);
} }
/** /**
@ -909,7 +911,7 @@ public abstract class StringUtils {
* @return the resulting {@code String} array * @return the resulting {@code String} array
*/ */
public static String[] toStringArray(@Nullable Enumeration<String> enumeration) { public static String[] toStringArray(@Nullable Enumeration<String> enumeration) {
return (enumeration != null ? toStringArray(Collections.list(enumeration)) : new String[0]); return (enumeration != null ? toStringArray(Collections.list(enumeration)) : EMPTY_STRING_ARRAY);
} }
/** /**
@ -1151,7 +1153,7 @@ public abstract class StringUtils {
@Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { @Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {
if (str == null) { if (str == null) {
return new String[0]; return EMPTY_STRING_ARRAY;
} }
StringTokenizer st = new StringTokenizer(str, delimiters); StringTokenizer st = new StringTokenizer(str, delimiters);
@ -1204,7 +1206,7 @@ public abstract class StringUtils {
@Nullable String str, @Nullable String delimiter, @Nullable String charsToDelete) { @Nullable String str, @Nullable String delimiter, @Nullable String charsToDelete) {
if (str == null) { if (str == null) {
return new String[0]; return EMPTY_STRING_ARRAY;
} }
if (delimiter == null) { if (delimiter == null) {
return new String[] {str}; return new String[] {str};