Consider this code:
public void example(String s, int i, @Foo Bar bar) {
/* ... */
}
I’m interested in the value of the argument annotated with @Foo. Assume that I have already figured out via reflection (with Method#getParameterAnnotations()) which method parameter has the @Foo annotation. (I know it is the third parameter of the parameter list.)
How can I now retrieve the value of bar for further usage?
You can’t. Reflection does not have access to local variables, including method parameters.
If you want that functionality, you need to intercept the method call, which you can do in one of several ways:
In all of these, you would gather the parameters from the method call and then tell the method call to execute. But there’s no way to get at the method parameters through reflection.
Update: here’s a sample aspect to get you started using annotation-based validation with AspectJ