add @SafeVarargs to some methods with vararg parameters to suppress "Type safety: Potential heap pollution via varargs parameter array" warnings

This commit is contained in:
pascalschumacher 2016-10-23 22:09:43 +02:00
parent d6644e4fbe
commit 96c8ea2fb3
4 changed files with 15 additions and 6 deletions

View File

@ -301,6 +301,7 @@ public static Map<Object, Object> toMap(final Object[] array) {
* @return the array, not null unless a null array is passed in
* @since 3.0
*/
@SafeVarargs
public static <T> T[] toArray(final T... items) {
return items;
}
@ -5058,6 +5059,7 @@ public static boolean isNotEmpty(final boolean[] array) {
* @since 2.1
* @throws IllegalArgumentException if the array types are incompatible
*/
@SafeVarargs
public static <T> T[] addAll(final T[] array1, final T... array2) {
if (array1 == null) {
return clone(array2);
@ -5066,8 +5068,7 @@ public static <T> T[] addAll(final T[] array1, final T... array2) {
}
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 static <T> T[] removeAll(final T[] array, final int... indices) {
* earliest-encountered occurrences of the specified elements.
* @since 3.0.1
*/
@SafeVarargs
public static <T> T[] removeElements(final T[] array, final T... values) {
if (isEmpty(array) || isEmpty(values)) {
return clone(array);
@ -6632,8 +6634,7 @@ public static <T> T[] removeElements(final T[] array, final T... values) {
}
}
@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;
}

View File

@ -198,6 +198,7 @@ public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumC
* @since 3.0.1
* @see #generateBitVectors(Class, Iterable)
*/
@SafeVarargs
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
Validate.noNullElements(values);
return generateBitVector(enumClass, Arrays.<E> asList(values));
@ -219,6 +220,7 @@ public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClas
* @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
* @since 3.2
*/
@SafeVarargs
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
asEnum(enumClass);
Validate.noNullElements(values);

View File

@ -117,6 +117,7 @@ public static <T> T defaultIfNull(final T object, final T defaultValue) {
* or {@code null} if there are no non-null values
* @since 3.0
*/
@SafeVarargs
public static <T> T firstNonNull(final T... values) {
if (values != null) {
for (final T val : values) {
@ -498,6 +499,7 @@ public static String toString(final Object obj, final String nullStr) {
* <li>If all the comparables are null, null is returned.
* </ul>
*/
@SafeVarargs
public static <T extends Comparable<? super T>> T min(final T... values) {
T result = null;
if (values != null) {
@ -523,6 +525,7 @@ public static <T extends Comparable<? super T>> T min(final T... values) {
* <li>If all the comparables are null, null is returned.
* </ul>
*/
@SafeVarargs
public static <T extends Comparable<? super T>> T max(final T... values) {
T result = null;
if (values != null) {
@ -583,14 +586,14 @@ public static <T extends Comparable<? super T>> int compare(final T c1, final T
* @throws IllegalArgumentException if items is empty or contains {@code null} values
* @since 3.0.1
*/
@SafeVarargs
public static <T extends Comparable<? super T>> T median(final T... items) {
Validate.notEmpty(items);
Validate.noNullElements(items);
final TreeSet<T> 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 static <T extends Comparable<? super T>> T median(final T... items) {
* @throws IllegalArgumentException if items is empty or contains {@code null} values
* @since 3.0.1
*/
@SafeVarargs
public static <T> T median(final Comparator<T> comparator, final T... items) {
Validate.notEmpty(items, "null/empty items");
Validate.noNullElements(items);
@ -627,6 +631,7 @@ public static <T> T median(final Comparator<T> comparator, final T... items) {
* @return most populous T, {@code null} if non-unique or no items supplied
* @since 3.0.1
*/
@SafeVarargs
public static <T> T mode(final T... items) {
if (ArrayUtils.isNotEmpty(items)) {
final HashMap<T, MutableInt> occurrences = new HashMap<>(items.length);

View File

@ -218,6 +218,7 @@ public void testIndirectEmptyArrayCreation()
assertEquals(0, array.length);
}
@SafeVarargs
private static <T> T[] toArrayPropagatingType(final T... items)
{
return ArrayUtils.toArray(items);