9
votes

I created custom annotation @MyAnn. And I will annotate 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 some sample code showing how to create pointcuts targeting custom annotations, here.

So for now, I created an aspect with a pointcut. But I don't know hot to get value of 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)? 
    }
}
1

1 Answers

17
votes

I don't think that pointcut work, because it is not the method which is annotated, by the way you can do:

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

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