From fe0c2d6b6e46d087fb483170c59888a5a7be152b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 8 Mar 2019 22:40:18 +0100 Subject: [PATCH] StringUtils.toStringArray/CollectionUtils.toIterator handle null input Closes gh-22547 --- .../springframework/util/CollectionUtils.java | 12 ++--- .../org/springframework/util/StringUtils.java | 52 ++++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index e27d5eef0d..46215b173f 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -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"); * you may not use this file except in compliance with the License. @@ -369,12 +369,12 @@ public abstract class CollectionUtils { } /** - * Adapt an enumeration to an iterator. - * @param enumeration the enumeration - * @return the iterator + * Adapt an {@link Enumeration} to an {@link Iterator}. + * @param enumeration the original {@code Enumeration} + * @return the adapted {@code Iterator} */ - public static Iterator toIterator(Enumeration enumeration) { - return new EnumerationIterator<>(enumeration); + public static Iterator toIterator(@Nullable Enumeration enumeration) { + return (enumeration != null ? new EnumerationIterator<>(enumeration) : Collections.emptyIterator()); } /** diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 6fc8a14018..ce1280ee48 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -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"); * you may not use this file except in compliance with the License. @@ -872,6 +872,28 @@ public abstract class StringUtils { // Convenience methods for working with String arrays //--------------------------------------------------------------------- + /** + * Copy the given {@link Collection} into a {@code String} array. + *

The {@code Collection} must contain {@code String} elements only. + * @param collection the {@code Collection} to copy + * (potentially {@code null} or empty) + * @return the resulting {@code String} array + */ + public static String[] toStringArray(@Nullable Collection collection) { + return (collection != null ? collection.toArray(new String[0]) : new String[0]); + } + + /** + * Copy the given {@link Enumeration} into a {@code String} array. + *

The {@code Enumeration} must contain {@code String} elements only. + * @param enumeration the {@code Enumeration} to copy + * (potentially {@code null} or empty) + * @return the resulting {@code String} array + */ + public static String[] toStringArray(@Nullable Enumeration enumeration) { + return (enumeration != null ? toStringArray(Collections.list(enumeration)) : new String[0]); + } + /** * Append the given {@code String} to the given {@code String} array, * returning a new array consisting of the input array contents plus @@ -947,39 +969,19 @@ public abstract class StringUtils { } /** - * Turn given source {@code String} array into sorted array. - * @param array the source array - * @return the sorted array (never {@code null}) + * Sort the given {@code String} array if necessary. + * @param array the original array (potentially empty) + * @return the array in sorted form (never {@code null}) */ public static String[] sortStringArray(String[] array) { if (ObjectUtils.isEmpty(array)) { - return new String[0]; + return array; } Arrays.sort(array); return array; } - /** - * Copy the given {@code Collection} into a {@code String} array. - *

The {@code Collection} must contain {@code String} elements only. - * @param collection the {@code Collection} to copy - * @return the {@code String} array - */ - public static String[] toStringArray(Collection collection) { - return collection.toArray(new String[0]); - } - - /** - * Copy the given Enumeration into a {@code String} array. - * The Enumeration must contain {@code String} elements only. - * @param enumeration the Enumeration to copy - * @return the {@code String} array - */ - public static String[] toStringArray(Enumeration enumeration) { - return toStringArray(Collections.list(enumeration)); - } - /** * Trim the elements of the given {@code String} array, * calling {@code String.trim()} on each of them.