diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 85149ef9b..f8ae6956e 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -301,6 +301,7 @@ public class ArrayUtils { * @return the array, not null unless a null array is passed in * @since 3.0 */ + @SafeVarargs public static T[] toArray(final T... items) { return items; } @@ -5058,6 +5059,7 @@ public class ArrayUtils { * @since 2.1 * @throws IllegalArgumentException if the array types are incompatible */ + @SafeVarargs public static T[] addAll(final T[] array1, final T... array2) { if (array1 == null) { return clone(array2); @@ -5066,8 +5068,7 @@ public class ArrayUtils { } final Class type1 = array1.getClass().getComponentType(); @SuppressWarnings("unchecked") // OK, because array is of type T - final - T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length); + final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length); System.arraycopy(array1, 0, joinedArray, 0, array1.length); try { System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); @@ -6607,6 +6608,7 @@ public class ArrayUtils { * earliest-encountered occurrences of the specified elements. * @since 3.0.1 */ + @SafeVarargs public static T[] removeElements(final T[] array, final T... values) { if (isEmpty(array) || isEmpty(values)) { return clone(array); @@ -6632,8 +6634,7 @@ public class ArrayUtils { } } @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input - final - T[] result = (T[]) removeAll(array, toRemove); + final T[] result = (T[]) removeAll(array, toRemove); return result; } diff --git a/src/main/java/org/apache/commons/lang3/EnumUtils.java b/src/main/java/org/apache/commons/lang3/EnumUtils.java index 8280bf845..d6bcdc0c3 100644 --- a/src/main/java/org/apache/commons/lang3/EnumUtils.java +++ b/src/main/java/org/apache/commons/lang3/EnumUtils.java @@ -198,6 +198,7 @@ public class EnumUtils { * @since 3.0.1 * @see #generateBitVectors(Class, Iterable) */ + @SafeVarargs public static > long generateBitVector(final Class enumClass, final E... values) { Validate.noNullElements(values); return generateBitVector(enumClass, Arrays. asList(values)); @@ -219,6 +220,7 @@ public class EnumUtils { * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} * @since 3.2 */ + @SafeVarargs public static > long[] generateBitVectors(final Class enumClass, final E... values) { asEnum(enumClass); Validate.noNullElements(values); diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 84b4b1066..f951e7d74 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -117,6 +117,7 @@ public class ObjectUtils { * or {@code null} if there are no non-null values * @since 3.0 */ + @SafeVarargs public static T firstNonNull(final T... values) { if (values != null) { for (final T val : values) { @@ -498,6 +499,7 @@ public class ObjectUtils { *
  • If all the comparables are null, null is returned. * */ + @SafeVarargs public static > T min(final T... values) { T result = null; if (values != null) { @@ -523,6 +525,7 @@ public class ObjectUtils { *
  • If all the comparables are null, null is returned. * */ + @SafeVarargs public static > T max(final T... values) { T result = null; if (values != null) { @@ -583,14 +586,14 @@ public class ObjectUtils { * @throws IllegalArgumentException if items is empty or contains {@code null} values * @since 3.0.1 */ + @SafeVarargs public static > T median(final T... items) { Validate.notEmpty(items); Validate.noNullElements(items); final TreeSet sort = new TreeSet<>(); Collections.addAll(sort, items); @SuppressWarnings("unchecked") //we know all items added were T instances - final - T result = (T) sort.toArray()[(sort.size() - 1) / 2]; + final T result = (T) sort.toArray()[(sort.size() - 1) / 2]; return result; } @@ -605,6 +608,7 @@ public class ObjectUtils { * @throws IllegalArgumentException if items is empty or contains {@code null} values * @since 3.0.1 */ + @SafeVarargs public static T median(final Comparator comparator, final T... items) { Validate.notEmpty(items, "null/empty items"); Validate.noNullElements(items); @@ -627,6 +631,7 @@ public class ObjectUtils { * @return most populous T, {@code null} if non-unique or no items supplied * @since 3.0.1 */ + @SafeVarargs public static T mode(final T... items) { if (ArrayUtils.isNotEmpty(items)) { final HashMap occurrences = new HashMap<>(items.length); diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 7b2f57843..0cb553097 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -218,6 +218,7 @@ public class ArrayUtilsTest { assertEquals(0, array.length); } + @SafeVarargs private static T[] toArrayPropagatingType(final T... items) { return ArrayUtils.toArray(items);