Compilation Java代码中的Java注释处理
这是与我的帖子相关的进一步问题-> 我已经完成了以下代码,用java编译器替换apt工具 首先,我使用javax库创建一个注释处理器,如下所示:Compilation Java代码中的Java注释处理,compilation,annotations,java-8,Compilation,Annotations,Java 8,这是与我的帖子相关的进一步问题-> 我已经完成了以下代码,用java编译器替换apt工具 首先,我使用javax库创建一个注释处理器,如下所示: public class MyAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironme
public class MyAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
System.out.println("annotation processing");
return false;
}
}
但问题是上述语句返回true,但不会触发System.out.printlnannotation处理语句
有人能看到这里有什么明显的东西吗
非常感谢可能是因为您的处理器对任何注释都不感兴趣。它的类必须要么是,要么用注释,要么重写Good one mate。因此,要处理在可编译代码中找到的所有注释,我的处理器类如下所示:Good one mate。因此,要处理在可编译代码中找到的所有注释,我的Processor类将如下重写该方法,现在至少触发process方法。@Override public Set getSupportedAnnotationTypes{HashSet supportedAnnotations=new HashSet;supportedAnnotations.add*;return supportedAnnotations;}@Override public Set getSupportedAnnotationTypes{return;}但是将@SupportedAnnotationTypes*添加到类声明中会更简单…
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("C:/Test/FileToCompile.java"));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
null, compilationUnits);
ArrayList<Processor> processors = new ArrayList<Processor>();
processors.add( new MyAnnotationProcessor() );
task.setProcessors( processors );
boolean success = task.call();