在eclipse中调试注释处理器

use*_*318 12 java eclipse ant remote-debugging annotation-processing

我正在编写一个简单的注释处理器并尝试使用eclipse进行调试.我为注释处理器创建了一个新项目,并根据需要在META-INF下配置了javax.annotation.processing.Processor,它可以很好地处理注释.

然后,我添加了一些代码并尝试调试,但永远不会在注释处理器中添加的断点处停止执行.我正在使用ant编译,我正在使用以下ANT选项.

export ANT_OPTS =" - Xdebug -Xrunjdwp:transport = dt_socket,server = y,suspend = y,address = 8000"

在触发ant build之后,我将创建一个远程调试配置,调试器启动正常.Ant构建也成功启动.但是在注释处理器中添加的任何断点处执行都不会停止.

ekj*_*ekj 24

这是我遇到的一个问题,eclipse插件解决方案对我来说似乎非常麻烦.我找到了一个更简单的解决方案,使用javax.tools.JavaCompiler来调用编译过程.使用下面的代码,您可以在eclipse中右键单击> Debug As> JUnit Test并直接从那里调试注释处理器

   @Test
   public void runAnnoationProcessor() throws Exception {
      String source = "my.project/src";

      Iterable<JavaFileObject> files = getSourceFiles(source);

      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

      CompilationTask task = compiler.getTask(new PrintWriter(System.out), null, null, null, null, files);
      task.setProcessors(Arrays.asList(new MyAnnotationProcessorClass()));

      task.call();
   }

   private Iterable<JavaFileObject> getSourceFiles(String p_path) throws Exception {
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
     StandardJavaFileManager files = compiler.getStandardFileManager(null, null, null);

     files.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(new File(p_path)));

     Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);
     return files.list(StandardLocation.SOURCE_PATH, "", fileKinds, true);
   }
Run Code Online (Sandbox Code Playgroud)