简体   繁体   English

在注解方面通过值修改方法的参数

[英]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: 我开始学习aspectJ,我想知道是否可以在文件.aj中创建方面,而不是在.java中创建注释,这是我的示例:

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? 而且我不知道如何在.aj文件中创建此检查,是否有可能?

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. 我建议使用Eclipse AspectJ Developer Tool ,它提供了许多有用的功能,例如智能自动完成,javadocs和方面可视化等。这可能有助于您更快地学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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