T
- the declared type for the field to access.public final class Invoker<T> extends 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);
Modifier and Type | Method and Description |
---|---|
T |
get()
Returns the value of the field managed by this class.
|
Field |
info()
Returns the "real" field managed by this class.
|
DecoratedInvoker<T> |
postDecorateWith(T decorator)
Post-decorates a targeted object's methods.
|
DecoratedInvoker<T> |
preDecorateWith(T decorator)
Pre-decorates a targeted object's methods.
|
void |
set(T value)
Sets a value in the field managed by this class.
|
public void set(T value)
value
- the value to set.ReflectionError
- if the given value cannot be set.public DecoratedInvoker<T> preDecorateWith(T decorator)
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:
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 calling
uploadFileServiceDecorator.upload(fileName, DEFAULT_DESTINATION)
the default program's flow will be
interrupted and the uploadFileService.upload(fileName, DEFAULT_DESTINATION)
will not be executed.
decorator
- which methods be called before the same targeted object methodsDecoratedInvoker
pre decorating the target field interface with given decorator.public DecoratedInvoker<T> postDecorateWith(T decorator)
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:
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 calling
uploadFileServiceDecorator.upload(fileName, DEFAULT_DESTINATION)
the default program's flow will be
interrupted and the uploadFileService.upload(fileName, DEFAULT_DESTINATION)
will not be executed.
decorator
- which methods be called after the same targeted object methodsDecoratedInvoker
post decorating the target field interface with given decorator.public T get()
ReflectionError
- if the value of the field cannot be retrieved.public Field info()
Copyright © 2007-2013 FEST (Fixtures for Easy Software Testing). All Rights Reserved.