类 ReflectUtils

java.lang.Object
com.alibaba.nacos.common.utils.ReflectUtils

public class ReflectUtils extends Object
reflect utils.
版本:
$Id: ReflectUtils.java, v 0.1 2020年08月20日 12:57 PM liuzunfei Exp $
作者:
liuzunfei
  • 方法详细资料

    • getFieldValue

      public static Object getFieldValue(Object obj, String fieldName)
      get filed value of obj.
      参数:
      obj - obj.
      fieldName - file name to get value.
      返回:
      field value.
    • getFieldValue

      public static Object getFieldValue(Object obj, String fieldName, Object defaultValue)
      get filed value of obj.
      参数:
      obj - obj.
      fieldName - file name to get value.
      返回:
      field value.
    • getField

      public static Object getField(Field field, Object target)
      Get the field represented by the supplied 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 get
      target - the target object from which to get the field (or null for a static field)
      返回:
      the field's current value
    • handleReflectionException

      public static void handleReflectionException(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(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(Throwable ex)
      Rethrow the given 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 rethrow
      抛出:
      RuntimeException - the rethrown exception
    • invokeMethod

      public static Object invokeMethod(Method method, Object target, Object... args)
      Invoke the specified 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 invoke
      target - the target object to invoke the method on
      args - the invocation arguments (may be null)
      返回:
      the invocation result, if any