Class ObjectUtils
java.lang.Object
org.springframework.util.ObjectUtils
org.springframework.geode.core.util.ObjectUtils
public abstract class ObjectUtils
extends org.springframework.util.ObjectUtils
The
ObjectUtils
class is an abstract utility class with operations for objects
.- Since:
- 1.0.0
- See Also:
-
Object
Constructor
Field
Method
PdxInstance
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
static <T> T
doOperationSafely
(ObjectUtils.ExceptionThrowingOperation<T> operation) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by rethrowing anIllegalStateException
wrapping the original checkedException
.static <T> T
doOperationSafely
(ObjectUtils.ExceptionThrowingOperation<T> operation, Function<Throwable, T> exceptionHandlingFunction) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by invoking the providedException
handlingFunction
.static <T> T
doOperationSafely
(ObjectUtils.ExceptionThrowingOperation<T> operation, Supplier<T> valueSupplier) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by returning adefault value
supplied by the givenSupplier
, or throws anIllegalStateException
if thesupplied value
is null.static <T> T
doOperationSafely
(ObjectUtils.ExceptionThrowingOperation<T> operation, T defaultValue) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by returning the givendefault value
or throwing anIllegalStateException
if thedefault value
is null.findMethod
(Class<?> type, String methodName, Object... args) static <T> T
static <T> T
static <T> T
initialize
(T target, Supplier<T> supplier) static <T> T
static Constructor<?>
makeAccessible
(Constructor<?> constructor) Makes theConstructor
accessible.static Field
makeAccessible
(Field field) Makes theField
accessible.static Method
makeAccessible
(Method method) Makes theMethod
accessible.static <T> T
resolveInvocationTarget
(T target, Method method) Resolves theinvocation target
for the givenMethod
.static <T> T
returnValueThrowOnNull
(T value) static <T> T
returnValueThrowOnNull
(T value, RuntimeException exception) Methods inherited from class org.springframework.util.ObjectUtils
addObjectToArray, addObjectToArray, caseInsensitiveValueOf, containsConstant, containsConstant, containsElement, getDisplayString, getIdentityHexString, identityToString, isArray, isCheckedException, isCompatibleWithThrowsClause, isEmpty, isEmpty, nullSafeClassName, nullSafeConciseToString, nullSafeEquals, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeHashCode, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, nullSafeToString, toObjectArray, unwrapOptional
-
Constructor Details
-
ObjectUtils
public ObjectUtils()
-
-
Method Details
-
asType
Tries to cast the given sourceObject
into an instance of the givenClass
type. This method is cable of handling Apache GeodePdxInstance
types.- Type Parameters:
T
- desiredtype
of the sourceObject
.- Parameters:
source
-Object
to evaluate.type
- desired targetClass
type; must not be null.- Returns:
- the source
Object
cast to an instance of the givenClass
type. - Throws:
IllegalArgumentException
- if the sourceObject
is not an instance of the givenClass
type or theClass
type is null.- See Also:
-
doOperationSafely
@Nullable public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by rethrowing anIllegalStateException
wrapping the original checkedException
.- Type Parameters:
T
-type
ofvalue
returned from the execution of the operation.- Parameters:
operation
-ObjectUtils.ExceptionThrowingOperation
to execute; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation
. - Throws:
IllegalStateException
- wrapping any checkedException
thrown by the operation.- See Also:
-
doOperationSafely
@Nullable public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull T defaultValue) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by returning the givendefault value
or throwing anIllegalStateException
if thedefault value
is null.- Type Parameters:
T
-type
ofvalue
returned from the execution of the operation as well as thetype
of thedefault value
.- Parameters:
operation
-ObjectUtils.ExceptionThrowingOperation
to execute; must not be null.defaultValue
-value
to return if the execution of the operation results in a checkedException
.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation
, returning thedefault value
if the execution of the operation throws a checkedException
- Throws:
IllegalStateException
- wrapping any checkedException
thrown by the operation when thedefault value
is null.- See Also:
-
doOperationSafely
public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull Supplier<T> valueSupplier) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by returning adefault value
supplied by the givenSupplier
, or throws anIllegalStateException
if thesupplied value
is null.- Type Parameters:
T
-type
ofvalue
returned from the execution of the operation as well as thetype
of thedefault value
.- Parameters:
operation
-ObjectUtils.ExceptionThrowingOperation
to execute; must not be null.valueSupplier
-Supplier
of thevalue
to return if the execution of the operation results in a checkedException
; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation
, returning thesupplied value
if the execution of the operation throws a checkedException
- Throws:
IllegalStateException
- wrapping any checkedException
thrown by the operation when thesupplied value
is null.- See Also:
-
doOperationSafely
public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull Function<Throwable, T> exceptionHandlingFunction) Safely executes the givenObjectUtils.ExceptionThrowingOperation
handling any checkedException
thrown during the normal execution of the operation by invoking the providedException
handlingFunction
.- Type Parameters:
T
-type
ofvalue
returned from the execution of the operation.- Parameters:
operation
-ObjectUtils.ExceptionThrowingOperation
to execute; must not be null.exceptionHandlingFunction
-Function
used to handle any checkedException
thrown byObjectUtils.ExceptionThrowingOperation
; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation
. - See Also:
-
findMethod
-
get
- Type Parameters:
T
-type
of theField's
value.- Parameters:
obj
-Object
containing thenamed
Field
.fieldName
-String
containing the name of theField
.- Returns:
- the
value
of thenamed
Field
on the givenObject
. - Throws:
IllegalArgumentException
- ifObject
is null, thenamed
Field
is not specified or the givenObject
contains noField
with the givenname
.- See Also:
-
get
-
initialize
An initialization operator used to evalutate a giventarget
and conditionallysupply
a new value if thetarget
is null. Theinitialize
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);
-
invoke
- Type Parameters:
T
-type
of theMethod
return value.- Parameters:
obj
-Object
on which to invoke theMethod
.methodName
-String
containing the name of theMethod
to invoke onObject
.- Returns:
- the return value of the invoked
Method
onObject
. - Throws:
IllegalArgumentException
- if noMethod
withname
could be found onObject
.- See Also:
-
makeAccessible
Makes theConstructor
accessible.- Parameters:
constructor
-Constructor
to make accessible; must not be null.- Returns:
- the given
Constructor
. - See Also:
-
makeAccessible
Makes theField
accessible. -
makeAccessible
Makes theMethod
accessible. -
returnValueThrowOnNull
public static <T> T returnValueThrowOnNull(T value) -
returnValueThrowOnNull
- Type Parameters:
T
-type
of thevalue
.- Parameters:
value
-Object
to return.exception
-RuntimeException
to throw ifvalue
is null.- Returns:
- the
value
or throw the givenRuntimeException
ifvalue
is null.
-
resolveInvocationTarget
Resolves theinvocation target
for the givenMethod
. If theMethod
isModifier.STATIC
then null is returned, otherwisetarget
will be returned.
-