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);
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T> Invoker<T>createInvoker(String fieldName, Class<?> expectedType, Object target) private static Fieldget()Returns the value of the field managed by this class.private static <T> T(package private) static ObjectgetNestedField(String fieldName, Object target) private static ReflectionErrorincorrectFieldType(Field field, Class<?> actual, Class<?> expected) info()Returns the "real" field managed by this class.private static FieldlookupInClassHierarchy(String fieldName, Class<?> declaringType) (package private) static <T> Invoker<T>newInvoker(String fieldName, Class<T> expectedType, Object target) (package private) static <T> Invoker<T>newInvoker(String fieldName, TypeRef<T> expectedType, Object target) postDecorateWith(T decorator) Post-decorates a targeted object's methods.preDecorateWith(T decorator) Pre-decorates a targeted object's methods.voidSets a value in the field managed by this class.private static Class<?>private static voidverifyCorrectType(Field field, Class<?> expectedType)
-
Field Details
-
target
-
field
-
accessible
private final boolean accessible -
expectedType
-
-
Constructor Details
-
Invoker
-
-
Method Details
-
newInvoker
-
newInvoker
-
createInvoker
-
typeOf
-
lookupInClassHierarchy
-
verifyCorrectType
-
field
-
incorrectFieldType
-
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
decoratorobject. 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
decoratorobject'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
DecoratedInvokerpre 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
decoratorobject 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
decoratorobject'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
DecoratedInvokerpost 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.
-
getNestedField
-
get
-
info
Returns the "real" field managed by this class.- Returns:
- the "real" field managed by this class.
-