我正在尝试使用 grails 创建自定义注释。我认为我不需要 AST 转换。我只想在调用方法(及其参数)之前添加一些验证。
我创建了以下内容:
>接口(interface)(ValidateSomething.java)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ValidateSomething {
}
>处理器 (ValidateSomethingProcessor.java)
@SupportedAnnotationTypes({"annotations.ValidateSomething"})
class ValidateSomethingProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
System.out.println("Hello world! :D");
return true;
}
}
> {grails root}/web-app/META-INF/services/javax.annotation.processing.Processor
annotations.api.ValidateSomething
>测试 Controller
@ValidateSomething
def index() {
println "test"
}
当我运行 Controller 操作时,它只打印“test”。
我是不是忘记了什么?我还需要做其他事情吗?
谢谢。
最佳答案
注释处理是一个用 javac 构建的工具,用于在编译时而不是运行时扫描和处理注释。你想要做的是拦截方法调用。为此,您可以使用java拦截器或aspectj。
关于java注释处理器与grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012188/