Class FieldHelper

java.lang.Object
de.qytera.qtaf.core.reflection.FieldHelper

public class FieldHelper extends Object
Class that provides helper methods for reflective field access and manipulation
  • Constructor Details

    • FieldHelper

      public FieldHelper()
  • Method Details

    • getDeclaredFieldsRecursively

      public static ArrayList<Field> getDeclaredFieldsRecursively(Class<?> clazz)
      Get declared fields of a class and its superclasses
      Parameters:
      clazz - class
      Returns:
      fields
    • getFieldValue

      public static Object getFieldValue(Object object, String fieldName)
      Get field value of given object. This method can be used to access private fields of objects. If the field is static this method will return null.
      Parameters:
      object - Object
      fieldName - Field name
      Returns:
      Field value on success, null otherwise
    • getFieldByName

      public static Field getFieldByName(String name, Class<?> clazz)
      Get a field reflection object of a given object by a class object
      Parameters:
      name - field name
      clazz - Object class
      Returns:
      Field reflection object on success, false otherwise
    • canAccess

      public static boolean canAccess(Field field, Object object) throws IllegalArgumentException
      Check if field can be accessed
      Parameters:
      field - Field reflection object
      object - Object that has the field
      Returns:
      true if accessible, false otherwise
      Throws:
      IllegalArgumentException - Thrown when trying to access static fields
    • getFieldValue

      public static Object getFieldValue(Object object, Field field)
      Get value of an object field using the Reflection API
      Parameters:
      field - Field reflection object
      object - Object that has the field
      Returns:
      Field value on success, null otherwise
    • setFieldValue

      public static boolean setFieldValue(Object object, Field field, Object value)
      Set value of an object field using the Reflection API
      Parameters:
      object - Object that has the field
      field - Field reflection object
      value - New value of the field
      Returns:
      Field value on success, null otherwise
    • setFieldValue

      public static boolean setFieldValue(Object object, String fieldName, Object value)
      Set value of an object field using the Reflection API
      Parameters:
      object - Object that has the field
      fieldName - Field's name
      value - New value of the field
      Returns:
      Field value on success, null otherwise