类 ReflectUtils
- java.lang.Object
-
- com.alibaba.nacos.common.utils.ReflectUtils
-
public class ReflectUtils extends java.lang.Objectreflect utils.- 版本:
- $Id: ReflectUtils.java, v 0.1 2020年08月20日 12:57 PM liuzunfei Exp $
- 作者:
- liuzunfei
-
-
构造器概要
构造器 构造器 说明 ReflectUtils()
-
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static java.lang.ObjectgetField(java.lang.reflect.Field field, java.lang.Object target)Get the field represented by the suppliedfield objecton the specifiedtarget object.static java.lang.ObjectgetFieldValue(java.lang.Object obj, java.lang.String fieldName)get filed value of obj.static java.lang.ObjectgetFieldValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object defaultValue)get filed value of obj.static voidhandleInvocationTargetException(java.lang.reflect.InvocationTargetException ex)Handle the given invocation target exception.static voidhandleReflectionException(java.lang.Exception ex)Handle the given reflection exception.static java.lang.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static voidrethrowRuntimeException(java.lang.Throwable ex)Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException.
-
-
-
方法详细资料
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String fieldName)get filed value of obj.- 参数:
obj- obj.fieldName- file name to get value.- 返回:
- field value.
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object defaultValue)get filed value of obj.- 参数:
obj- obj.fieldName- file name to get value.- 返回:
- field value.
-
getField
public static java.lang.Object getField(java.lang.reflect.Field field, java.lang.Object target)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
public static void handleReflectionException(java.lang.Exception ex)
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
public static void handleInvocationTargetException(java.lang.reflect.InvocationTargetException ex)
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
public static void rethrowRuntimeException(java.lang.Throwable ex)
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- 抛出:
java.lang.RuntimeException- the rethrown exception
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)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
-
-