简体   繁体   中英

AspectJ - Get value of annotated method parameter

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)? 
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM