gpt4 book ai didi

Java APT 和 __LINE__

转载 作者:行者123 更新时间:2023-11-30 05:10:50 24 4
gpt4 key购买 nike

在另一篇文章 (Scala, Maven, and preprocessors) 中,我询问了有关使用 m4 等工具预处理 Java 和 Scala 的问题。我需要添加 __FILE__ 和 __LINE__ 功能(请不要提出“用例”问题)。有人建议检查 Java 编译器插件 (javax.annotation.processing.Processor)。

如何使用特殊注释(@File@Line@FileLine 也许)来做到这一点?任何与此类似的示例将不胜感激。

最佳答案

在对您上一个问题的评论中您提到http://www.gallot.be/?p=85 ,它使用 javaagent。修改该代码以在预处理步骤中运行相同的转换应该相对容易。您需要将 CodeLocationClassAdapter 提取到它自己的顶级类中,并为每个类文件像这样调用它:

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();

关于Java APT 和 __LINE__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3405783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com