Package org.fest.reflect.field
Class FieldName
- java.lang.Object
-
- org.fest.reflect.field.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
(newTypeRef
<List<String>>() {}).in
(jedi).get
(); // Sets the value of the field "powers" List<String> powers = new ArrayList<String>(); powers.add("heal");field
("powers").ofType
(newTypeRef
<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
: the starting point of the fluent interface for accessing fields using Java Reflection.FieldName
<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.
-
-
-
Method Detail
-
beginFieldAccess
public static FieldName beginFieldAccess(java.lang.String name)
Creates a new
: the starting point of the fluent interface for accessing fields using Java Reflection.FieldName
- Parameters:
name
- the name of the field to access using Java Reflection.- Returns:
- the created
FieldName
. - Throws:
java.lang.NullPointerException
- if the given name isnull
.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 isnull
.
-
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
(newTypeRef
<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 isnull
.- Since:
- 1.1
-
-