Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ - Get value of annotated method parameter

Tags:

java

aspectj

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)? 
    }
}
like image 620
WelcomeTo Avatar asked Feb 10 '13 17:02

WelcomeTo


1 Answers

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().

like image 105
Amir Pashazadeh Avatar answered Nov 01 '22 11:11

Amir Pashazadeh