Package org.fest.reflect.field
Class Invoker<T>
java.lang.Object
org.fest.reflect.field.Invoker<T>
- Type Parameters:
T
- the declared type for the field to access.
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 3field
("count").ofType
(int.class).in
(Person.class).set
(3);
- Author:
- Alex Ruiz, Ivan Hristov
-
Method Summary
Modifier and TypeMethodDescriptionget()
Returns the value of the field managed by this class.info()
Returns the "real" field managed by this class.postDecorateWith
(T decorator) Post-decorates a targeted object's methods.preDecorateWith
(T decorator) Pre-decorates a targeted object's methods.void
Sets a value in the field managed by this class.
-
Method Details
-
set
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.
-
preDecorateWith
Pre-decorates a targeted object's methods.Each execution of a targeted object's method will be first performed on the same method of the
decorator
object. The result (if any) from the invocation of the targeted object's method will be returned but you can choose to return the decorator result if you want to.Be aware:
- The type of a targeted object should be an interface for this functionality to work
- Any exception caused by an invocation of a
decorator
object's method will result in disrupting the default program's flowExample: Assuming we have the following code:
interface IUploadFileService { boolean upload(String file, String destination); } public class FileManager { private IUploadFileService uploadFileService; private static final String DEFAULT_DESTINATION = "http://example.org/default/destination/"; public void manage(String fileName) { if( uploadFileService.upload(fileName, DEFAULT_DESTINATION) ) { System.out.println("File "+fileName+" sent to "+DEFAULT_DESTINATION); } else { System.out.println("Unable to sent "+fileName+" to "+DEFAULT_DESTINATION); } } }
Let's say we want to decorate the uploadFileService.upload(...) part, so that additional functionality is executed before the actual uploadFileService.upload(...) logic, the following code will do the job:IUploadFileService uploadFileServiceDecorator = ...; FileManager fileManager = new FileManager(); field("uploadFileService").ofType(IUploadFileService.class) .in(fileManager) .preDecorateWith(uploadFileServiceDecorator);
However, if there is an exception when callinguploadFileServiceDecorator.upload(fileName, DEFAULT_DESTINATION)
the default program's flow will be interrupted and theuploadFileService.upload(fileName, DEFAULT_DESTINATION)
will not be executed.- Parameters:
decorator
- which methods be called before the same targeted object methods- Returns:
- the
DecoratedInvoker
pre decorating the target field interface with given decorator.
-
postDecorateWith
Post-decorates a targeted object's methods.After each execution of a targeted object's method, the same method of the
decorator
object will be called. The result (if any) from the invocation of the targeted object's method will be returned but you can choose to return the decorator result if you want to.Be aware:
- The type of a targeted object should be an interface for this functionality to work
- Any exception caused by an invocation of a
decorator
object's method will result in disrupting the default program's flowExample: Assuming we have the following code:
interface IUploadFileService { boolean upload(String file, String destination); } public class FileManager { private IUploadFileService uploadFileService; private static final String DEFAULT_DESTINATION = "http://example.org/default/destination/"; public void manage(String fileName) { if( uploadFileService.upload(fileName, DEFAULT_DESTINATION) ) { System.out.println("File "+fileName+" sent to "+DEFAULT_DESTINATION); } else { System.out.println("Unable to sent "+fileName+" to "+DEFAULT_DESTINATION); } } }
Let's say we want to decorate the uploadFileService.upload(...) part, so that additional functionality is executed before the actual uploadFileService.upload(...) logic, the following code will do the job:IUploadFileService uploadFileServiceDecorator = ...; FileManager fileManager = new FileManager(); field("uploadFileService").ofType(IUploadFileService.class) .in(fileManager) .postDecorateWith(uploadFileServiceDecorator);
However, if there is an exception when callinguploadFileServiceDecorator.upload(fileName, DEFAULT_DESTINATION)
the default program's flow will be interrupted and theuploadFileService.upload(fileName, DEFAULT_DESTINATION)
will not be executed.- Parameters:
decorator
- which methods be called after the same targeted object methods- Returns:
- the
DecoratedInvoker
post decorating the target field interface with given decorator.
-
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
Returns the "real" field managed by this class.- Returns:
- the "real" field managed by this class.
-