public class ReflectUtils extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static Object |
getField(Field field,
Object target)
Get the field represented by the supplied
field object on the specified target
object. |
static Object |
getFieldValue(Object obj,
String fieldName)
get filed value of obj.
|
static Object |
getFieldValue(Object obj,
String fieldName,
Object defaultValue)
get filed value of obj.
|
static void |
handleInvocationTargetException(InvocationTargetException ex)
Handle the given invocation target exception.
|
static void |
handleReflectionException(Exception ex)
Handle the given reflection exception.
|
static Object |
invokeMethod(Method method,
Object target,
Object... args)
Invoke the specified
Method against the supplied target object with the supplied arguments. |
static void |
rethrowRuntimeException(Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
public static Object getFieldValue(Object obj, String fieldName)
obj - obj.fieldName - file name to get value.public static Object getFieldValue(Object obj, String fieldName, Object defaultValue)
obj - obj.fieldName - file name to get value.public static Object getField(Field field, Object target)
field object on the specified target
object. In accordance with Field.get(Object) semantics, the returned value is automatically wrapped if
the underlying field has a primitive type.
Thrown exceptions are handled via a call to handleReflectionException(Exception).
field - the field to gettarget - the target object from which to get the field (or null for a static field)public static void handleReflectionException(Exception ex)
Should only be called if no checked exception is expected to be thrown by a target method, or if an error occurs while accessing a method or field.
Throws the underlying RuntimeException or Error in case of an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message or UndeclaredThrowableException otherwise.
ex - the reflection exception to handlepublic static void handleInvocationTargetException(InvocationTargetException ex)
Throws the underlying RuntimeException or Error in case of such a root cause. Throws an UndeclaredThrowableException otherwise.
ex - the invocation target exception to handlepublic static void rethrowRuntimeException(Throwable ex)
exception, which is presumably the
target exception of an InvocationTargetException.
Should only be called if no checked exception is expected to be thrown by the target method.
Rethrows the underlying exception cast to a RuntimeException or
Error if appropriate; otherwise, throws an UndeclaredThrowableException.
ex - the exception to rethrowRuntimeException - the rethrown exceptionpublic static Object invokeMethod(Method method, Object target, Object... args)
Method against the supplied target object with the supplied arguments. The target
object can be null when invoking a static Method.
Thrown exceptions are handled via a call to handleReflectionException(java.lang.Exception).
method - the method to invoketarget - the target object to invoke the method onargs - the invocation arguments (may be null)Copyright © 2018–2024 Alibaba Group. All rights reserved.