类 ReflectUtils
- 版本:
- $Id: ReflectUtils.java, v 0.1 2020年08月20日 12:57 PM liuzunfei Exp $
- 作者:
- liuzunfei
-
方法概要
修饰符和类型方法说明static ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static ObjectgetFieldValue(Object obj, String fieldName) get filed value of obj.static ObjectgetFieldValue(Object obj, String fieldName, Object defaultValue) get filed value of obj.static voidHandle the given invocation target exception.static voidHandle the given reflection exception.static ObjectinvokeMethod(Method method, Object target, Object... args) Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static voidRethrow the givenexception, which is presumably the target exception of anInvocationTargetException.
-
方法详细资料
-
getFieldValue
get filed value of obj.- 参数:
obj- obj.fieldName- file name to get value.- 返回:
- field value.
-
getFieldValue
get filed value of obj.- 参数:
obj- obj.fieldName- file name to get value.- 返回:
- field value.
-
getField
Get the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.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 (ornullfor a static field)- 返回:
- the field's current value
-
handleReflectionException
Handle the given reflection exception.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 handle
-
handleInvocationTargetException
Handle the given invocation target exception. Should only be called if no checked exception is expected to be thrown by the target method.Throws the underlying RuntimeException or Error in case of such a root cause. Throws an UndeclaredThrowableException otherwise.
- 参数:
ex- the invocation target exception to handle
-
rethrowRuntimeException
Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.Rethrows the underlying exception cast to a
RuntimeExceptionorErrorif appropriate; otherwise, throws anUndeclaredThrowableException.- 参数:
ex- the exception to rethrow- 抛出:
RuntimeException- the rethrown exception
-
invokeMethod
Invoke the specifiedMethodagainst the supplied target object with the supplied arguments. The target object can benullwhen invoking a staticMethod.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 benull)- 返回:
- the invocation result, if any
-