public abstract class ObjectUtils
extends org.springframework.util.ObjectUtils
ObjectUtils
class is an abstract utility class with operations for objects
.Object
,
Method
Modifier and Type | Class and Description |
---|---|
static interface |
ObjectUtils.ExceptionThrowingOperation<T> |
Constructor and Description |
---|
ObjectUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation)
Executes the given
ObjectUtils.ExceptionThrowingOperation handling any checked Exception thrown during
the normal execution of the operation by rethrowing an IllegalStateException wrapping
the checked Exception . |
static <T> T |
doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation,
java.util.function.Function<java.lang.Throwable,T> exceptionHandlingFunction)
Executes the given
ObjectUtils.ExceptionThrowingOperation handling any checked Exception thrown
during the normal execution of the operation by invoking the provided Exception handling
Function . |
static <T> T |
doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation,
T defaultValue)
Executes the given
ObjectUtils.ExceptionThrowingOperation handling any checked Exception thrown
during the normal execution of the operation, returning the default value in its place
or throwing a RuntimeException if the default value is null. |
static java.util.Optional<java.lang.reflect.Method> |
findMethod(java.lang.Class<?> type,
java.lang.String methodName,
java.lang.Object... args)
Finds a
Method with the given name on type which can be invoked
with the given arguments . |
static <T> T |
get(java.lang.Object obj,
java.lang.reflect.Field field)
Gets the
value of the given Field on the given Object . |
static <T> T |
get(java.lang.Object obj,
java.lang.String fieldName)
Gets the
value of the given named Field on the given Object . |
static <T> T |
invoke(java.lang.Object obj,
java.lang.String methodName)
Invokes a
Method on an Object with the given name . |
static java.lang.reflect.Constructor |
makeAccessible(java.lang.reflect.Constructor<?> constructor) |
static java.lang.reflect.Field |
makeAccessible(java.lang.reflect.Field field) |
static java.lang.reflect.Method |
makeAccessible(java.lang.reflect.Method method) |
static <T> T |
resolveInvocationTarget(T target,
java.lang.reflect.Method method)
Resolves the
invocation target for the given Method . |
static <T> T |
returnValueThrowOnNull(T value)
Returns the given
value or throws an IllegalArgumentException
if value is null. |
static <T> T |
returnValueThrowOnNull(T value,
java.lang.RuntimeException exception)
Returns the given
value or throws the given RuntimeException
if value is null. |
addObjectToArray, caseInsensitiveValueOf, containsConstant, containsConstant, containsElement, getDisplayString, getIdentityHexString, hashCode, hashCode, hashCode, hashCode, identityToString, isArray, isCheckedException, isCompatibleWithThrowsClause, isEmpty, isEmpty, nullSafeClassName, nullSafeEquals, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, toObjectArray, unwrapOptional
@Nullable public static <T> T doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation)
ObjectUtils.ExceptionThrowingOperation
handling any checked Exception
thrown during
the normal execution of the operation by rethrowing an IllegalStateException
wrapping
the checked Exception
.T
- type
of value returned from the operation execution.operation
- ObjectUtils.ExceptionThrowingOperation
to execute.ObjectUtils.ExceptionThrowingOperation
or throw an IllegalStateException
wrapping the checked Exception
thrown by the operation.ObjectUtils.ExceptionThrowingOperation
,
doOperationSafely(ExceptionThrowingOperation, Object)
@Nullable public static <T> T doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation, T defaultValue)
ObjectUtils.ExceptionThrowingOperation
handling any checked Exception
thrown
during the normal execution of the operation, returning the default value
in its place
or throwing a RuntimeException
if the default value
is null.T
- type
of value returned from the operation execution as well as
the default value
.operation
- ObjectUtils.ExceptionThrowingOperation
to execute.defaultValue
- value
to return if the operation results in a checked Exception
.ObjectUtils.ExceptionThrowingOperation
, returning the default value
if the operation throws a checked Exception
or throws an IllegalStateException
wrapping
the checked Exception
if the default value
is null.java.lang.IllegalStateException
- if the ObjectUtils.ExceptionThrowingOperation
throws a checked Exception
and default value
is null.ObjectUtils.ExceptionThrowingOperation
,
doOperationSafely(ExceptionThrowingOperation, Function)
,
returnValueThrowOnNull(Object, RuntimeException)
public static <T> T doOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation, java.util.function.Function<java.lang.Throwable,T> exceptionHandlingFunction)
ObjectUtils.ExceptionThrowingOperation
handling any checked Exception
thrown
during the normal execution of the operation by invoking the provided Exception
handling
Function
.T
- type
of value returned from the operation execution.operation
- ObjectUtils.ExceptionThrowingOperation
to execute.exceptionHandlingFunction
- Function
used to handle any Exception
thrown by
the operation
.ObjectUtils.ExceptionThrowingOperation
.ObjectUtils.ExceptionThrowingOperation
,
Function
,
Throwable
public static java.util.Optional<java.lang.reflect.Method> findMethod(@NonNull java.lang.Class<?> type, @NonNull java.lang.String methodName, java.lang.Object... args)
Method
with the given name
on type
which can be invoked
with the given arguments
.type
- Class
type to evaluate for the Method
.methodName
- String
containing the name of the Method
to find.args
- array of arguments
used when invoking the method.Optional
Method
on type
potentially matching
the arguments
of the invocation.Class
,
Method
,
Optional
public static <T> T get(java.lang.Object obj, java.lang.String fieldName)
value
of the given named
Field
on the given Object
.T
- type
of the Field's
value.obj
- Object
containing the named
Field
.fieldName
- String
containing the name of the Field
.value
of the named
Field
on the given Object
.java.lang.IllegalArgumentException
- if Object
is null, the named
Field
is not specified or the given Object
contains no Field
with the given name
.get(Object, Field)
,
Object
public static <T> T get(java.lang.Object obj, java.lang.reflect.Field field)
value
of the given Field
on the given Object
.T
- type
of the Field's
value.obj
- Object
containing the Field
.field
- Field
of the given Object
.value
of the Field
on the given Object
.java.lang.IllegalArgumentException
- if Object
or Field
is null.Field
,
Object
public static <T> T invoke(java.lang.Object obj, java.lang.String methodName)
Method
on an Object
with the given name
.T
- type
of the Method
return value.obj
- Object
on which to invoke the Method
.methodName
- String
containing the name of the Method
to invoke on Object
.Method
on Object
.java.lang.IllegalArgumentException
- if no Method
with name
could be found on Object
.Method
,
Object
public static java.lang.reflect.Constructor makeAccessible(java.lang.reflect.Constructor<?> constructor)
public static java.lang.reflect.Field makeAccessible(java.lang.reflect.Field field)
public static java.lang.reflect.Method makeAccessible(java.lang.reflect.Method method)
public static <T> T returnValueThrowOnNull(T value)
value
or throws an IllegalArgumentException
if value
is null.T
- type
of the value
.value
- Object
to return.value
or throw an IllegalArgumentException
if value
is null.returnValueThrowOnNull(Object, RuntimeException)
public static <T> T returnValueThrowOnNull(T value, java.lang.RuntimeException exception)
value
or throws the given RuntimeException
if value
is null.T
- type
of the value
.value
- Object
to return.exception
- RuntimeException
to throw if value
is null.value
or throw the given RuntimeException
if value
is null.public static <T> T resolveInvocationTarget(T target, java.lang.reflect.Method method)
invocation target
for the given Method
.
If the Method
is Modifier.STATIC
then null is returned,
otherwise target
will be returned.T
- type
of the target
.target
- Object
on which the Method
will be invoked.method
- Method
to invoke on the Object
.invocation method
.Object
,
Method