AspectJ - Get parameter value of annotated method

I created a custom annotation @MyAnn . And I will annotate the method parameters with it.

For example: public static call(@MyAnn String name){...}

Using AspectJ, how can I access and update the values โ€‹โ€‹of all parameters annotated with the annotation?

I found a sample code showing how to create pointcuts designed for custom annotations here .

So now I have created an aspect with pointcut. But I do not know how hot to get the value of a parameter annotated with MyAnn .

 @Aspect public class MyAnnAspect { @Around("execution(@my.package.test.MyAnn") // I hope this pointcut will work public void changeParameter(final ProceedingJoinPoint pjp) throws Throwable { // How I can there get parameter value (and chage it)? } } 
+8
java aspectj
source share
1 answer

I do not think this pointcut works because it is not a method that is annotated, as you can do:

 MethodSignature ms = (MethodSignature) pjp.getSignature(); Method m = ms.getMethod(); Annotation[][] pa = m.getParameterAnnotations(); 

Now you can iterate over annotations and find the correct annotation, if present, get the parameter value by calling pjp.getArgs() .

+17
source share

All Articles