public abstract class ObjectUtils
extends org.springframework.util.ObjectUtils
ObjectUtils
class is an abstract utility class with operations for objects
.Object
,
Constructor
,
Field
,
Method
,
PdxInstance
Modifier and Type | Class and Description |
---|---|
static interface |
ObjectUtils.ExceptionThrowingOperation<T> |
Constructor and Description |
---|
ObjectUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
asType(java.lang.Object source,
java.lang.Class<T> type)
Tries to cast the given source
Object into an instance of the given Class type. |
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 |
initialize(T target,
java.util.function.Supplier<T> supplier)
An initialization operator used to evalutate a given
target and conditionally
supply a new value if the target is null. |
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)
Makes the
Constructor accessible. |
static java.lang.reflect.Field |
makeAccessible(java.lang.reflect.Field field)
Makes the
Field accessible. |
static java.lang.reflect.Method |
makeAccessible(java.lang.reflect.Method method)
Makes the
Method accessible. |
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 asType(@Nullable java.lang.Object source, @NonNull java.lang.Class<T> type)
Object
into an instance of the given Class
type.
This method is cable of handling GemFire/Geode PdxInstance
types.T
- desired type
of the source Object
.source
- Object
to evaluate.type
- desired target Class
type; must not be null.Object
cast to an instance of the given Class
type.java.lang.IllegalArgumentException
- if the source Object
is not an instance of
the given Class
type or the Class
type is null.PdxInstance
,
Class
,
Object
@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 initialize(@Nullable T target, @NonNull java.util.function.Supplier<T> supplier)
target
and conditionally
supply
a new value if the target
is null.
The initialize
operator simplifies a common initialization safety pattern that appears in code as:
target = target != null ? target : new Target();
While the expression uses Java's ternary operator, users could very well use a if-then-else statement instead.
Either way, since Java is call-by-value then the above statement and expression can be replaced with:
target = initialize(target, Target::new);
T
- type
of the target
.target
- Object
to evaluate and initialize; must not be null.supplier
- Supplier
used to initialize the target
on return.target
if not null, otherwise invoke the Supplier
to supply a new instance of T
.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(@NonNull java.lang.reflect.Constructor<?> constructor)
Constructor
accessible.constructor
- Constructor
to make accessible; must not be null.Constructor
.Constructor
public static java.lang.reflect.Field makeAccessible(java.lang.reflect.Field field)
Field
accessible.field
- Field
to make accessible; must not be null.Field
.Field
public static java.lang.reflect.Method makeAccessible(java.lang.reflect.Method method)
Method
accessible.method
- Method
to make accessible; must not be null.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