Deprecate Validate#notNull(Object) in favor of using

Objects#requireNonNull(Object, String).

Note that the NPE message are now specific in which parameter is null
instead of using a whole sentence to report what the exception class
says, and less US English-centric.
This commit is contained in:
Gary Gregory 2022-03-07 11:16:32 -05:00
parent 72ad3cbc1a
commit 13121dafec
6 changed files with 35 additions and 35 deletions

View File

@ -68,7 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in ThreadUtils.getSystemThreadGroup() when the current thread is stopped.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">ArrayUtils.toPrimitive(Boolean...) null array elements map to false, like Boolean.parseBoolean(null) and its callers return false.</action>
<action type="fix" dev="ggregory" due-to="CodeQL, Gary Gregory">StrBuilder.StrBuilderReader.skip(long): Throw an exception when an implicit narrowing conversion in a compound assignment would result in information loss or a numeric error such as an overflows.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate Validate#notNull(Object) in favor of using Objects#requireNonNull(Object, String).</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>

View File

@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -115,7 +116,7 @@ public class EnumUtils {
*/
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> values) {
checkBitVectorable(enumClass);
Validate.notNull(values);
Objects.requireNonNull(values, "values");
long total = 0;
for (final E constant : values) {
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
@ -172,7 +173,7 @@ public class EnumUtils {
*/
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
asEnum(enumClass);
Validate.notNull(values);
Objects.requireNonNull(values, "values");
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
for (final E constant : values) {
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
@ -419,7 +420,7 @@ public class EnumUtils {
*/
public static <E extends Enum<E>> EnumSet<E> processBitVectors(final Class<E> enumClass, final long... values) {
final EnumSet<E> results = EnumSet.noneOf(asEnum(enumClass));
final long[] lvalues = ArrayUtils.clone(Validate.notNull(values));
final long[] lvalues = ArrayUtils.clone(Objects.requireNonNull(values, "values"));
ArrayUtils.reverse(lvalues);
for (final E constant : enumClass.getEnumConstants()) {
final int block = constant.ordinal() / Long.SIZE;

View File

@ -201,7 +201,9 @@ public class Validate {
* @return the validated object (never {@code null} for method chaining)
* @throws NullPointerException if the object is {@code null}
* @see #notNull(Object, String, Object...)
* @deprecated Use {@link Objects#requireNonNull(Object)}.
*/
@Deprecated
public static <T> T notNull(final T object) {
return notNull(object, DEFAULT_IS_NULL_EX_MESSAGE);
}
@ -218,7 +220,7 @@ public class Validate {
* @param values the optional values for the formatted exception message
* @return the validated object (never {@code null} for method chaining)
* @throws NullPointerException if the object is {@code null}
* @see #notNull(Object)
* @see Objects#requireNonNull(Object)
*/
public static <T> T notNull(final T object, final String message, final Object... values) {
return Objects.requireNonNull(object, () -> String.format(message, values));
@ -493,7 +495,7 @@ public class Validate {
* @see #noNullElements(Object[])
*/
public static <T> T[] noNullElements(final T[] array, final String message, final Object... values) {
notNull(array);
Objects.requireNonNull(array, "array");
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
final Object[] values2 = ArrayUtils.add(values, Integer.valueOf(i));
@ -555,7 +557,7 @@ public class Validate {
* @see #noNullElements(Iterable)
*/
public static <T extends Iterable<?>> T noNullElements(final T iterable, final String message, final Object... values) {
notNull(iterable);
Objects.requireNonNull(iterable, "iterable");
int i = 0;
for (final Iterator<?> it = iterable.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
@ -616,7 +618,7 @@ public class Validate {
* @since 3.0
*/
public static <T> T[] validIndex(final T[] array, final int index, final String message, final Object... values) {
notNull(array);
Objects.requireNonNull(array, "array");
if (index < 0 || index >= array.length) {
throw new IndexOutOfBoundsException(String.format(message, values));
}
@ -675,7 +677,7 @@ public class Validate {
* @since 3.0
*/
public static <T extends Collection<?>> T validIndex(final T collection, final int index, final String message, final Object... values) {
notNull(collection);
Objects.requireNonNull(collection, "collection");
if (index < 0 || index >= collection.size()) {
throw new IndexOutOfBoundsException(String.format(message, values));
}
@ -732,7 +734,7 @@ public class Validate {
* @since 3.0
*/
public static <T extends CharSequence> T validIndex(final T chars, final int index, final String message, final Object... values) {
notNull(chars);
Objects.requireNonNull(chars, "chars");
if (index < 0 || index >= chars.length()) {
throw new IndexOutOfBoundsException(String.format(message, values));
}

View File

@ -324,18 +324,15 @@ public class MethodUtils {
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/
public static Object invokeExactMethod(final Object object, final String methodName,
Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
public static Object invokeExactMethod(final Object object, final String methodName, Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Objects.requireNonNull(object, "object");
args = ArrayUtils.nullToEmpty(args);
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
final Method method = getAccessibleMethod(object.getClass(), methodName,
parameterTypes);
final Class<? extends Object> cls = object.getClass();
final Method method = getAccessibleMethod(cls, methodName, parameterTypes);
if (method == null) {
throw new NoSuchMethodException("No such accessible method: "
+ methodName + "() on object: "
+ object.getClass().getName());
throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + cls.getName());
}
return method.invoke(object, args);
}
@ -734,7 +731,7 @@ public class MethodUtils {
*/
public static Method getMatchingMethod(final Class<?> cls, final String methodName,
final Class<?>... parameterTypes) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
Validate.notEmpty(methodName, "methodName");
final List<Method> methods = Stream.of(cls.getDeclaredMethods())
@ -820,7 +817,7 @@ public class MethodUtils {
* @since 3.2
*/
public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
Validate.notNull(method);
Objects.requireNonNull(method, "method");
final Set<Method> result = new LinkedHashSet<>();
result.add(method);
@ -922,7 +919,7 @@ public class MethodUtils {
final Class<? extends Annotation> annotationCls,
final boolean searchSupers, final boolean ignoreAccess) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
Validate.notNull(annotationCls, "annotationCls");
final List<Class<?>> classes = searchSupers ? getAllSuperclassesAndInterfaces(cls) : new ArrayList<>();
classes.add(0, cls);
@ -959,8 +956,8 @@ public class MethodUtils {
public static <A extends Annotation> A getAnnotation(final Method method, final Class<A> annotationCls,
final boolean searchSupers, final boolean ignoreAccess) {
Validate.notNull(method, "method");
Validate.notNull(annotationCls, "annotationCls");
Objects.requireNonNull(method, "method");
Objects.requireNonNull(annotationCls, "annotationCls");
if (!ignoreAccess && !MemberUtils.isAccessible(method)) {
return null;
}

View File

@ -1709,7 +1709,7 @@ public class TypeUtils {
* @since 3.2
*/
public static String toString(final Type type) {
Validate.notNull(type);
Objects.requireNonNull(type, "type");
if (type instanceof Class<?>) {
return classToString((Class<?>) type);
}

View File

@ -650,7 +650,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.noNullElements((Object[]) null));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("array", ex.getMessage());
}
@Test
@ -685,7 +685,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.noNullElements((Object[]) null, "MSG"));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("array", ex.getMessage());
}
@Test
@ -724,7 +724,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.noNullElements((Collection<?>) null));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("iterable", ex.getMessage());
}
@Test
@ -759,7 +759,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.noNullElements((Collection<?>) null, "MSG"));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("iterable", ex.getMessage());
}
@Test
@ -802,7 +802,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((Object[]) null, 1));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("array", ex.getMessage());
}
@Test
@ -846,7 +846,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((Object[]) null, 1, "MSG"));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("array", ex.getMessage());
}
@Test
@ -894,7 +894,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((Collection<?>) null, 1));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("collection", ex.getMessage());
}
@Test
@ -938,7 +938,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((Collection<?>) null, 1, "MSG"));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("collection", ex.getMessage());
}
@Test
@ -986,7 +986,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((String) null, 1));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("chars", ex.getMessage());
}
@Test
@ -1030,7 +1030,7 @@ class ValidateTest {
NullPointerException.class,
() -> Validate.validIndex((String) null, 1, "MSG"));
assertEquals("The validated object is null", ex.getMessage());
assertEquals("chars", ex.getMessage());
}
@Test