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:
parent
d6644e4fbe
commit
96c8ea2fb3
|
@ -301,6 +301,7 @@ public class ArrayUtils {
|
||||||
* @return the array, not null unless a null array is passed in
|
* @return the array, not null unless a null array is passed in
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T[] toArray(final T... items) {
|
public static <T> T[] toArray(final T... items) {
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
@ -5058,6 +5059,7 @@ public class ArrayUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
* @throws IllegalArgumentException if the array types are incompatible
|
* @throws IllegalArgumentException if the array types are incompatible
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T[] addAll(final T[] array1, final T... array2) {
|
public static <T> T[] addAll(final T[] array1, final T... array2) {
|
||||||
if (array1 == null) {
|
if (array1 == null) {
|
||||||
return clone(array2);
|
return clone(array2);
|
||||||
|
@ -5066,8 +5068,7 @@ public class ArrayUtils {
|
||||||
}
|
}
|
||||||
final Class<?> type1 = array1.getClass().getComponentType();
|
final Class<?> type1 = array1.getClass().getComponentType();
|
||||||
@SuppressWarnings("unchecked") // OK, because array is of type T
|
@SuppressWarnings("unchecked") // OK, because array is of type T
|
||||||
final
|
final T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
|
||||||
T[] joinedArray = (T[]) Array.newInstance(type1, array1.length + array2.length);
|
|
||||||
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
|
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
|
||||||
try {
|
try {
|
||||||
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
|
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
|
||||||
|
@ -6607,6 +6608,7 @@ public class ArrayUtils {
|
||||||
* earliest-encountered occurrences of the specified elements.
|
* earliest-encountered occurrences of the specified elements.
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T[] removeElements(final T[] array, final T... values) {
|
public static <T> T[] removeElements(final T[] array, final T... values) {
|
||||||
if (isEmpty(array) || isEmpty(values)) {
|
if (isEmpty(array) || isEmpty(values)) {
|
||||||
return clone(array);
|
return clone(array);
|
||||||
|
@ -6632,8 +6634,7 @@ public class ArrayUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
|
@SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
|
||||||
final
|
final T[] result = (T[]) removeAll(array, toRemove);
|
||||||
T[] result = (T[]) removeAll(array, toRemove);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,6 +198,7 @@ public class EnumUtils {
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
* @see #generateBitVectors(Class, Iterable)
|
* @see #generateBitVectors(Class, Iterable)
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
|
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
|
||||||
Validate.noNullElements(values);
|
Validate.noNullElements(values);
|
||||||
return generateBitVector(enumClass, Arrays.<E> asList(values));
|
return generateBitVector(enumClass, Arrays.<E> 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}
|
* @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null}
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
|
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
|
||||||
asEnum(enumClass);
|
asEnum(enumClass);
|
||||||
Validate.noNullElements(values);
|
Validate.noNullElements(values);
|
||||||
|
|
|
@ -117,6 +117,7 @@ public class ObjectUtils {
|
||||||
* or {@code null} if there are no non-null values
|
* or {@code null} if there are no non-null values
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T firstNonNull(final T... values) {
|
public static <T> T firstNonNull(final T... values) {
|
||||||
if (values != null) {
|
if (values != null) {
|
||||||
for (final T val : values) {
|
for (final T val : values) {
|
||||||
|
@ -498,6 +499,7 @@ public class ObjectUtils {
|
||||||
* <li>If all the comparables are null, null is returned.
|
* <li>If all the comparables are null, null is returned.
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T extends Comparable<? super T>> T min(final T... values) {
|
public static <T extends Comparable<? super T>> T min(final T... values) {
|
||||||
T result = null;
|
T result = null;
|
||||||
if (values != null) {
|
if (values != null) {
|
||||||
|
@ -523,6 +525,7 @@ public class ObjectUtils {
|
||||||
* <li>If all the comparables are null, null is returned.
|
* <li>If all the comparables are null, null is returned.
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T extends Comparable<? super T>> T max(final T... values) {
|
public static <T extends Comparable<? super T>> T max(final T... values) {
|
||||||
T result = null;
|
T result = null;
|
||||||
if (values != null) {
|
if (values != null) {
|
||||||
|
@ -583,14 +586,14 @@ public class ObjectUtils {
|
||||||
* @throws IllegalArgumentException if items is empty or contains {@code null} values
|
* @throws IllegalArgumentException if items is empty or contains {@code null} values
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T extends Comparable<? super T>> T median(final T... items) {
|
public static <T extends Comparable<? super T>> T median(final T... items) {
|
||||||
Validate.notEmpty(items);
|
Validate.notEmpty(items);
|
||||||
Validate.noNullElements(items);
|
Validate.noNullElements(items);
|
||||||
final TreeSet<T> sort = new TreeSet<>();
|
final TreeSet<T> sort = new TreeSet<>();
|
||||||
Collections.addAll(sort, items);
|
Collections.addAll(sort, items);
|
||||||
@SuppressWarnings("unchecked") //we know all items added were T instances
|
@SuppressWarnings("unchecked") //we know all items added were T instances
|
||||||
final
|
final T result = (T) sort.toArray()[(sort.size() - 1) / 2];
|
||||||
T result = (T) sort.toArray()[(sort.size() - 1) / 2];
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -605,6 +608,7 @@ public class ObjectUtils {
|
||||||
* @throws IllegalArgumentException if items is empty or contains {@code null} values
|
* @throws IllegalArgumentException if items is empty or contains {@code null} values
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T median(final Comparator<T> comparator, final T... items) {
|
public static <T> T median(final Comparator<T> comparator, final T... items) {
|
||||||
Validate.notEmpty(items, "null/empty items");
|
Validate.notEmpty(items, "null/empty items");
|
||||||
Validate.noNullElements(items);
|
Validate.noNullElements(items);
|
||||||
|
@ -627,6 +631,7 @@ public class ObjectUtils {
|
||||||
* @return most populous T, {@code null} if non-unique or no items supplied
|
* @return most populous T, {@code null} if non-unique or no items supplied
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
*/
|
*/
|
||||||
|
@SafeVarargs
|
||||||
public static <T> T mode(final T... items) {
|
public static <T> T mode(final T... items) {
|
||||||
if (ArrayUtils.isNotEmpty(items)) {
|
if (ArrayUtils.isNotEmpty(items)) {
|
||||||
final HashMap<T, MutableInt> occurrences = new HashMap<>(items.length);
|
final HashMap<T, MutableInt> occurrences = new HashMap<>(items.length);
|
||||||
|
|
|
@ -218,6 +218,7 @@ public class ArrayUtilsTest {
|
||||||
assertEquals(0, array.length);
|
assertEquals(0, array.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
private static <T> T[] toArrayPropagatingType(final T... items)
|
private static <T> T[] toArrayPropagatingType(final T... items)
|
||||||
{
|
{
|
||||||
return ArrayUtils.toArray(items);
|
return ArrayUtils.toArray(items);
|
||||||
|
|
Loading…
Reference in New Issue