类 FieldUtils


  • public abstract class FieldUtils
    extends BaseUtils
    The Java Reflection Field Utility class
    从以下版本开始:
    1.0.0
    作者:
    Mercy
    • 构造器概要

      构造器 
      构造器 说明
      FieldUtils()  
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static void assertFieldMatchType​(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<?> expectedType)
      Assert Field type match
      static java.util.Set<java.lang.reflect.Field> findAllDeclaredFields​(java.lang.Class<?> declaredClass, java.util.function.Predicate<java.lang.reflect.Field>... fieldFilters)  
      static java.util.Set<java.lang.reflect.Field> findAllFields​(java.lang.Class<?> declaredClass, java.util.function.Predicate<java.lang.reflect.Field>... fieldFilters)  
      static java.lang.reflect.Field findField​(java.lang.Class<?> klass, java.lang.String fieldName)
      Find the declared Field by its' name
      static java.lang.reflect.Field findField​(java.lang.Class<?> klass, java.lang.String fieldName, java.lang.Class<?> fieldType)
      Find the declared Field by its' name
      static java.lang.reflect.Field findField​(java.lang.Class<?> klass, java.lang.String fieldName, java.util.function.Predicate<java.lang.reflect.Field>... predicates)
      Find the declared Field by its' name
      static java.lang.reflect.Field findField​(java.lang.Object object, java.lang.String fieldName)
      Find the specified objects' declared Field by its' name
      static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> declaredClass, java.lang.String fieldName)
      Like the Class.getDeclaredField(String) method without throwing any Exception
      static <V> V getFieldValue​(java.lang.Object instance, java.lang.reflect.Field field)
      Get the value of the specified Field
      static <V> V getFieldValue​(java.lang.Object instance, java.lang.String fieldName)
      Get the value of the specified Field
      static <V> V getFieldValue​(java.lang.Object instance, java.lang.String fieldName, java.lang.Class<V> fieldType)
      Get Field Value
      static <V> V getFieldValue​(java.lang.Object instance, java.lang.String fieldName, V defaultValue)
      Get Field Value
      static <T> T getStaticFieldValue​(java.lang.Class<?> klass, java.lang.String fieldName)
      Get the static Field Value
      static <T> T getStaticFieldValue​(java.lang.reflect.Field field)
      Get the static Field Value
      static <V> V setFieldValue​(java.lang.Object instance, java.lang.reflect.Field field, V value)
      Set the value for the specified Field
      static <V> V setFieldValue​(java.lang.Object instance, java.lang.String fieldName, V value)
      Set the value for the specified Field
      static void setStaticFieldValue​(java.lang.Class<?> klass, java.lang.String fieldName, java.lang.Object fieldValue)
      Set the value to static Field
      • 从类继承的方法 java.lang.Object

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

      • FieldUtils

        public FieldUtils()
    • 方法详细资料

      • findField

        public static java.lang.reflect.Field findField​(java.lang.Object object,
                                                        java.lang.String fieldName)
        Find the specified objects' declared Field by its' name
        参数:
        object - the object to find
        fieldName - the name of field
        返回:
        null if not found
      • findField

        public static java.lang.reflect.Field findField​(java.lang.Class<?> klass,
                                                        java.lang.String fieldName)
        Find the declared Field by its' name
        参数:
        klass - class to find
        fieldName - the name of field
        返回:
        null if not found
      • findField

        public static java.lang.reflect.Field findField​(java.lang.Class<?> klass,
                                                        java.lang.String fieldName,
                                                        java.lang.Class<?> fieldType)
        Find the declared Field by its' name
        参数:
        klass - class to find
        fieldName - the name of field
        fieldType - the type of field
        返回:
        null if not found
      • findField

        public static java.lang.reflect.Field findField​(java.lang.Class<?> klass,
                                                        java.lang.String fieldName,
                                                        java.util.function.Predicate<java.lang.reflect.Field>... predicates)
        Find the declared Field by its' name
        参数:
        klass - class to find
        fieldName - the name of field
        predicates - zero or more Predicate
        返回:
        null if not found
      • getStaticFieldValue

        public static <T> T getStaticFieldValue​(java.lang.Class<?> klass,
                                                java.lang.String fieldName)
        Get the static Field Value
        参数:
        klass - class to find
        fieldName - the name of field
        返回:
        null if field is null or get failed
      • getStaticFieldValue

        public static <T> T getStaticFieldValue​(java.lang.reflect.Field field)
        Get the static Field Value
        参数:
        field - Field
        返回:
        null if field is null or get failed
      • setStaticFieldValue

        public static void setStaticFieldValue​(java.lang.Class<?> klass,
                                               java.lang.String fieldName,
                                               java.lang.Object fieldValue)
        Set the value to static Field
        参数:
        klass - the class declared the field
        fieldName - the name of Field
        fieldValue - the value of Field
      • findAllFields

        public static java.util.Set<java.lang.reflect.Field> findAllFields​(java.lang.Class<?> declaredClass,
                                                                           java.util.function.Predicate<java.lang.reflect.Field>... fieldFilters)
      • findAllDeclaredFields

        public static java.util.Set<java.lang.reflect.Field> findAllDeclaredFields​(java.lang.Class<?> declaredClass,
                                                                                   java.util.function.Predicate<java.lang.reflect.Field>... fieldFilters)
      • getDeclaredField

        public static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> declaredClass,
                                                               java.lang.String fieldName)
        Like the Class.getDeclaredField(String) method without throwing any Exception
        参数:
        declaredClass - the declared class
        fieldName - the name of Field
        返回:
        if can't be found, return null
      • getFieldValue

        public static <V> V getFieldValue​(java.lang.Object instance,
                                          java.lang.String fieldName)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Get the value of the specified Field
        参数:
        instance - the instance whose field should be modified
        fieldName - the name of Field
        返回:
        the value of the specified Field
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • getFieldValue

        public static <V> V getFieldValue​(java.lang.Object instance,
                                          java.lang.String fieldName,
                                          V defaultValue)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Get Field Value
        类型参数:
        V - field type
        参数:
        instance - the instance whose field should be modified
        fieldName - field name
        defaultValue - default value
        返回:
        Field Value
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • getFieldValue

        public static <V> V getFieldValue​(java.lang.Object instance,
                                          java.lang.String fieldName,
                                          java.lang.Class<V> fieldType)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Get Field Value
        类型参数:
        V - field type
        参数:
        instance - the instance whose field should be modified
        fieldName - field name
        fieldType - field type
        返回:
        Field Value
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • getFieldValue

        public static <V> V getFieldValue​(java.lang.Object instance,
                                          java.lang.reflect.Field field)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Get the value of the specified Field
        参数:
        instance - the instance whose field should be modified
        field - Field
        返回:
        the value of the specified Field
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • setFieldValue

        public static <V> V setFieldValue​(java.lang.Object instance,
                                          java.lang.String fieldName,
                                          V value)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Set the value for the specified Field
        参数:
        instance - the instance whose field should be modified
        fieldName - the name of Field
        value - the value of field to be set
        返回:
        the previous value of the specified Field
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • setFieldValue

        public static <V> V setFieldValue​(java.lang.Object instance,
                                          java.lang.reflect.Field field,
                                          V value)
                                   throws java.lang.IllegalStateException,
                                          java.lang.IllegalArgumentException
        Set the value for the specified Field
        参数:
        instance - the instance whose field should be modified
        field - Field
        value - the value of field to be set
        返回:
        the previous value of the specified Field
        抛出:
        java.lang.IllegalStateException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        java.lang.IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
      • assertFieldMatchType

        public static void assertFieldMatchType​(java.lang.Object instance,
                                                java.lang.String fieldName,
                                                java.lang.Class<?> expectedType)
                                         throws java.lang.IllegalArgumentException
        Assert Field type match
        参数:
        instance - Object
        fieldName - field name
        expectedType - expected type
        抛出:
        java.lang.IllegalArgumentException - if type is not matched