Class FieldUtils

java.lang.Object
org.apache.felix.scr.impl.inject.field.FieldUtils

public class FieldUtils extends Object
Utility methods for handling field injection.
  • Constructor Details

    • FieldUtils

      public FieldUtils()
  • Method Details

    • searchField

      public static FieldUtils.FieldSearchResult searchField(Class<?> componentClass, String fieldName, ComponentLogger logger)
      Searches the field named fieldName in the given targetClass. If the target class has no acceptable field the class hierarchy is traversed until a field is found or the root of the class hierarchy is reached without finding a field.

      If an unexpected error occurs while searching, null is returned. In all other cases a FieldSearchResult is returned. If no field is found, FieldSearchResult#field is set to null. If the field is found, but not usable (e.g. due to visibility restrictions), FieldSearchResult#usable is set to false.

      Parameters:
      fieldName - The name of the field
      logger - A logger to log errors / problems
      targetClass - The class of the component
      Returns:
      A field search result or null if an unexpected error occurred.
    • getField

      private static FieldUtils.FieldSearchResult getField(Class<?> componentClass, Class<?> targetClass, String fieldName, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger) throws InvocationTargetException
      Finds the field named fieldName field in the given targetClass. If the target class has no acceptable field the class hierarchy is traversed until a field is found or the root of the class hierarchy is reached without finding a field.
      Parameters:
      componentClass - The class of the component (for logging)
      targetClass - The class in which to look for the method
      fieldName - The name of the field
      acceptPrivate - true if private fields should be considered.
      acceptPackage - true if package private fields should be considered.
      logger - For error logging
      Returns:
      If the field is found a FieldSearchResult is returned. If the field is not found, null is returned.
      Throws:
      InvocationTargetException - If an unexpected Throwable is caught trying to find the requested field.
    • accept

      private static FieldUtils.FieldSearchResult accept(Class<?> componentClass, Field field, boolean acceptPrivate, boolean acceptPackage, ComponentLogger logger)
      This method checks whether the found field is acceptable (= usable) for the component instance. It returns a FieldSearchResult with the usable flag set to true if the field is not static and
      • Is public or protected
      • Is private and acceptPrivate is true
      • Is package private and acceptPackage is true

      If the field is usable, this method makes the field accessible.

      If the field is not usable, a FieldSearchResult with the usable flag set to false is returned and an error is logged with the provided logger.

      Parameters:
      componentClass - The class of the component,.
      field - The field to check
      acceptPrivate - Whether a private field is acceptable
      acceptPackage - Whether a package private field is acceptable
      logger - The logger for error logging
      Returns:
      A field search result, this is never null
    • toString

      public static String toString(Class<?> componentClass, Field field)
      Return a string representation of the field
      Parameters:
      componentClass - The component class
      field - The field
      Returns:
      A string representation of the field
    • setAccessible

      private static void setAccessible(Field field)
      Make the field accessible
      Parameters:
      field - The field
    • setField

      public static void setField(Field f, Object component, Object value, ComponentLogger logger)
      Set the field, type etc.
      Parameters:
      f - The field
      component - The component instance
      value - The value to set
      logger - The logger