如何在最终编译之前使用java注释修改源代码?
我从 apt 工具页面读到,可以创建 AnnotationProcessors 到 生成新的派生文件(源文件、类文件、部署描述符等)。我正在寻找这样做的例子。
我的需要是在编译时对所有带注释的字符串进行编码,以便读取类文件不允许读取静态字符串:
基本代码:
String message = (@Obfuscated "a string that should not be readable in class file");
应该重新设计为:
String message = new ObfuscatedString(new long[] {0x86DD4DBB5166C13DL, 0x4C79B1CDC313AE09L, 0x1A353051DAF6463BL}).toString();
基于静态 TrueLicense 框架的ObfuscatedString.obfuscate(String)
方法,处理器可以生成代码来替换带注释的字符串。事实上,此方法生成字符串“new ObfuscatedString([numeric_code]).toString()”。 在运行时,ObfuscatedString 的 toString() 方法能够返回以数字代码编码的字符串。
关于如何编写 AnnotationProcessor 的 process() 方法来编辑带注释的代码有什么想法吗?
提前致谢,
I've read from the apt tool page that one can create AnnotationProcessors to generate new derived files (source files, class files, deployment descriptors, etc.). I am looking for example to do so.
My need is to encode all annotated strings at compile time, so that reading the class file does not allow reading the static strings:
Base code:
String message = (@Obfuscated "a string that should not be readable in class file");
Should be reworked as:
String message = new ObfuscatedString(new long[] {0x86DD4DBB5166C13DL, 0x4C79B1CDC313AE09L, 0x1A353051DAF6463BL}).toString();
Based on the static ObfuscatedString.obfuscate(String)
method of the TrueLicense framework, the processor can generate the code to replace the annotated string. Indeed, this method generates the string "new ObfuscatedString([numeric_code]).toString()".
At runtime the toString() method of ObfuscatedString is able to return the string encoded in the numeric code.
Any idea on how to write the process() method of the AnnotationProcessor to edit the annotated code?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以
正常编译,但是编译后你有一个工具可以检查字节码,例如使用ObjectWeb的ASM,更改字符串文字,使其看起来像
为了更容易识别需要更改的字符串,你可以添加一个前缀它们,并且您可以确保在代码被混淆后该前缀确实出现。
You could have
which compiles normally, however after compilation you have a tool which examines the bytecode e.g. using ObjectWeb's ASM, changes the String literal so it looks like
To make it easer to identify strings which need to be changed, you could add a prefix to them, and you could ensure this prefix does appear after the code has been obfuscated.
我敢打赌 spoon 可能就是您正在寻找的东西。但为什么要混淆静态字符串呢?
I bet spoon may be what you are looking for. But why do you want to obfuscate static strings?
Zelix KlassMaster 提供了此功能。如果没记错的话,以前对3个开发者以下的公司是免费的,但我刚刚查看了他们的购买页面,他们现在似乎对小公司收取小开发者费用。我已经好几年没有使用它了(至少有五七年了),但它在混淆字符串和一般代码方面做得非常出色。
Zelix KlassMaster offers this ability. If memory serves, it used to be free for companies under 3 developers, but I just checked their purchase page, and it seems that they now charge a small-developers fee for small companies. I haven't used it in several years (at least 5 or 7), but it did a fantastic job obfuscating strings, and code in general.
当我要生成用于分发的版本时,我有一个类,它使用 Ofuscated String 调用覆盖所有常量:
这是过程:
ANT:
Java Ofuscate 代码 (ObfuscatedString):
希望有帮助:)
When I am going to generate the version for distribution it, I have a class that override all the constants with the Ofuscated String call:
This is the process:
The ANT:
The Java Ofuscate code (ObfuscatedString):
Hope it helps :)