在另一篇文章(Scala, Maven, and preprocessors)中,我问到如何使用像m4这样的工具对Java和Scala进行预处理。我需要添加__FILE__和__LINE__功能(请不要问“用例”问题)。有人建议查看Java编译器插件(javax.annotation.processing.Processor)。
如何使用特殊的注释(可能是@File、@Line或@FileLine )来做这件事呢?任何与此类似的例子都将不胜感激。
发布于 2010-08-04 21:31:19
在对上一个问题的评论中,您提到了http://www.gallot.be/?p=85,它使用了javaagent。修改代码以在预处理步骤中运行相同转换应该相对容易。您需要将CodeLocationClassAdapter提取到它自己的toplevel类中,并为每个类文件调用它,如下所示:
String name = "com/stackoverflow/Test.class";
byte[] bytes = // read bytes of the classfile from disk
ClassReader cr = new ClassReader(bytes);
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor cv = new CodeLocationClassAdapter(cw);
cr.accept(cv, 0);
// write modified class file
OutputStream out = new FileOutputStream(name);
out.write(cw.toByteArray());
out.close();发布于 2010-08-04 21:37:40
如果我理解正确的话,标准的方法是使用JSR-45,就像JSP-pages允许调试一样。
这是您选择的预处理器的一个选项吗?
https://stackoverflow.com/questions/3405783
复制相似问题