类 ReflectUtils


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

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static java.lang.Object getField​(java.lang.reflect.Field field, java.lang.Object target)
      Get the field represented by the supplied field object on the specified target object.
      static java.lang.Object getFieldValue​(java.lang.Object obj, java.lang.String fieldName)
      get filed value of obj.
      static java.lang.Object getFieldValue​(java.lang.Object obj, java.lang.String fieldName, java.lang.Object defaultValue)
      get filed value of obj.
      static void handleInvocationTargetException​(java.lang.reflect.InvocationTargetException ex)
      Handle the given invocation target exception.
      static void handleReflectionException​(java.lang.Exception ex)
      Handle the given reflection exception.
      static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
      Invoke the specified Method against the supplied target object with the supplied arguments.
      static void rethrowRuntimeException​(java.lang.Throwable ex)
      Rethrow the given exception, which is presumably the target exception of an InvocationTargetException.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • ReflectUtils

        public ReflectUtils()
    • 方法详细资料

      • 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 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​(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 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
        抛出:
        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 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