17

I am using Spring 4.3. Is it possible to get method parameter names and values passed to it? I believe this can be done using AOP (before advice) if possible could you please give me a source code.

4 Answers 4

27

The following works as expected (Java 8 + Spring 5.0.4 + AspectJ 1.8.13):

@Aspect
@Component
public class SomeAspect {

    @Around("@annotation(SomeAnnotation)")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();

        System.out.println("First parameter's name: " + codeSignature.getParameterNames()[0]);
        System.out.println("First argument's value: " + joinPoint.getArgs()[0]);

        return joinPoint.proceed();
    }
}
1
  • 1
    if you want to map it String[] names = codeSignature.getParameterNames(); Object[] args = joinPoint.getArgs(); Map<String, Object> fieldToValue = new HashMap<>(); for (int i = 0; i < names.length; i++) { fieldToValue.put(names[i], args[i]); } Apr 12, 2021 at 7:01
16
CodeSignature methodSignature = (CodeSignature) joinPoint.getSignature();
String[] sigParamNames = methodSignature.getParameterNames(); 

You can get method signature arguments names.

1
0

Unfortunately, you can't do this. It is a well-known limitation of bytecode - argument names can't be obtained using reflection, as they are not always stored in bytecode.

As workaround, you can add additional annotations like @ParamName(name = "paramName").

So that, you can get params names in the following way:

MethodSignature.getMethod().getParameterAnnotations()

UPDATE

Since Java 8 you can do this

You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. (The classes Method and Constructor extend the class Executable and therefore inherit the method Executable.getParameters.) However, .class files do not store formal parameter names by default. This is because many tools that produce and consume class files may not expect the larger static and dynamic footprint of .class files that contain parameter names. In particular, these tools would have to handle larger .class files, and the Java Virtual Machine (JVM) would use more memory. In addition, some parameter names, such as secret or password, may expose information about security-sensitive methods.

To store formal parameter names in a particular .class file, and thus enable the Reflection API to retrieve formal parameter names, compile the source file with the -parameters option to the javac compiler.

https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html

1
  • This is possible. See my answer on this page. Oct 3, 2017 at 3:49
0

In your AOP advice you can use methods of the JoinPoint to get access to methods and their parameters. There are multiple examples online and at stackoverflow.

Get method arguments using spring aop?

For getting arguments: https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/apidocs/org/jboss/aop/joinpoint/MethodInvocation.html#getArguments()

For getting method details: https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/apidocs/org/jboss/aop/joinpoint/MethodInvocation.html#getMethod%28%29

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.