Class FieldUtils


  • public class FieldUtils
    extends java.lang.Object
    Shameless copy from Apache commons lang and then modified to keep only the interesting stuff for AssertJ. Utilities for working with fields by reflection. Adapted and refactored from the dormant [reflect] Commons sandbox component.

    The ability is provided to break the scoping restrictions coded by the programmer. This can allow fields to be changed that shouldn't be. This facility should be used with care.

    • Constructor Summary

      Constructors 
      Constructor Description
      FieldUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.lang.reflect.Field getField​(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
      Gets an accessible Field by name breaking scope if requested.
      (package private) static java.lang.Object readField​(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
      Reads the named field.
      private static java.lang.Object readField​(java.lang.reflect.Field field, java.lang.Object target)
      Reads an accessible Field.
      private static java.lang.Object readField​(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess)
      Reads a Field.
      • Methods inherited from class java.lang.Object

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

      • FieldUtils

        public FieldUtils()
    • Method Detail

      • getField

        static java.lang.reflect.Field getField​(java.lang.Class<?> cls,
                                                java.lang.String fieldName,
                                                boolean forceAccess)
                                         throws java.lang.IllegalAccessException
        Gets an accessible Field by name breaking scope if requested. Superclasses/interfaces will be considered.
        Parameters:
        cls - the class to reflect, must not be null
        fieldName - the field name to obtain
        forceAccess - whether to break scope restrictions using the setAccessible method. False will only match public fields.
        Returns:
        the Field object
        Throws:
        java.lang.IllegalArgumentException - if the class or field name is null
        java.lang.IllegalAccessException - if field exists but is not public
      • readField

        private static java.lang.Object readField​(java.lang.reflect.Field field,
                                                  java.lang.Object target)
                                           throws java.lang.IllegalAccessException
        Reads an accessible Field.
        Parameters:
        field - the field to use
        target - the object to call on, may be null for static fields
        Returns:
        the field value
        Throws:
        java.lang.IllegalArgumentException - if the field is null
        java.lang.IllegalAccessException - if the field is not accessible
      • readField

        private static java.lang.Object readField​(java.lang.reflect.Field field,
                                                  java.lang.Object target,
                                                  boolean forceAccess)
                                           throws java.lang.IllegalAccessException
        Reads a Field.
        Parameters:
        field - the field to use
        target - the object to call on, may be null for static fields
        forceAccess - whether to break scope restrictions using the setAccessible method.
        Returns:
        the field value
        Throws:
        java.lang.IllegalArgumentException - if the field is null
        java.lang.IllegalAccessException - if the field is not made accessible
      • readField

        static java.lang.Object readField​(java.lang.Object target,
                                          java.lang.String fieldName,
                                          boolean forceAccess)
                                   throws java.lang.IllegalAccessException
        Reads the named field. Superclasses will be considered.
        Parameters:
        target - the object to reflect, must not be null
        fieldName - the field name to obtain
        forceAccess - whether to break scope restrictions using the setAccessible method. False will only match public fields.
        Returns:
        the field value
        Throws:
        java.lang.IllegalArgumentException - if the class or field name is null
        java.lang.IllegalAccessException - if the named field is not made accessible