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:
parent
72ad3cbc1a
commit
13121dafec
|
@ -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">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="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="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 -->
|
<!-- ADD -->
|
||||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
|
<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>
|
<action type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
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) {
|
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> values) {
|
||||||
checkBitVectorable(enumClass);
|
checkBitVectorable(enumClass);
|
||||||
Validate.notNull(values);
|
Objects.requireNonNull(values, "values");
|
||||||
long total = 0;
|
long total = 0;
|
||||||
for (final E constant : values) {
|
for (final E constant : values) {
|
||||||
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
|
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) {
|
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
|
||||||
asEnum(enumClass);
|
asEnum(enumClass);
|
||||||
Validate.notNull(values);
|
Objects.requireNonNull(values, "values");
|
||||||
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
|
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
|
||||||
for (final E constant : values) {
|
for (final E constant : values) {
|
||||||
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
|
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) {
|
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 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);
|
ArrayUtils.reverse(lvalues);
|
||||||
for (final E constant : enumClass.getEnumConstants()) {
|
for (final E constant : enumClass.getEnumConstants()) {
|
||||||
final int block = constant.ordinal() / Long.SIZE;
|
final int block = constant.ordinal() / Long.SIZE;
|
||||||
|
|
|
@ -201,7 +201,9 @@ public class Validate {
|
||||||
* @return the validated object (never {@code null} for method chaining)
|
* @return the validated object (never {@code null} for method chaining)
|
||||||
* @throws NullPointerException if the object is {@code null}
|
* @throws NullPointerException if the object is {@code null}
|
||||||
* @see #notNull(Object, String, Object...)
|
* @see #notNull(Object, String, Object...)
|
||||||
|
* @deprecated Use {@link Objects#requireNonNull(Object)}.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static <T> T notNull(final T object) {
|
public static <T> T notNull(final T object) {
|
||||||
return notNull(object, DEFAULT_IS_NULL_EX_MESSAGE);
|
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
|
* @param values the optional values for the formatted exception message
|
||||||
* @return the validated object (never {@code null} for method chaining)
|
* @return the validated object (never {@code null} for method chaining)
|
||||||
* @throws NullPointerException if the object is {@code null}
|
* @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) {
|
public static <T> T notNull(final T object, final String message, final Object... values) {
|
||||||
return Objects.requireNonNull(object, () -> String.format(message, values));
|
return Objects.requireNonNull(object, () -> String.format(message, values));
|
||||||
|
@ -493,7 +495,7 @@ public class Validate {
|
||||||
* @see #noNullElements(Object[])
|
* @see #noNullElements(Object[])
|
||||||
*/
|
*/
|
||||||
public static <T> T[] noNullElements(final T[] array, final String message, final Object... values) {
|
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++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
if (array[i] == null) {
|
if (array[i] == null) {
|
||||||
final Object[] values2 = ArrayUtils.add(values, Integer.valueOf(i));
|
final Object[] values2 = ArrayUtils.add(values, Integer.valueOf(i));
|
||||||
|
@ -555,7 +557,7 @@ public class Validate {
|
||||||
* @see #noNullElements(Iterable)
|
* @see #noNullElements(Iterable)
|
||||||
*/
|
*/
|
||||||
public static <T extends Iterable<?>> T noNullElements(final T iterable, final String message, final Object... values) {
|
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;
|
int i = 0;
|
||||||
for (final Iterator<?> it = iterable.iterator(); it.hasNext(); i++) {
|
for (final Iterator<?> it = iterable.iterator(); it.hasNext(); i++) {
|
||||||
if (it.next() == null) {
|
if (it.next() == null) {
|
||||||
|
@ -616,7 +618,7 @@ public class Validate {
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public static <T> T[] validIndex(final T[] array, final int index, final String message, final Object... values) {
|
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) {
|
if (index < 0 || index >= array.length) {
|
||||||
throw new IndexOutOfBoundsException(String.format(message, values));
|
throw new IndexOutOfBoundsException(String.format(message, values));
|
||||||
}
|
}
|
||||||
|
@ -675,7 +677,7 @@ public class Validate {
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public static <T extends Collection<?>> T validIndex(final T collection, final int index, final String message, final Object... values) {
|
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()) {
|
if (index < 0 || index >= collection.size()) {
|
||||||
throw new IndexOutOfBoundsException(String.format(message, values));
|
throw new IndexOutOfBoundsException(String.format(message, values));
|
||||||
}
|
}
|
||||||
|
@ -732,7 +734,7 @@ public class Validate {
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public static <T extends CharSequence> T validIndex(final T chars, final int index, final String message, final Object... values) {
|
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()) {
|
if (index < 0 || index >= chars.length()) {
|
||||||
throw new IndexOutOfBoundsException(String.format(message, values));
|
throw new IndexOutOfBoundsException(String.format(message, values));
|
||||||
}
|
}
|
||||||
|
|
|
@ -324,18 +324,15 @@ public class MethodUtils {
|
||||||
* @throws IllegalAccessException if the requested method is not accessible
|
* @throws IllegalAccessException if the requested method is not accessible
|
||||||
* via reflection
|
* via reflection
|
||||||
*/
|
*/
|
||||||
public static Object invokeExactMethod(final Object object, final String methodName,
|
public static Object invokeExactMethod(final Object object, final String methodName, Object[] args, Class<?>[] parameterTypes)
|
||||||
Object[] args, Class<?>[] parameterTypes)
|
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
|
||||||
throws NoSuchMethodException, IllegalAccessException,
|
Objects.requireNonNull(object, "object");
|
||||||
InvocationTargetException {
|
|
||||||
args = ArrayUtils.nullToEmpty(args);
|
args = ArrayUtils.nullToEmpty(args);
|
||||||
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
|
||||||
final Method method = getAccessibleMethod(object.getClass(), methodName,
|
final Class<? extends Object> cls = object.getClass();
|
||||||
parameterTypes);
|
final Method method = getAccessibleMethod(cls, methodName, parameterTypes);
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
throw new NoSuchMethodException("No such accessible method: "
|
throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + cls.getName());
|
||||||
+ methodName + "() on object: "
|
|
||||||
+ object.getClass().getName());
|
|
||||||
}
|
}
|
||||||
return method.invoke(object, args);
|
return method.invoke(object, args);
|
||||||
}
|
}
|
||||||
|
@ -734,7 +731,7 @@ public class MethodUtils {
|
||||||
*/
|
*/
|
||||||
public static Method getMatchingMethod(final Class<?> cls, final String methodName,
|
public static Method getMatchingMethod(final Class<?> cls, final String methodName,
|
||||||
final Class<?>... parameterTypes) {
|
final Class<?>... parameterTypes) {
|
||||||
Validate.notNull(cls, "cls");
|
Objects.requireNonNull(cls, "cls");
|
||||||
Validate.notEmpty(methodName, "methodName");
|
Validate.notEmpty(methodName, "methodName");
|
||||||
|
|
||||||
final List<Method> methods = Stream.of(cls.getDeclaredMethods())
|
final List<Method> methods = Stream.of(cls.getDeclaredMethods())
|
||||||
|
@ -820,7 +817,7 @@ public class MethodUtils {
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
|
public static Set<Method> getOverrideHierarchy(final Method method, final Interfaces interfacesBehavior) {
|
||||||
Validate.notNull(method);
|
Objects.requireNonNull(method, "method");
|
||||||
final Set<Method> result = new LinkedHashSet<>();
|
final Set<Method> result = new LinkedHashSet<>();
|
||||||
result.add(method);
|
result.add(method);
|
||||||
|
|
||||||
|
@ -922,7 +919,7 @@ public class MethodUtils {
|
||||||
final Class<? extends Annotation> annotationCls,
|
final Class<? extends Annotation> annotationCls,
|
||||||
final boolean searchSupers, final boolean ignoreAccess) {
|
final boolean searchSupers, final boolean ignoreAccess) {
|
||||||
|
|
||||||
Validate.notNull(cls, "cls");
|
Objects.requireNonNull(cls, "cls");
|
||||||
Validate.notNull(annotationCls, "annotationCls");
|
Validate.notNull(annotationCls, "annotationCls");
|
||||||
final List<Class<?>> classes = searchSupers ? getAllSuperclassesAndInterfaces(cls) : new ArrayList<>();
|
final List<Class<?>> classes = searchSupers ? getAllSuperclassesAndInterfaces(cls) : new ArrayList<>();
|
||||||
classes.add(0, cls);
|
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,
|
public static <A extends Annotation> A getAnnotation(final Method method, final Class<A> annotationCls,
|
||||||
final boolean searchSupers, final boolean ignoreAccess) {
|
final boolean searchSupers, final boolean ignoreAccess) {
|
||||||
|
|
||||||
Validate.notNull(method, "method");
|
Objects.requireNonNull(method, "method");
|
||||||
Validate.notNull(annotationCls, "annotationCls");
|
Objects.requireNonNull(annotationCls, "annotationCls");
|
||||||
if (!ignoreAccess && !MemberUtils.isAccessible(method)) {
|
if (!ignoreAccess && !MemberUtils.isAccessible(method)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1709,7 +1709,7 @@ public class TypeUtils {
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public static String toString(final Type type) {
|
public static String toString(final Type type) {
|
||||||
Validate.notNull(type);
|
Objects.requireNonNull(type, "type");
|
||||||
if (type instanceof Class<?>) {
|
if (type instanceof Class<?>) {
|
||||||
return classToString((Class<?>) type);
|
return classToString((Class<?>) type);
|
||||||
}
|
}
|
||||||
|
|
|
@ -650,7 +650,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.noNullElements((Object[]) null));
|
() -> Validate.noNullElements((Object[]) null));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("array", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -685,7 +685,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.noNullElements((Object[]) null, "MSG"));
|
() -> Validate.noNullElements((Object[]) null, "MSG"));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("array", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -724,7 +724,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.noNullElements((Collection<?>) null));
|
() -> Validate.noNullElements((Collection<?>) null));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("iterable", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -759,7 +759,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.noNullElements((Collection<?>) null, "MSG"));
|
() -> Validate.noNullElements((Collection<?>) null, "MSG"));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("iterable", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -802,7 +802,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((Object[]) null, 1));
|
() -> Validate.validIndex((Object[]) null, 1));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("array", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -846,7 +846,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((Object[]) null, 1, "MSG"));
|
() -> Validate.validIndex((Object[]) null, 1, "MSG"));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("array", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -894,7 +894,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((Collection<?>) null, 1));
|
() -> Validate.validIndex((Collection<?>) null, 1));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("collection", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -938,7 +938,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((Collection<?>) null, 1, "MSG"));
|
() -> Validate.validIndex((Collection<?>) null, 1, "MSG"));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("collection", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -986,7 +986,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((String) null, 1));
|
() -> Validate.validIndex((String) null, 1));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("chars", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1030,7 +1030,7 @@ class ValidateTest {
|
||||||
NullPointerException.class,
|
NullPointerException.class,
|
||||||
() -> Validate.validIndex((String) null, 1, "MSG"));
|
() -> Validate.validIndex((String) null, 1, "MSG"));
|
||||||
|
|
||||||
assertEquals("The validated object is null", ex.getMessage());
|
assertEquals("chars", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue