Class ReflectUtil

java.lang.Object
org.qiunet.utils.reflect.ReflectUtil

public final class ReflectUtil extends Object
反射工具类
Author:
qiunet 2020-09-27 11:00
  • Field Details

    • USER_DECLARED_METHODS

      public static final Predicate<Method> USER_DECLARED_METHODS
      Pre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared on java.lang.Object.
      Since:
      3.0.5
  • Method Details

    • findFieldList

      public static List<Field> findFieldList(Class<?> clazz, Predicate<Field> filter)
      Attempt to find a field on the supplied Class with the supplied name. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      Returns:
      the corresponding Field object, or null if not found
    • findField

      public static Field findField(Class<?> clazz, String name)
      Attempt to find a field on the supplied Class with the supplied name. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      name - the name of the field
      Returns:
      the corresponding Field object, or null if not found
    • findField

      public static Field findField(Class<?> clazz, String name, Class<?> type)
      Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      name - the name of the field (may be null if type is specified)
      type - the type of the field (may be null if name is specified)
      Returns:
      the corresponding Field object, or null if not found
    • getField

      public static Object getField(Object t, String name)
      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

      Parameters:
      t - the target object from which to get the field
      name - the field to get
      Returns:
      the field's current value
    • makeAccessible

      public static void makeAccessible(Method method)
      Make the given method accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
      Parameters:
      method - the method to make accessible
      See Also:
    • makeAccessible

      public static void makeAccessible(Field field)
      Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
      Parameters:
      field - the field to make accessible
      See Also:
    • setField

      public static void setField(Object declaringObj, String name, Object value)
      Set the field represented by the supplied field object on the specified target object to the specified value. In accordance with Field.set(Object, Object) semantics, the new value is automatically unwrapped if the underlying field has a primitive type.

      Thrown exceptions are handled via a call to

      Parameters:
      declaringObj - the target object on which to set the field
      name - the field to set
      value - the value to set; may be null
    • setField

      public static void setField(Object declaringObj, Field field, Object value)
      给字段设置值
      Parameters:
      declaringObj - the target object on which to set the field
      field - the field to set
      value - the value to set; may be null
    • getField

      public static Object getField(Field field, Object obj)
      Get the field represented by the supplied field object on the specified target object . In accordance with Field.get(Object)

      Thrown exceptions are handled via a call to

      Parameters:
      field - the field to get
      obj - the field declare object instance. if field is static. it is null.
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Object... params)
      生成新的对象
      Type Parameters:
      T -
      Parameters:
      clazz -
      params -
      Returns:
    • getMatchConstructor

      public static <T> Constructor<T> getMatchConstructor(Class<T> clazz, Object... params)
      获得匹配的构造
      Type Parameters:
      T -
      Parameters:
      clazz -
      params -
      Returns:
    • newInstance

      public static <T> T newInstance(Constructor<T> constructor, Object... params)
      生成新的对象.
      Type Parameters:
      T -
      Parameters:
      constructor -
      params -
      Returns:
    • doWithFields

      public static void doWithFields(Class<?> clazz, Consumer<Field> consumer)
      Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
      Parameters:
      clazz - the target class to analyze
      consumer - the callback to invoke for each field
      Throws:
      IllegalStateException - if introspection fails
    • doWithFields

      public static void doWithFields(Class<?> clazz, Consumer<Field> consumer, Predicate<Field> predicate)
      Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
      Parameters:
      clazz - the target class to analyze
      consumer - the callback to invoke for each field
      predicate - the filter that determines the fields to apply the callback to
      Throws:
      IllegalStateException - if introspection fails
    • getDeclaredFields

      public static Field[] getDeclaredFields(Class<?> clazz)
      This variant retrieves Class.getDeclaredFields() from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.
      Parameters:
      clazz - the class to introspect
      Returns:
      the cached array of fields
      Throws:
      IllegalStateException - if introspection fails
      See Also:
    • doWithMethods

      public static void doWithMethods(Class<?> clazz, Consumer<Method> mc)
      Perform the given callback operation on all matching methods of the given class and superclasses.

      The same named method occurring on subclass and superclass will appear twice, unless excluded by a Predicate.

      Parameters:
      clazz - the class to introspect
      mc - the callback to invoke for each method
      Throws:
      IllegalStateException - if introspection fails
      See Also:
    • doWithMethods

      public static void doWithMethods(Class<?> clazz, Consumer<Method> mc, Predicate<Method> mf)
      Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

      The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified Predicate.

      Parameters:
      clazz - the class to introspect
      mc - the callback to invoke for each method
      mf - the filter that determines the methods to apply the callback to
      Throws:
      IllegalStateException - if introspection fails
    • getAllDeclaredMethods

      public static Method[] getAllDeclaredMethods(Class<?> leafClass)
      Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.
      Parameters:
      leafClass - the class to introspect
      Throws:
      IllegalStateException - if introspection fails
    • getDeclaredMethods

      public static Method[] getDeclaredMethods(Class<?> clazz)
      Variant of Class.getDeclaredMethods() that uses a local cache in order to avoid the JVM's SecurityManager check and new Method instances. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.
      Parameters:
      clazz - the class to introspect
      Returns:
      the cached array of methods
      Throws:
      IllegalStateException - if introspection fails
      Since:
      5.2
      See Also:
    • findGenericParameterizedType

      public static Class<?> findGenericParameterizedType(Class<?> oriClazz, Predicate<Class<?>> filter)
      找出Class 上对应的泛型参数类型
      Parameters:
      oriClazz - 原始class
      filter - 类型过滤 仅给出泛型class
      Returns:
    • findGenericParameterizedType

      public static Class<?> findGenericParameterizedType(Class<?> oriClazz, BiPredicate<Class<?>,Class<?>> filter)
      找出Class 上对应的泛型参数类型
      Parameters:
      oriClazz - 原始class
      filter - 类型过滤 给出所在class 以及 泛型class
      Returns:
    • getListGenericParameterizedType

      public static Class<?> getListGenericParameterizedType(Field field)
      得到list field class的泛型
      Returns:
    • modifyAnnotationValue

      public static void modifyAnnotationValue(Annotation annotation, String key, Object value)
      修改注解的值
      Parameters:
      annotation - 注解对象
      key - 注解字段名.
      value - 设置的值