17

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?

soc
  • 27,983
  • 20
  • 111
  • 215
  • 3
    I don't get the question. The value of `bar` will only be available during runtime. Do you want to intercept calls to `example`? Or do you mean the datatype `Bar`? – home Aug 29 '11 at 12:54
  • look at my answer here https://stackoverflow.com/a/73263342/5050750 – Amir Aug 06 '22 at 21:33

1 Answers1

22

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:

  • AOP (AspectJ / Spring AOP etc.)
  • Proxies (JDK, CGLib etc)

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

public aspect ValidationAspect {

    pointcut serviceMethodCall() : execution(public * com.yourcompany.**.*(..));

    Object around(final Object[] args) : serviceMethodCall() && args(args){
        Signature signature = thisJoinPointStaticPart.getSignature();
        if(signature instanceof MethodSignature){
            MethodSignature ms = (MethodSignature) signature;
            Method method = ms.getMethod();
            Annotation[][] parameterAnnotations = 
                method.getParameterAnnotations();
            String[] parameterNames = ms.getParameterNames();
            for(int i = 0; i < parameterAnnotations.length; i++){
                Annotation[] annotations = parameterAnnotations[i];
                validateParameter(parameterNames[i], args[i],annotations);
            }
        }
        return proceed(args);
    }

    private void validateParameter(String paramName, Object object,
        Annotation[] annotations){

        // validate object against the annotations
        // throw a RuntimeException if validation fails
    }

}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    I'm already using AspectJ, but I haven't found a way to do it. This was the earlier question: http://stackoverflow.com/questions/7228590/how-to-check-if-a-parameter-of-the-current-method-has-an-annotation-and-retrieve – soc Aug 29 '11 at 14:09
  • @soc OK, see my update for starters, buy [AspectJ in Action](http://www.manning.com/laddad2/) to really learn how to do it (or ask questions on the [AspectJ users list](http://www.eclipse.org/aspectj/userlists.php) – Sean Patrick Floyd Aug 29 '11 at 14:29
  • Hey thanks! :-) I already bought the book, but I tried to put everything into the match signatures, which didn't work... – soc Aug 29 '11 at 14:35
  • @soc I know. Matching annotations on a variable number of annotations on method parameters just won't work. – Sean Patrick Floyd Aug 29 '11 at 14:38
  • Edit: Mhhh, does this example also work with the annotation syntax? I can't find an approrpiate import for `thisJoinPointStaticPart`. – soc Aug 29 '11 at 14:42
  • @soc with `@AspectJ` syntax, your aspect needs a parameter of type `ProceedingJoinPoint`. – Sean Patrick Floyd Aug 29 '11 at 14:45