Class FieldName

java.lang.Object
org.fest.reflect.field.FieldName

public final class FieldName extends Object
Understands the name of a field to access using Java Reflection.

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 field "powers"
   List<String> powers = field("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).get();

   // Sets the value of the field "powers"
   List<String> powers = new ArrayList<String>();
   powers.add("heal");
   field("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).set(powers);
 

Author:
Alex Ruiz, Ivan Hristov
  • Method Details

    • beginFieldAccess

      public static FieldName beginFieldAccess(String name)
      Creates a new FieldName: the starting point of the fluent interface for accessing fields using Java Reflection.
      Parameters:
      name - the name of the field to access using Java Reflection.
      Returns:
      the created FieldName.
      Throws:
      NullPointerException - if the given name is null.
      IllegalArgumentException - if the given name is empty.
    • ofType

      public <T> FieldType<T> ofType(Class<T> type)
      Sets the type of the field to access.
      Type Parameters:
      T - the generic type of the field type.
      Parameters:
      type - the type of the field to access.
      Returns:
      a recipient for the field type.
      Throws:
      NullPointerException - if the given type is null.
    • ofType

      public <T> FieldTypeRef<T> ofType(TypeRef<T> type)
      Sets the type reference of the field to access. This method reduces casting when the type of the field to access uses generics.

      For example:

         List<String> powers = field("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).get();
       

      Type Parameters:
      T - the generic type of the field type.
      Parameters:
      type - the type of the field to access.
      Returns:
      a recipient for the field type.
      Throws:
      NullPointerException - if the given type reference is null.
      Since:
      1.1