簡體   English   中英

在注解方面通過值修改方法的參數

[英]Modify parameter of method by value in annotation aspectJ

我開始學習aspectJ,我想知道是否可以在文件.aj中創建方面,而不是在.java中創建注釋,這是我的示例:

我有這方面修改方法中參數的值

@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);
}

而且我不知道如何在.aj文件中創建此檢查,是否有可能?

是的,那是可能的。

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

我建議使用Eclipse AspectJ Developer Tool ,它提供了許多有用的功能,例如智能自動完成,javadocs和方面可視化等。這可能有助於您更快地學習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM