Class FieldUtils

  • All Implemented Interfaces:
    Utils

    public abstract class FieldUtils
    extends java.lang.Object
    implements Utils
    The Java Reflection Field Utility class
    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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<? super java.lang.reflect.Field>... fieldFilters)  
      static java.util.Set<java.lang.reflect.Field> findAllFields​(java.lang.Class<?> declaredClass, java.util.function.Predicate<? super 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<? super 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 <V> V setStaticFieldValue​(java.lang.Class<?> klass, java.lang.String fieldName, V fieldValue)
      Set the value to static Field
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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
        Parameters:
        object - the object to find
        fieldName - the name of field
        Returns:
        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
        Parameters:
        klass - class to find
        fieldName - the name of field
        Returns:
        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
        Parameters:
        klass - class to find
        fieldName - the name of field
        fieldType - the type of field
        Returns:
        null if not found
      • findField

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

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

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

        public static <V> V setStaticFieldValue​(java.lang.Class<?> klass,
                                                java.lang.String fieldName,
                                                V fieldValue)
        Set the value to static Field
        Parameters:
        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<? super java.lang.reflect.Field>... fieldFilters)
      • findAllDeclaredFields

        public static java.util.Set<java.lang.reflect.Field> findAllDeclaredFields​(java.lang.Class<?> declaredClass,
                                                                                   java.util.function.Predicate<? super 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
        Parameters:
        declaredClass - the declared class
        fieldName - the name of Field
        Returns:
        if can't be found, return null
        Throws:
        java.lang.RuntimeException - thrown if you can't find the Field
      • 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
        Parameters:
        instance - the instance whose field should be modified
        fieldName - the name of Field
        Returns:
        the value of the specified Field
        Throws:
        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
        Type Parameters:
        V - field type
        Parameters:
        instance - the instance whose field should be modified
        fieldName - field name
        defaultValue - default value
        Returns:
        Field Value
        Throws:
        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
        Type Parameters:
        V - field type
        Parameters:
        instance - the instance whose field should be modified
        fieldName - field name
        fieldType - field type
        Returns:
        Field Value
        Throws:
        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
        Parameters:
        instance - the instance whose field should be modified
        field - Field
        Returns:
        the value of the specified Field
        Throws:
        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
        Parameters:
        instance - the instance whose field should be modified
        fieldName - the name of Field
        value - the value of field to be set
        Returns:
        the previous value of the specified Field
        Throws:
        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
        Parameters:
        instance - the instance whose field should be modified
        field - Field
        value - the value of field to be set
        Returns:
        the previous value of the specified Field
        Throws:
        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
        Parameters:
        instance - Object or class
        fieldName - field name
        expectedType - expected type
        Throws:
        java.lang.IllegalArgumentException - if type is not matched