Use Objects.requireNonNull() directly

This commit is contained in:
Gary Gregory 2022-11-04 14:42:11 -04:00
parent ecd23d5e20
commit d839a82521
23 changed files with 108 additions and 110 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
/**
* A contiguous range of characters, optionally negated.
@ -189,7 +190,7 @@ final class CharRange implements Iterable<Character>, Serializable {
* @throws NullPointerException if {@code null} input
*/
public boolean contains(final CharRange range) {
Validate.notNull(range, "range");
Objects.requireNonNull(range, "range");
if (negated) {
if (range.negated) {
return start >= range.start && end <= range.end;

View File

@ -69,8 +69,8 @@ public class ClassPathUtils {
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedName(final Class<?> context, final String resourceName) {
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
Objects.requireNonNull(context, "context");
Objects.requireNonNull(resourceName, "resourceName");
return toFullyQualifiedName(context.getPackage(), resourceName);
}
@ -91,8 +91,8 @@ public class ClassPathUtils {
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedName(final Package context, final String resourceName) {
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
Objects.requireNonNull(context, "context");
Objects.requireNonNull(resourceName, "resourceName");
return context.getName() + "." + resourceName;
}
@ -113,8 +113,8 @@ public class ClassPathUtils {
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) {
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
Objects.requireNonNull(context, "context");
Objects.requireNonNull(resourceName, "resourceName");
return toFullyQualifiedPath(context.getPackage(), resourceName);
}
@ -135,8 +135,8 @@ public class ClassPathUtils {
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedPath(final Package context, final String resourceName) {
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
Objects.requireNonNull(context, "context");
Objects.requireNonNull(resourceName, "resourceName");
return packageToPath(context.getName()) + "/" + resourceName;
}

View File

@ -1502,7 +1502,7 @@ public class ClassUtils {
*/
private static String toCanonicalName(final String className) {
String canonicalName = StringUtils.deleteWhitespace(className);
Validate.notNull(canonicalName, "className");
Objects.requireNonNull(canonicalName, "className");
if (canonicalName.endsWith("[]")) {
final StringBuilder classNameBuffer = new StringBuilder();
while (canonicalName.endsWith("[]")) {

View File

@ -51,7 +51,7 @@ public class EnumUtils {
* @since 3.2
*/
private static <E extends Enum<E>> Class<E> asEnum(final Class<E> enumClass) {
Validate.notNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED);
Objects.requireNonNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED);
Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass);
return enumClass;
}
@ -119,7 +119,7 @@ public class EnumUtils {
Objects.requireNonNull(values, "values");
long total = 0;
for (final E constant : values) {
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
Objects.requireNonNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
total |= 1L << constant.ordinal();
}
return total;
@ -175,7 +175,7 @@ public class EnumUtils {
asEnum(enumClass);
Objects.requireNonNull(values, "values");
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
values.forEach(constant -> condensed.add(Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED)));
values.forEach(constant -> condensed.add(Objects.requireNonNull(constant, NULL_ELEMENTS_NOT_PERMITTED)));
final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
for (final E value : condensed) {
result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);

View File

@ -277,7 +277,7 @@ public class Range<T> implements Serializable {
*/
public int elementCompareTo(final T element) {
// Comparable API says throw NPE on null
Validate.notNull(element, "element");
Objects.requireNonNull(element, "element");
if (isAfter(element)) {
return -1;
}
@ -333,7 +333,7 @@ public class Range<T> implements Serializable {
*/
public T fit(final T element) {
// Comparable API says throw NPE on null
Validate.notNull(element, "element");
Objects.requireNonNull(element, "element");
if (isAfter(element)) {
return minimum;
}

View File

@ -27,6 +27,7 @@ import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Assists with the serialization process and performs additional functionality based
@ -169,7 +170,7 @@ public class SerializationUtils {
* @throws SerializationException (runtime) if the serialization fails
*/
public static <T> T deserialize(final byte[] objectData) {
Validate.notNull(objectData, "objectData");
Objects.requireNonNull(objectData, "objectData");
return deserialize(new ByteArrayInputStream(objectData));
}
@ -201,7 +202,7 @@ public class SerializationUtils {
*/
@SuppressWarnings("resource") // inputStream is managed by the caller
public static <T> T deserialize(final InputStream inputStream) {
Validate.notNull(inputStream, "inputStream");
Objects.requireNonNull(inputStream, "inputStream");
try (ObjectInputStream in = new ObjectInputStream(inputStream)) {
@SuppressWarnings("unchecked")
final T obj = (T) in.readObject();
@ -258,7 +259,7 @@ public class SerializationUtils {
*/
@SuppressWarnings("resource") // outputStream is managed by the caller
public static void serialize(final Serializable obj, final OutputStream outputStream) {
Validate.notNull(outputStream, "outputStream");
Objects.requireNonNull(outputStream, "outputStream");
try (ObjectOutputStream out = new ObjectOutputStream(outputStream)) {
out.writeObject(obj);
} catch (final IOException ex) {

View File

@ -82,7 +82,7 @@ public class ThreadUtils {
* @throws NullPointerException if the name is {@code null}
*/
public NamePredicate(final String name) {
Validate.notNull(name, "name");
Objects.requireNonNull(name, "name");
this.name = name;
}
@ -213,7 +213,7 @@ public class ThreadUtils {
* thread groups from this thread's thread group up to the system thread group
*/
public static Thread findThreadById(final long threadId, final String threadGroupName) {
Validate.notNull(threadGroupName, "threadGroupName");
Objects.requireNonNull(threadGroupName, "threadGroupName");
final Thread thread = findThreadById(threadId);
if (thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
return thread;
@ -237,7 +237,7 @@ public class ThreadUtils {
* thread groups from this thread's thread group up to the system thread group
*/
public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
Validate.notNull(threadGroup, "threadGroup");
Objects.requireNonNull(threadGroup, "threadGroup");
final Thread thread = findThreadById(threadId);
if (thread != null && threadGroup.equals(thread.getThreadGroup())) {
return thread;
@ -274,8 +274,8 @@ public class ThreadUtils {
* @since 3.13.0
*/
public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup threadGroup, final boolean recurse, final Predicate<ThreadGroup> predicate) {
Validate.notNull(threadGroup, "group");
Validate.notNull(predicate, "predicate");
Objects.requireNonNull(threadGroup, "threadGroup");
Objects.requireNonNull(predicate, "predicate");
int count = threadGroup.activeGroupCount();
ThreadGroup[] threadGroups;
@ -367,8 +367,8 @@ public class ThreadUtils {
* @since 3.13.0
*/
public static Collection<Thread> findThreads(final ThreadGroup threadGroup, final boolean recurse, final Predicate<Thread> predicate) {
Validate.notNull(threadGroup, "The group must not be null");
Validate.notNull(predicate, "The predicate must not be null");
Objects.requireNonNull(threadGroup, "The group must not be null");
Objects.requireNonNull(predicate, "The predicate must not be null");
int count = threadGroup.activeCount();
Thread[] threads;
do {
@ -445,8 +445,8 @@ public class ThreadUtils {
* thread groups from this thread's thread group up to the system thread group
*/
public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
Validate.notNull(threadName, "threadName");
Validate.notNull(threadGroupName, "threadGroupName");
Objects.requireNonNull(threadName, "threadName");
Objects.requireNonNull(threadGroupName, "threadGroupName");
return Collections.unmodifiableCollection(findThreadGroups(predicateThreadGroup(threadGroupName)).stream()
.flatMap(group -> findThreads(group, false, predicateThread(threadName)).stream()).collect(Collectors.toList()));
}

View File

@ -23,7 +23,6 @@ import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
/**
* Assists in implementing {@link Diffable#diff(Object)} methods.
@ -102,8 +101,8 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
public DiffBuilder(final T lhs, final T rhs,
final ToStringStyle style, final boolean testTriviallyEqual) {
Validate.notNull(lhs, "lhs");
Validate.notNull(rhs, "rhs");
Objects.requireNonNull(lhs, "lhs");
Objects.requireNonNull(rhs, "rhs");
this.diffs = new ArrayList<>();
this.left = lhs;
@ -906,7 +905,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
*/
public DiffBuilder<T> append(final String fieldName, final DiffResult<T> diffResult) {
validateFieldNameNotNull(fieldName);
Validate.notNull(diffResult, "diffResult");
Objects.requireNonNull(diffResult, "diffResult");
if (objectsTriviallyEqual) {
return this;
}
@ -927,7 +926,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
}
private void validateFieldNameNotNull(final String fieldName) {
Validate.notNull(fieldName, "fieldName");
Objects.requireNonNull(fieldName, "fieldName");
}
}

View File

@ -19,8 +19,7 @@ package org.apache.commons.lang3.builder;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.Validate;
import java.util.Objects;
/**
* A {@link DiffResult} contains a collection of the differences between two
@ -69,9 +68,9 @@ public class DiffResult<T> implements Iterable<Diff<?>> {
*/
DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList,
final ToStringStyle style) {
Validate.notNull(lhs, "lhs");
Validate.notNull(rhs, "rhs");
Validate.notNull(diffList, "diffList");
Objects.requireNonNull(lhs, "lhs");
Objects.requireNonNull(rhs, "rhs");
Objects.requireNonNull(diffList, "diffList");
this.diffList = diffList;
this.lhs = lhs;

View File

@ -23,6 +23,7 @@ import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang3.ArraySorter;
@ -342,7 +343,7 @@ public class HashCodeBuilder implements Builder<Integer> {
*/
public static <T> int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final T object,
final boolean testTransients, final Class<? super T> reflectUpToClass, final String... excludeFields) {
Validate.notNull(object, "object");
Objects.requireNonNull(object, "object");
final HashCodeBuilder builder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber);
Class<?> clazz = object.getClass();
reflectionAppend(object, clazz, builder, testTransients, excludeFields);

View File

@ -28,7 +28,6 @@ import java.util.Objects;
import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.stream.Streams;
/**
@ -101,10 +100,6 @@ import org.apache.commons.lang3.stream.Streams;
*/
public class ReflectionToStringBuilder extends ToStringBuilder {
private static Object checkNotNull(final Object obj) {
return Validate.notNull(obj, "obj");
}
/**
* Converts the given Collection into an array of Strings. The returned array does not contain {@code null}
* entries. Note that {@link Arrays#sort(Object[])} will throw an {@link NullPointerException} if an array element
@ -495,7 +490,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object passed in is {@code null}
*/
public ReflectionToStringBuilder(final Object object) {
super(checkNotNull(object));
super(Objects.requireNonNull(object, "obj"));
}
/**
@ -513,7 +508,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object passed in is {@code null}
*/
public ReflectionToStringBuilder(final Object object, final ToStringStyle style) {
super(checkNotNull(object), style);
super(Objects.requireNonNull(object, "obj"), style);
}
/**
@ -537,7 +532,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object passed in is {@code null}
*/
public ReflectionToStringBuilder(final Object object, final ToStringStyle style, final StringBuffer buffer) {
super(checkNotNull(object), style, buffer);
super(Objects.requireNonNull(object, "obj"), style, buffer);
}
/**
@ -562,7 +557,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
public <T> ReflectionToStringBuilder(
final T object, final ToStringStyle style, final StringBuffer buffer,
final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics) {
super(checkNotNull(object), style, buffer);
super(Objects.requireNonNull(object, "obj"), style, buffer);
this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients);
this.setAppendStatics(outputStatics);
@ -593,7 +588,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
final T object, final ToStringStyle style, final StringBuffer buffer,
final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics,
final boolean excludeNullValues) {
super(checkNotNull(object), style, buffer);
super(Objects.requireNonNull(object, "obj"), style, buffer);
this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients);
this.setAppendStatics(outputStatics);

View File

@ -16,8 +16,9 @@
*/
package org.apache.commons.lang3.builder;
import java.util.Objects;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
/**
* Assists in implementing {@link Object#toString()} methods.
@ -131,7 +132,7 @@ public class ToStringBuilder implements Builder<String> {
* @throws IllegalArgumentException if the style is {@code null}
*/
public static void setDefaultStyle(final ToStringStyle style) {
defaultStyle = Validate.notNull(style, "style");
defaultStyle = Objects.requireNonNull(style, "style");
}
/**

View File

@ -17,13 +17,12 @@
package org.apache.commons.lang3.concurrent;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.Validate;
/**
* An implementation of the {@link ThreadFactory} interface that provides some
* configuration options for the threads it creates.
@ -280,7 +279,7 @@ public class BasicThreadFactory implements ThreadFactory {
* is <b>null</b>
*/
public Builder wrappedFactory(final ThreadFactory factory) {
Validate.notNull(factory, "factory");
Objects.requireNonNull(factory, "factory");
wrappedFactory = factory;
return this;
@ -295,7 +294,7 @@ public class BasicThreadFactory implements ThreadFactory {
* @throws NullPointerException if the naming pattern is <b>null</b>
*/
public Builder namingPattern(final String pattern) {
Validate.notNull(pattern, "pattern");
Objects.requireNonNull(pattern, "pattern");
namingPattern = pattern;
return this;
@ -337,7 +336,7 @@ public class BasicThreadFactory implements ThreadFactory {
*/
public Builder uncaughtExceptionHandler(
final Thread.UncaughtExceptionHandler handler) {
Validate.notNull(handler, "handler");
Objects.requireNonNull(handler, "handler");
exceptionHandler = handler;
return this;

View File

@ -16,11 +16,10 @@
*/
package org.apache.commons.lang3.concurrent;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import org.apache.commons.lang3.Validate;
/**
* A specialized {@link BackgroundInitializer} implementation that wraps a
* {@link Callable} object.
@ -118,6 +117,6 @@ public class CallableBackgroundInitializer<T> extends BackgroundInitializer<T> {
* @throws IllegalArgumentException if the {@link Callable} is <b>null</b>
*/
private void checkCallable(final Callable<T> callable) {
Validate.notNull(callable, "callable");
Objects.requireNonNull(callable, "callable");
}
}

View File

@ -20,11 +20,10 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import org.apache.commons.lang3.Validate;
/**
* A specialized {@link BackgroundInitializer} implementation that can deal with
* multiple background initialization tasks.
@ -132,8 +131,8 @@ public class MultiBackgroundInitializer
* @throws IllegalStateException if {@code start()} has already been called
*/
public void addInitializer(final String name, final BackgroundInitializer<?> backgroundInitializer) {
Validate.notNull(name, "name");
Validate.notNull(backgroundInitializer, "backgroundInitializer");
Objects.requireNonNull(name, "name");
Objects.requireNonNull(backgroundInitializer, "backgroundInitializer");
synchronized (this) {
if (isStarted()) {

View File

@ -28,6 +28,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.lang3.ArrayUtils;
@ -140,8 +141,8 @@ public class EventListenerSupport<L> implements Serializable {
*/
public EventListenerSupport(final Class<L> listenerInterface, final ClassLoader classLoader) {
this();
Validate.notNull(listenerInterface, "listenerInterface");
Validate.notNull(classLoader, "classLoader");
Objects.requireNonNull(listenerInterface, "listenerInterface");
Objects.requireNonNull(classLoader, "classLoader");
Validate.isTrue(listenerInterface.isInterface(), "Class %s is not an interface",
listenerInterface.getName());
initializeTransientFields(listenerInterface, classLoader);
@ -194,7 +195,7 @@ public class EventListenerSupport<L> implements Serializable {
* @since 3.5
*/
public void addListener(final L listener, final boolean allowDuplicate) {
Validate.notNull(listener, "listener");
Objects.requireNonNull(listener, "listener");
if (allowDuplicate || !listeners.contains(listener)) {
listeners.add(listener);
}
@ -218,7 +219,7 @@ public class EventListenerSupport<L> implements Serializable {
* {@code null}.
*/
public void removeListener(final L listener) {
Validate.notNull(listener, "listener");
Objects.requireNonNull(listener, "listener");
listeners.remove(listener);
}

View File

@ -17,8 +17,7 @@
package org.apache.commons.lang3.math;
import java.math.BigInteger;
import org.apache.commons.lang3.Validate;
import java.util.Objects;
/**
* {@link Fraction} is a {@link Number} implementation that
@ -311,7 +310,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
* @throws NumberFormatException if the number format is invalid
*/
public static Fraction getFraction(String str) {
Validate.notNull(str, "str");
Objects.requireNonNull(str, "str");
// parse double format
int pos = str.indexOf('.');
if (pos >= 0) {
@ -718,7 +717,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
* cannot be represented in an {@code int}.
*/
private Fraction addSub(final Fraction fraction, final boolean isAdd) {
Validate.notNull(fraction, "fraction");
Objects.requireNonNull(fraction, "fraction");
// zero is identity for addition.
if (numerator == 0) {
return isAdd ? fraction : fraction.negate();
@ -766,7 +765,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
* {@code Integer.MAX_VALUE}
*/
public Fraction multiplyBy(final Fraction fraction) {
Validate.notNull(fraction, "fraction");
Objects.requireNonNull(fraction, "fraction");
if (numerator == 0 || fraction.numerator == 0) {
return ZERO;
}
@ -789,7 +788,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
* {@code Integer.MAX_VALUE}
*/
public Fraction divideBy(final Fraction fraction) {
Validate.notNull(fraction, "fraction");
Objects.requireNonNull(fraction, "fraction");
if (fraction.numerator == 0) {
throw new ArithmeticException("The fraction to divide by must not be zero");
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.lang3.math;
import java.util.Objects;
import org.apache.commons.lang3.Validate;
/**
@ -37,7 +39,7 @@ public class IEEE754rUtils {
* @since 3.4 Changed signature from min(double[]) to min(double...)
*/
public static double min(final double... array) {
Validate.notNull(array, "array");
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
@ -59,7 +61,7 @@ public class IEEE754rUtils {
* @since 3.4 Changed signature from min(float[]) to min(float...)
*/
public static float min(final float... array) {
Validate.notNull(array, "array");
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns min
@ -147,7 +149,7 @@ public class IEEE754rUtils {
* @since 3.4 Changed signature from max(double[]) to max(double...)
*/
public static double max(final double... array) {
Validate.notNull(array, "array");
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max
@ -169,7 +171,7 @@ public class IEEE754rUtils {
* @since 3.4 Changed signature from max(float[]) to max(float...)
*/
public static float max(final float... array) {
Validate.notNull(array, "array");
Objects.requireNonNull(array, "array");
Validate.isTrue(array.length != 0, "Array cannot be empty.");
// Finds and returns max

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
@ -1338,7 +1339,7 @@ public class NumberUtils {
* @throws NullPointerException if {@code array} is {@code null}
*/
private static void validateArray(final Object array) {
Validate.notNull(array, "array");
Objects.requireNonNull(array, "array");
Validate.isTrue(Array.getLength(array) != 0, "Array cannot be empty.");
}

View File

@ -18,10 +18,10 @@ package org.apache.commons.lang3.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate;
/**
* Utility reflection methods focused on constructors, modeled after
@ -194,7 +194,7 @@ public class ConstructorUtils {
*/
public static <T> Constructor<T> getAccessibleConstructor(final Class<T> cls,
final Class<?>... parameterTypes) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
try {
return getAccessibleConstructor(cls.getConstructor(parameterTypes));
} catch (final NoSuchMethodException e) {
@ -214,7 +214,7 @@ public class ConstructorUtils {
* @throws NullPointerException if {@code ctor} is {@code null}
*/
public static <T> Constructor<T> getAccessibleConstructor(final Constructor<T> ctor) {
Validate.notNull(ctor, "ctor");
Objects.requireNonNull(ctor, "ctor");
return MemberUtils.isAccessible(ctor)
&& isAccessible(ctor.getDeclaringClass()) ? ctor : null;
}
@ -239,7 +239,7 @@ public class ConstructorUtils {
*/
public static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T> cls,
final Class<?>... parameterTypes) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
// see if we can find the constructor directly
// most of the time this works and it's much faster
try {

View File

@ -22,6 +22,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
@ -84,7 +85,7 @@ public class FieldUtils {
* in the inheritance hierarchy
*/
public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
Validate.isTrue(StringUtils.isNotBlank(fieldName), "The field name must not be blank/empty");
// FIXME is this workaround still needed? lang requires Java 6
// Sun Java 1.3 has a bugged implementation of getField hence we write the
@ -166,7 +167,7 @@ public class FieldUtils {
* if the class is {@code null}, or the field name is blank or empty
*/
public static Field getDeclaredField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
Validate.isTrue(StringUtils.isNotBlank(fieldName), "The field name must not be blank/empty");
try {
// only consider the specified class by using getDeclaredField()
@ -209,7 +210,7 @@ public class FieldUtils {
* @since 3.2
*/
public static List<Field> getAllFieldsList(final Class<?> cls) {
Validate.notNull(cls, "cls");
Objects.requireNonNull(cls, "cls");
final List<Field> allFields = new ArrayList<>();
Class<?> currentClass = cls;
while (currentClass != null) {
@ -247,7 +248,7 @@ public class FieldUtils {
* @since 3.4
*/
public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
Validate.notNull(annotationCls, "annotationCls");
Objects.requireNonNull(annotationCls, "annotationCls");
return getAllFieldsList(cls).stream().filter(field -> field.getAnnotation(annotationCls) != null).collect(Collectors.toList());
}
@ -281,7 +282,7 @@ public class FieldUtils {
* if the field is not made accessible
*/
public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException {
Validate.notNull(field, "field");
Objects.requireNonNull(field, "field");
Validate.isTrue(MemberUtils.isStatic(field), "The field '%s' is not static", field.getName());
return readField(field, (Object) null, forceAccess);
}
@ -407,7 +408,7 @@ public class FieldUtils {
* if the field is not made accessible
*/
public static Object readField(final Field field, final Object target, final boolean forceAccess) throws IllegalAccessException {
Validate.notNull(field, "field");
Objects.requireNonNull(field, "field");
if (forceAccess && !field.isAccessible()) {
field.setAccessible(true);
} else {
@ -451,7 +452,7 @@ public class FieldUtils {
* if the named field is not made accessible
*/
public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
Validate.notNull(target, "target");
Objects.requireNonNull(target, "target");
final Class<?> cls = target.getClass();
final Field field = getField(cls, fieldName, forceAccess);
Validate.isTrue(field != null, "Cannot locate field %s on %s", fieldName, cls);
@ -494,7 +495,7 @@ public class FieldUtils {
* if the field is not made accessible
*/
public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
Validate.notNull(target, "target");
Objects.requireNonNull(target, "target");
final Class<?> cls = target.getClass();
final Field field = getDeclaredField(cls, fieldName, forceAccess);
Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls, fieldName);
@ -535,7 +536,7 @@ public class FieldUtils {
* if the field is not made accessible or is {@code final}
*/
public static void writeStaticField(final Field field, final Object value, final boolean forceAccess) throws IllegalAccessException {
Validate.notNull(field, "field");
Objects.requireNonNull(field, "field");
Validate.isTrue(MemberUtils.isStatic(field), "The field %s.%s is not static", field.getDeclaringClass().getName(),
field.getName());
writeField(field, (Object) null, value, forceAccess);
@ -669,7 +670,7 @@ public class FieldUtils {
*/
public static void writeField(final Field field, final Object target, final Object value, final boolean forceAccess)
throws IllegalAccessException {
Validate.notNull(field, "field");
Objects.requireNonNull(field, "field");
if (forceAccess && !field.isAccessible()) {
field.setAccessible(true);
} else {
@ -709,7 +710,7 @@ public class FieldUtils {
*/
@Deprecated
public static void removeFinalModifier(final Field field, final boolean forceAccess) {
Validate.notNull(field, "field");
Objects.requireNonNull(field, "field");
try {
if (Modifier.isFinal(field.getModifiers())) {
@ -778,7 +779,7 @@ public class FieldUtils {
*/
public static void writeField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
Validate.notNull(target, "target");
Objects.requireNonNull(target, "target");
final Class<?> cls = target.getClass();
final Field field = getField(cls, fieldName, forceAccess);
Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName);
@ -826,7 +827,7 @@ public class FieldUtils {
*/
public static void writeDeclaredField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException {
Validate.notNull(target, "target");
Objects.requireNonNull(target, "target");
final Class<?> cls = target.getClass();
final Field field = getDeclaredField(cls, fieldName, forceAccess);
Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName);

View File

@ -418,8 +418,8 @@ public class TypeUtils {
*/
public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> cls,
final ParameterizedType superParameterizedType) {
Validate.notNull(cls, "cls");
Validate.notNull(superParameterizedType, "superParameterizedType");
Objects.requireNonNull(cls, "cls");
Objects.requireNonNull(superParameterizedType, "superParameterizedType");
final Class<?> superClass = getRawType(superParameterizedType);
@ -583,7 +583,7 @@ public class TypeUtils {
* @since 3.2
*/
public static GenericArrayType genericArrayType(final Type componentType) {
return new GenericArrayTypeImpl(Validate.notNull(componentType, "componentType"));
return new GenericArrayTypeImpl(Objects.requireNonNull(componentType, "componentType"));
}
/**
@ -672,7 +672,7 @@ public class TypeUtils {
* @return a non-empty array containing the bounds of the type variable.
*/
public static Type[] getImplicitBounds(final TypeVariable<?> typeVariable) {
Validate.notNull(typeVariable, "typeVariable");
Objects.requireNonNull(typeVariable, "typeVariable");
final Type[] bounds = typeVariable.getBounds();
return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
@ -688,7 +688,7 @@ public class TypeUtils {
* type.
*/
public static Type[] getImplicitLowerBounds(final WildcardType wildcardType) {
Validate.notNull(wildcardType, "wildcardType");
Objects.requireNonNull(wildcardType, "wildcardType");
final Type[] bounds = wildcardType.getLowerBounds();
return bounds.length == 0 ? new Type[] { null } : bounds;
@ -705,7 +705,7 @@ public class TypeUtils {
* type.
*/
public static Type[] getImplicitUpperBounds(final WildcardType wildcardType) {
Validate.notNull(wildcardType, "wildcardType");
Objects.requireNonNull(wildcardType, "wildcardType");
final Type[] bounds = wildcardType.getUpperBounds();
return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
@ -1511,7 +1511,7 @@ public class TypeUtils {
* redundant types.
*/
public static Type[] normalizeUpperBounds(final Type[] bounds) {
Validate.notNull(bounds, "bounds");
Objects.requireNonNull(bounds, "bounds");
// don't bother if there's only one (or none) type
if (bounds.length < 2) {
return bounds;
@ -1547,8 +1547,8 @@ public class TypeUtils {
*/
public static final ParameterizedType parameterize(final Class<?> rawClass,
final Map<TypeVariable<?>, Type> typeVariableMap) {
Validate.notNull(rawClass, "rawClass");
Validate.notNull(typeVariableMap, "typeVariableMap");
Objects.requireNonNull(rawClass, "rawClass");
Objects.requireNonNull(typeVariableMap, "typeVariableMap");
return parameterizeWithOwner(null, rawClass,
extractTypeArgumentsFrom(typeVariableMap, rawClass.getTypeParameters()));
}
@ -1611,8 +1611,8 @@ public class TypeUtils {
*/
public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> rawClass,
final Map<TypeVariable<?>, Type> typeVariableMap) {
Validate.notNull(rawClass, "rawClass");
Validate.notNull(typeVariableMap, "typeVariableMap");
Objects.requireNonNull(rawClass, "rawClass");
Objects.requireNonNull(typeVariableMap, "typeVariableMap");
return parameterizeWithOwner(owner, rawClass,
extractTypeArgumentsFrom(typeVariableMap, rawClass.getTypeParameters()));
}
@ -1629,7 +1629,7 @@ public class TypeUtils {
*/
public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> rawClass,
final Type... typeArguments) {
Validate.notNull(rawClass, "rawClass");
Objects.requireNonNull(rawClass, "rawClass");
final Type useOwner;
if (rawClass.getEnclosingClass() == null) {
Validate.isTrue(owner == null, "no owner allowed for top-level %s", rawClass);
@ -1678,7 +1678,7 @@ public class TypeUtils {
* @since 3.2
*/
public static String toLongString(final TypeVariable<?> typeVariable) {
Validate.notNull(typeVariable, "typeVariable");
Objects.requireNonNull(typeVariable, "typeVariable");
final StringBuilder buf = new StringBuilder();
final GenericDeclaration d = typeVariable.getGenericDeclaration();
if (d instanceof Class<?>) {
@ -1744,7 +1744,7 @@ public class TypeUtils {
* variables.
*/
public static boolean typesSatisfyVariables(final Map<TypeVariable<?>, Type> typeVariableMap) {
Validate.notNull(typeVariableMap, "typeVariableMap");
Objects.requireNonNull(typeVariableMap, "typeVariableMap");
// all types must be assignable to all the bounds of their mapped
// type variable.
for (final Map.Entry<TypeVariable<?>, Type> entry : typeVariableMap.entrySet()) {

View File

@ -21,12 +21,12 @@ import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.Validate;
/**
* FormatCache is a cache and factory for {@link Format}s.
@ -70,7 +70,7 @@ abstract class FormatCache<F extends Format> {
* @throws IllegalArgumentException if pattern is invalid
*/
public F getInstance(final String pattern, final TimeZone timeZone, final Locale locale) {
Validate.notNull(pattern, "pattern");
Objects.requireNonNull(pattern, "pattern");
final TimeZone actualTimeZone = TimeZones.toTimeZone(timeZone);
final Locale actualLocale = LocaleUtils.toLocale(locale);
final ArrayKey key = new ArrayKey(pattern, actualTimeZone, actualLocale);