Class Invoker<T>

java.lang.Object
org.fest.reflect.field.Invoker<T>
Type Parameters:
T - the declared type for the field to access.

public final class Invoker<T> extends Object
Understands the use of reflection to access a field from an object.

The following is an example of proper usage of this class:

   // Retrieves the value of the field "name"
   String name = field("name").ofType(String.class).in(person).get();

   // Sets the value of the field "name" to "Yoda"
   field("name").ofType(String.class).in(person).set("Yoda");

   // Retrieves the value of the static field "count"
   int count = staticField("count").ofType(int.class).in(Person.class).get();

   // Sets the value of the static field "count" to 3
   field("count").ofType(int.class).in(Person.class).set(3);
 

  • Field Details

    • target

      private final Object target
    • field

      private final Field field
    • accessible

      private final boolean accessible
  • Constructor Details

    • Invoker

      private Invoker(Object target, Field field)
  • Method Details

    • newInvoker

      static <T> Invoker<T> newInvoker(String fieldName, TypeRef<T> expectedType, Object target)
    • newInvoker

      static <T> Invoker<T> newInvoker(String fieldName, Class<T> expectedType, Object target)
    • createInvoker

      private static <T> Invoker<T> createInvoker(String fieldName, Class<?> expectedType, Object target)
    • typeOf

      private static Class<?> typeOf(Object target)
    • lookupInClassHierarchy

      private static Field lookupInClassHierarchy(String fieldName, Class<?> declaringType)
    • verifyCorrectType

      private static void verifyCorrectType(Field field, Class<?> expectedType)
    • field

      private static Field field(String fieldName, Class<?> declaringType)
    • incorrectFieldType

      private static ReflectionError incorrectFieldType(Field field, Class<?> actual, Class<?> expected)
    • set

      public void set(T value)
      Sets a value in the field managed by this class.
      Parameters:
      value - the value to set.
      Throws:
      ReflectionError - if the given value cannot be set.
    • get

      public T get()
      Returns the value of the field managed by this class.
      Returns:
      the value of the field managed by this class.
      Throws:
      ReflectionError - if the value of the field cannot be retrieved.
    • info

      public Field info()
      Returns the "real" field managed by this class.
      Returns:
      the "real" field managed by this class.