最终编译前如何使用java注解修改源代码? [英] How to use java annotations to modify source code before final compilation?

查看:78
本文介绍了最终编译前如何使用java注解修改源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 apt 工具页面读到可以创建 AnnotationProcessors 到 生成新的派生文件(源文件、类文件、部署描述符等).我正在寻找这样做的例子.

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:

基本代码:

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)方法a>,处理器可以生成代码来替换带注释的字符串.实际上,此方法生成字符串new ObfuscatedString([numeric_code]).toString()".在运行时,ObfuscatedString 的 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.

知道如何编写 AnnotationProcessor 的 process() 方法来编辑带注释的代码吗?

Any idea on how to write the process() method of the AnnotationProcessor to edit the annotated code?

提前致谢,

推荐答案

我敢打赌 spoon 可能是你在找什么.但是为什么要混淆静态字符串呢?

I bet spoon may be what you are looking for. But why do you want to obfuscate static strings?

这篇关于最终编译前如何使用java注解修改源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆