编译时的Java注释成员验证
我有一个自定义java注释,如下所示编译时的Java注释成员验证,java,Java,我有一个自定义java注释,如下所示 @customelement(folder = "/path/") public testMethod() { } 我想验证文件夹成员变量的文件夹路径/path/,并在java源代码中报告错误。我怎么能做到 另外,请记住使用@SupportedAnnotationTypespackage.CustomAnnotation和@SupportedSourceVersionSourceVersion.RELEASE_7或您将支持的任何Java语法版本为您的Co
@customelement(folder = "/path/")
public testMethod() {
}
我想验证文件夹成员变量的文件夹路径/path/,并在java源代码中报告错误。我怎么能做到
另外,请记住使用@SupportedAnnotationTypespackage.CustomAnnotation和@SupportedSourceVersionSourceVersion.RELEASE_7或您将支持的任何Java语法版本为您的CompileTimeAnnotationProcessor添加注释
如果还有其他问题,我建议您阅读这篇很棒的教程博客。您想什么时候报告错误?编译此类时,这是不可能的。很抱歉除了编译阶段之外,还有其他方法吗?您可以在运行时执行此操作,但需要类的实例。或者为构建管理工具编写插件。
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for(Element te : elements){
CustomAnnotation annotation = te.getAnnotation(CustomAnnotation.class);
String folder = annotation.folder();
//... do sth with folder in context of te.
//te represent annotated method, e.g. testMethod from your example
}
return true;
}