类 Reflections

java.lang.Object
com.jeeplus.devtools.util.Reflections

public class Reflections extends Object
反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
版本:
2021-01-15
作者:
calvin
  • 构造器详细资料

    • Reflections

      public Reflections()
  • 方法详细资料

    • invokeGetter

      public static Object invokeGetter(Object obj, String propertyName)
      调用Getter方法. 支持多级,如:对象名.对象名.方法
    • invokeSetter

      public static void invokeSetter(Object obj, String propertyName, Object value)
      调用Setter方法, 仅匹配方法名。 支持多级,如:对象名.对象名.方法
    • getFieldValue

      public static Object getFieldValue(Object obj, String fieldName)
      直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
    • setFieldValue

      public static void setFieldValue(Object obj, String fieldName, Object value)
      直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
    • invokeMethod

      public static Object invokeMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args)
      直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用. 同时匹配方法名+参数类型,
    • invokeMethodByName

      public static Object invokeMethodByName(Object obj, String methodName, Object[] args)
      直接调用对象方法, 无视private/protected修饰符, 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用. 只匹配函数名,如果有多个同名函数调用第一个。
    • getAccessibleField

      public static Field getAccessibleField(Object obj, String fieldName)
      循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.

      如向上转型到Object仍无法找到, 返回null.

    • getAccessibleMethod

      public static Method getAccessibleMethod(Object obj, String methodName, Class<?>... parameterTypes)
      循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 匹配函数名+参数类型。

      用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)

    • getAccessibleMethodByName

      public static Method getAccessibleMethodByName(Object obj, String methodName)
      循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 只匹配函数名。

      用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)

    • makeAccessible

      public static void makeAccessible(Method method)
      改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
    • makeAccessible

      public static void makeAccessible(Field field)
      改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
    • getClassGenricType

      public static <T> Class<T> getClassGenricType(Class clazz)
      通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class. eg. public UserDao extends HibernateDao
      参数:
      clazz - The class to introspect
      返回:
      the first generic declaration, or Object.class if cannot be determined
    • getClassGenricType

      public static Class getClassGenricType(Class clazz, int index)
      通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.

      如public UserDao extends HibernateDao<User,Long>

      参数:
      clazz - clazz The class to introspect
      index - the Index of the generic ddeclaration,start from 0.
      返回:
      the index generic declaration, or Object.class if cannot be determined
    • getUserClass

      public static Class<?> getUserClass(Object instance)
    • convertReflectionExceptionToUnchecked

      public static RuntimeException convertReflectionExceptionToUnchecked(Exception e)
      将反射时的checked exception转换为unchecked exception.