微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在最终编译之前如何使用java注释来修改源代码?

我从apt工具页面读取可以创建AnnotationProcessors到 generate new derived files (source files,class files,deployment descriptors,etc.).我正在寻找例子来做到这一点.

我的需要是在编译时对所有注释字符串进行编码,以便读取类文件不允许读取静态字符串:

基本代码

String message = (@Obfuscated "a string that should not be readable in class file");

应该重做:

String message = new ObfuscatedString(new long[] {0x86DD4DBB5166C13DL,0x4C79B1CDC313AE09L,0x1A353051DAF6463BL}).toString();

基于静态ObfuscatedString.obfuscate(String) method of the TrueLicense framework,处理器可以生成代码来替换注释字符串.的确,此方法生成字符串“new ObfuscatedString([numeric_code])toString()”.
在运行时,ObfuscatedString的toString()方法能够返回用数字代码编码的字符串.

任何关于如何编写AnnotationProcessor的process()方法来编辑注释代码的想法?

提前致谢,

解决方法

你可以有
String message = Obfuscated.decode("a string that should not be readable in class file");

然而在编译之后,您有一个工具来检查字节码例如使用ObjectWeb的ASM,更改字符串文字,因此它看起来像

String message = Obfuscated.decode("\u86DD\u4DBB\u5166\uC13D\u4C79\uB1CD\uC313\uAE09\u1A35\u3051\uDAF6\u463B");

为了更容易地识别需要更改的字符串,您可以为它们添加前缀,并且可以确保在前缀已经被模糊化之后出现.

String s = "Obfuscate: a string that should not be readable in class file";
// later
String message = Obfuscated.decode(s);

原文地址:https://www.jb51.cc/java/124106.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐