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
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();
}
}

- 2,590
- 26
- 36
-
1if you want to map it String[] names = codeSignature.getParameterNames(); Object[] args = joinPoint.getArgs(); Map
fieldToValue = new HashMap<>(); for (int i = 0; i < names.length; i++) { fieldToValue.put(names[i], args[i]); } – Денис Бурмистров Apr 12 '21 at 07:01
CodeSignature methodSignature = (CodeSignature) joinPoint.getSignature();
String[] sigParamNames = methodSignature.getParameterNames();
You can get method signature arguments names.

- 457
- 4
- 20

- 489
- 4
- 11
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
- 1

- 5,103
- 3
- 39
- 48
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

- 1,508
- 1
- 11
- 20
-
Of course, you can get all arguments, but you CANNOT get arguments names, it's impossible – Mykola Yashchenko Oct 03 '17 at 08:05
-
https://stackoverflow.com/questions/5720494/aspectj-joinpoint-question first answer provides code examples for retrieving parameter names and values. This does require using AspectJ. – Reenactor Rob Oct 04 '17 at 02:57