Class FieldName


  • public final class FieldName
    extends java.lang.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 Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static FieldName beginFieldAccess​(java.lang.String name)
      Creates a new FieldName: the starting point of the fluent interface for accessing fields using Java Reflection.
      <T> FieldType<T> ofType​(java.lang.Class<T> type)
      Sets the type of the field to access.
      <T> FieldTypeRef<T> ofType​(TypeRef<T> type)
      Sets the type reference of the field to access.
      • Methods inherited from class java.lang.Object

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

      • beginFieldAccess

        public static FieldName beginFieldAccess​(java.lang.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:
        java.lang.NullPointerException - if the given name is null.
        java.lang.IllegalArgumentException - if the given name is empty.
      • ofType

        public <T> FieldType<T> ofType​(java.lang.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:
        java.lang.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:
        java.lang.NullPointerException - if the given type reference is null.
        Since:
        1.1