简体   繁体   English

如何通过自定义 aspectJ 注释传入操作以对方法参数进行操作?

[英]How can I pass in operations to make on a method parameter via a custom aspectJ annotation?

It is non-spring and operationsToDo needs to be able to handle any operations on the first parameter.它是非弹簧的,operationsToDo 需要能够处理对第一个参数的任何操作。 I need to be able to do operationsToDo from the annotation on param.我需要能够从参数上的注释中执行操作ToDo。 See below code example请参见下面的代码示例

Annotation example A:注释示例A:

@MyCustomAnnotation(operationsToDo=".getIdentity().getUsername()")
public Int getHighScore(User user, Game game) {
    ...
}

Annotation example B:注释示例 B:

@MyCustomAnnotation(operationsToDo=".getEmail().get(0).getSubject()")
public List<Cat> getUsersCats(User user) {
    ...
}

Aspect File:方面文件:

@Aspect
class MyCustomAspect {
    @Around(value = "@annotation(myCustomAnnotation)", argNames = "joinPoint,myCustomAnnotation")
    public Object getData(ProceedingJoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) throws Throwable {
        Object param = joinPoint.getArgs()[0];
        String something = *Do operationsToDo on param*;
        System.out.println(something);
        joinPoint.proceed();
    }

}

You cannot just write code into a text constant as an annotation parameter and expect it to magically get compiled and executed in a language like Java.您不能只是将代码作为注释参数写入文本常量,并期望它神奇地以 Java 之类的语言编译和执行。 This just is not how it works.这不是它的工作方式。 Please change your application design and use a design pattern better suited to solving your problem.请更改您的应用程序设计并使用更适合解决您的问题的设计模式。

It would be easy to get a reference to the annotation and its parameter value in AspectJ, but that does not give you compiled and executable code.在 AspectJ 中很容易获得对注解及其参数值的引用,但这不会为您提供已编译和可执行的代码。

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

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