Consistent use of Collection.toArray with zero-sized array argument

Includes consistent use of ClassUtils.toClassArray (as non-null variant)

Issue: SPR-16523
This commit is contained in:
Juergen Hoeller
2018-02-22 11:29:46 +01:00
parent 1ab3f88e82
commit a5cbf5fe24
72 changed files with 208 additions and 233 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -211,7 +211,7 @@ public final class Property {
addAnnotationsToMap(annotationMap, getReadMethod());
addAnnotationsToMap(annotationMap, getWriteMethod());
addAnnotationsToMap(annotationMap, getField());
annotations = annotationMap.values().toArray(new Annotation[annotationMap.size()]);
annotations = annotationMap.values().toArray(new Annotation[0]);
annotationCache.put(this, annotations);
}
return annotations;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -320,7 +320,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
if (logger.isDebugEnabled()) {
logger.debug("Resolved classpath location [" + location + "] to resources " + result);
}
return result.toArray(new Resource[result.size()]);
return result.toArray(new Resource[0]);
}
/**
@@ -515,7 +515,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
if (logger.isDebugEnabled()) {
logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
}
return result.toArray(new Resource[result.size()]);
return result.toArray(new Resource[0]);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -162,7 +162,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
Resource.class.getName() + "]: only location String and Resource object supported");
}
}
super.setValue(merged.toArray(new Resource[merged.size()]));
super.setValue(merged.toArray(new Resource[0]));
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -78,8 +78,7 @@ class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationVisitor
@Override
public void visitEnd() {
if (!this.allNestedAttributes.isEmpty()) {
this.attributes.put(this.attributeName,
this.allNestedAttributes.toArray(new AnnotationAttributes[this.allNestedAttributes.size()]));
this.attributes.put(this.attributeName, this.allNestedAttributes.toArray(new AnnotationAttributes[0]));
}
}

View File

@@ -1058,18 +1058,15 @@ public abstract class ClassUtils {
}
/**
* Copy the given Collection into a Class array.
* The Collection must contain Class elements only.
* @param collection the Collection to copy
* @return the Class array ({@code null} if the passed-in
* Collection was {@code null})
* Copy the given {@code Collection} into a {@code Class} array.
* <p>The {@code Collection} must contain {@code Class} elements only.
* @param collection the {@code Collection} to copy
* @return the {@code Class} array
* @since 3.1
* @see StringUtils#toStringArray
*/
@Nullable
public static Class<?>[] toClassArray(@Nullable Collection<Class<?>> collection) {
if (collection == null) {
return null;
}
return collection.toArray(new Class<?>[collection.size()]);
public static Class<?>[] toClassArray(Collection<Class<?>> collection) {
return collection.toArray(new Class<?>[0]);
}
/**
@@ -1104,8 +1101,7 @@ public abstract class ClassUtils {
* @return all interfaces that the given object implements as an array
*/
public static Class<?>[] getAllInterfacesForClass(Class<?> clazz, @Nullable ClassLoader classLoader) {
Set<Class<?>> ifcs = getAllInterfacesForClassAsSet(clazz, classLoader);
return ifcs.toArray(new Class<?>[ifcs.size()]);
return toClassArray(getAllInterfacesForClassAsSet(clazz, classLoader));
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -584,7 +584,7 @@ public abstract class ReflectionUtils {
public static Method[] getAllDeclaredMethods(Class<?> leafClass) {
final List<Method> methods = new ArrayList<>(32);
doWithMethods(leafClass, methods::add);
return methods.toArray(new Method[methods.size()]);
return methods.toArray(new Method[0]);
}
/**
@@ -620,7 +620,7 @@ public abstract class ReflectionUtils {
methods.add(method);
}
});
return methods.toArray(new Method[methods.size()]);
return methods.toArray(new Method[0]);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -229,7 +229,7 @@ public class StopWatch {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
}
return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
return this.taskList.toArray(new TaskInfo[0]);
}

View File

@@ -945,7 +945,7 @@ public abstract class StringUtils {
* @return the {@code String} array
*/
public static String[] toStringArray(Collection<String> collection) {
return collection.toArray(new String[collection.size()]);
return collection.toArray(new String[0]);
}
/**
@@ -955,8 +955,7 @@ public abstract class StringUtils {
* @return the {@code String} array
*/
public static String[] toStringArray(Enumeration<String> enumeration) {
List<String> list = Collections.list(enumeration);
return list.toArray(new String[list.size()]);
return toStringArray(Collections.list(enumeration));
}
/**