简体   繁体   中英

Modify parameter of method by value in annotation aspectJ

I started learing aspectJ and I wondering is it possible to create aspect in file .aj instead of annotation in .java, this is my example:

I have this aspect which modify value of parameter in method

@Around("execution(* *(..)) && @annotation(Te)")
public Object setupParam(ProceedingJoinPoint pjp) throws Throwable {
    Object[] args = pjp.getArgs();
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    Te myAnnotation = method.getAnnotation(Te.class);
    if (args != null) 
        args[0] = (int) args[0] * myAnnotation.w();
    return pjp.proceed(args);
}

and I don't know how to create this asspect in .aj file, is it even possible?

Yes, that is possible.

public aspect MyAspect {

    public MyAspect() {
      System.out.println("Aspect instance created");
    }

   pointcut myPointcut(ParameterType parameter)
               : ("execution(* *(..)) && @annotation(Te));

    Object around(ParameterType parameter) : myPointcut(parameter) {
       // Business logic here
       // 'thisJoinPointStaticPart' will give you access to join point
       // 'this' will give you access to advice instance itself
       // `return proceed();` will allow you to execute advised join point
    }
}

I would suggest to use Eclipse AspectJ Developer Tool , which provides many useful features, like intellisence autocomplete, javadocs and aspect visualization and etc. This might help you to learn faster.

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