Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of a method argument via reflection in Java?

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?

like image 525
soc Avatar asked Aug 29 '11 12:08

soc


People also ask

Can I obtain method parameter name using Java reflection?

You can obtain the names of the formal parameters of any method or constructor with the method java. lang. reflect.

What is method parameter reflection in Java?

Overview. Method Parameter Reflection support was added in Java 8. Simply put, it provides support for getting the names of parameters at runtime. In this quick tutorial, we'll take a look at how to access parameter names for constructors and methods at runtime – using reflection.

How do you find the instance of a class using reflection?

We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don't have the classes information at compile time, we can assign it to Object and then further use reflection to access it's fields and invoke it's methods.

Which method is used to fetch the parameter types using method parameter reflection is?

reflect package is used to fetch the parameter types using method parameter reflection. Reflection is a process of analyzing and modifying all capabilities of class at runtime. It is also used to manipulate private members of the class which includes fields, methods and constructors, etc..


1 Answers

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
    }

}
like image 145
Sean Patrick Floyd Avatar answered Sep 23 '22 22:09

Sean Patrick Floyd