Package org.fest.reflect.field
Class FieldName
java.lang.Object
org.fest.reflect.field.FieldName
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
Modifier and TypeMethodDescriptionstatic FieldName
beginFieldAccess
(String name) Creates a new
: the starting point of the fluent interface for accessing fields using Java Reflection.FieldName
<T> FieldType<T>
Sets the type of the field to access.<T> FieldTypeRef<T>
Sets the type reference of the field to access.
-
Method Details
-
beginFieldAccess
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:
NullPointerException
- if the given name isnull
.IllegalArgumentException
- if the given name is empty.
-
ofType
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 isnull
.
-
ofType
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:
NullPointerException
- if the given type reference isnull
.- Since:
- 1.1
-