Add get(:Object: fieldName:String) and get(:Object, :Field) methods to ObjectUtils.

Add overloaded doOperationSafely(:ExceptionThrowingOperation, :Function<Throwable, T>) method.
This commit is contained in:
John Blum
2018-09-19 13:14:27 -07:00
parent bcc763bc16
commit f1c9e3ea3c
2 changed files with 46 additions and 4 deletions

View File

@@ -19,12 +19,16 @@ package org.springframework.geode.core.util;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
@@ -54,7 +58,7 @@ public abstract class ObjectUtils extends org.springframework.util.ObjectUtils {
*/
@Nullable
public static <T> T doOperationSafely(ExceptionThrowingOperation<T> operation) {
return doOperationSafely(operation, null);
return doOperationSafely(operation, (T) null);
}
/**
@@ -76,6 +80,15 @@ public abstract class ObjectUtils extends org.springframework.util.ObjectUtils {
@Nullable
public static <T> T doOperationSafely(ExceptionThrowingOperation<T> operation, T defaultValue) {
Function<Throwable, T> exceptionHandlingFunction = cause ->
returnValueThrowOnNull(defaultValue, newIllegalStateException(cause, "Failed to execute operation"));
return doOperationSafely(operation, exceptionHandlingFunction);
}
public static <T> T doOperationSafely(ExceptionThrowingOperation<T> operation,
Function<Throwable, T> exceptionHandlingFunction) {
try {
return operation.doExceptionThrowingOperation();
}
@@ -85,11 +98,30 @@ public abstract class ObjectUtils extends org.springframework.util.ObjectUtils {
logger.debug(String.format("Failed to execute operation [%s]", operation), cause);
}
return returnValueThrowOnNull(defaultValue,
newIllegalStateException(cause, "Failed to execute operation"));
return exceptionHandlingFunction.apply(cause);
}
}
public static <T> T get(Object obj, String fieldName) {
Assert.notNull(obj, "Object is requried");
Assert.hasText(fieldName, String.format("Field name [%s] is require", fieldName));
return Optional.ofNullable(ReflectionUtils.findField(obj.getClass(), fieldName))
.map(ObjectUtils::makeAccessible)
.map(field -> ObjectUtils.<T>get(obj, field))
.orElseThrow(() -> newIllegalArgumentException("No field with name [%s] exists on object of type [%s]",
fieldName, ObjectUtils.nullSafeClassName(obj)));
}
public static <T> T get(Object obj, Field field) {
Assert.notNull(obj, "Object is requried");
Assert.notNull(field, "Field is required");
return doOperationSafely(() -> (T) field.get(obj), (T) null);
}
/**
* Invokes a {@link Method} on an {@link Object} with the given {@link String name}.
*
@@ -113,6 +145,16 @@ public abstract class ObjectUtils extends org.springframework.util.ObjectUtils {
methodName, org.springframework.util.ObjectUtils.nullSafeClassName(obj)));
}
private static Constructor makeAccessible(Constructor<?> constructor) {
ReflectionUtils.makeAccessible(constructor);
return constructor;
}
private static Field makeAccessible(Field field) {
ReflectionUtils.makeAccessible(field);
return field;
}
private static Method makeAccessible(Method method) {
ReflectionUtils.makeAccessible(method);
return method;

View File

@@ -46,7 +46,7 @@ public class ObjectUtilsUnitTests {
public void doOperationSafelyThrowsIllegalStateException() {
try {
ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); }, null);
ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); }, (Object) null);
}
catch (IllegalStateException expected) {