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:
-
ObjectConstructorFieldMethodPdxInstance
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tstatic <T> TdoOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by rethrowing anIllegalStateExceptionwrapping the original checkedException.static <T> TdoOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation, Function<Throwable, T> exceptionHandlingFunction) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by invoking the providedExceptionhandlingFunction.static <T> TdoOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation, Supplier<T> valueSupplier) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by returning adefault valuesupplied by the givenSupplier, or throws anIllegalStateExceptionif thesupplied valueis null.static <T> TdoOperationSafely(ObjectUtils.ExceptionThrowingOperation<T> operation, T defaultValue) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by returning the givendefault valueor throwing anIllegalStateExceptionif thedefault valueis null.findMethod(Class<?> type, String methodName, Object... args) static <T> Tstatic <T> Tstatic <T> Tinitialize(T target, Supplier<T> supplier) static <T> Tstatic Constructor<?>makeAccessible(Constructor<?> constructor) Makes theConstructoraccessible.static FieldmakeAccessible(Field field) Makes theFieldaccessible.static MethodmakeAccessible(Method method) Makes theMethodaccessible.static <T> TresolveInvocationTarget(T target, Method method) Resolves theinvocation targetfor the givenMethod.static <T> TreturnValueThrowOnNull(T value) static <T> TreturnValueThrowOnNull(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 sourceObjectinto an instance of the givenClasstype. This method is cable of handling Apache GeodePdxInstancetypes.- Type Parameters:
T- desiredtypeof the sourceObject.- Parameters:
source-Objectto evaluate.type- desired targetClasstype; must not be null.- Returns:
- the source
Objectcast to an instance of the givenClasstype. - Throws:
IllegalArgumentException- if the sourceObjectis not an instance of the givenClasstype or theClasstype is null.- See Also:
-
doOperationSafely
@Nullable public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by rethrowing anIllegalStateExceptionwrapping the original checkedException.- Type Parameters:
T-typeofvaluereturned from the execution of the operation.- Parameters:
operation-ObjectUtils.ExceptionThrowingOperationto execute; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation. - Throws:
IllegalStateException- wrapping any checkedExceptionthrown by the operation.- See Also:
-
doOperationSafely
@Nullable public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull T defaultValue) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by returning the givendefault valueor throwing anIllegalStateExceptionif thedefault valueis null.- Type Parameters:
T-typeofvaluereturned from the execution of the operation as well as thetypeof thedefault value.- Parameters:
operation-ObjectUtils.ExceptionThrowingOperationto execute; must not be null.defaultValue-valueto return if the execution of the operation results in a checkedException.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation, returning thedefault valueif the execution of the operation throws a checkedException - Throws:
IllegalStateException- wrapping any checkedExceptionthrown by the operation when thedefault valueis null.- See Also:
-
doOperationSafely
public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull Supplier<T> valueSupplier) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by returning adefault valuesupplied by the givenSupplier, or throws anIllegalStateExceptionif thesupplied valueis null.- Type Parameters:
T-typeofvaluereturned from the execution of the operation as well as thetypeof thedefault value.- Parameters:
operation-ObjectUtils.ExceptionThrowingOperationto execute; must not be null.valueSupplier-Supplierof thevalueto return if the execution of the operation results in a checkedException; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation, returning thesupplied valueif the execution of the operation throws a checkedException - Throws:
IllegalStateException- wrapping any checkedExceptionthrown by the operation when thesupplied valueis null.- See Also:
-
doOperationSafely
public static <T> T doOperationSafely(@NonNull ObjectUtils.ExceptionThrowingOperation<T> operation, @NonNull Function<Throwable, T> exceptionHandlingFunction) Safely executes the givenObjectUtils.ExceptionThrowingOperationhandling any checkedExceptionthrown during the normal execution of the operation by invoking the providedExceptionhandlingFunction.- Type Parameters:
T-typeofvaluereturned from the execution of the operation.- Parameters:
operation-ObjectUtils.ExceptionThrowingOperationto execute; must not be null.exceptionHandlingFunction-Functionused to handle any checkedExceptionthrown byObjectUtils.ExceptionThrowingOperation; must not be null.- Returns:
- the result of the
ObjectUtils.ExceptionThrowingOperation. - See Also:
-
findMethod
-
get
- Type Parameters:
T-typeof theField'svalue.- Parameters:
obj-Objectcontaining thenamedField.fieldName-Stringcontaining the name of theField.- Returns:
- the
valueof thenamedFieldon the givenObject. - Throws:
IllegalArgumentException- ifObjectis null, thenamedFieldis not specified or the givenObjectcontains noFieldwith the givenname.- See Also:
-
get
-
initialize
An initialization operator used to evalutate a giventargetand conditionallysupplya new value if thetargetis null. Theinitializeoperator 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-typeof theMethodreturn value.- Parameters:
obj-Objecton which to invoke theMethod.methodName-Stringcontaining the name of theMethodto invoke onObject.- Returns:
- the return value of the invoked
MethodonObject. - Throws:
IllegalArgumentException- if noMethodwithnamecould be found onObject.- See Also:
-
makeAccessible
Makes theConstructoraccessible.- Parameters:
constructor-Constructorto make accessible; must not be null.- Returns:
- the given
Constructor. - See Also:
-
makeAccessible
Makes theFieldaccessible. -
makeAccessible
Makes theMethodaccessible. -
returnValueThrowOnNull
public static <T> T returnValueThrowOnNull(T value) -
returnValueThrowOnNull
- Type Parameters:
T-typeof thevalue.- Parameters:
value-Objectto return.exception-RuntimeExceptionto throw ifvalueis null.- Returns:
- the
valueor throw the givenRuntimeExceptionifvalueis null.
-
resolveInvocationTarget
Resolves theinvocation targetfor the givenMethod. If theMethodisModifier.STATICthen null is returned, otherwisetargetwill be returned.
-