Java code transform at compile time

穿精又带淫゛_ 提交于 2019-11-30 01:37:38
fglez

I think you could try the same technique used in Project Lombok

It's approximately explained by the authors in this interview:

What's going on under the hood? I.e., how does an annotation result in the boilerplate ending up in the bytecode?

Reinier: The annotation processor API only lets you create new files, it does not let you modify the file that has the annotation inside of it. Which is what Lombok does, so Lombok does not use the annotation processor API.

Instead, Lombok uses the annotation processor API only as a mechanism to inject itself into the compilation process. All annotation processors are initialized early in the compilation process, and Lombok modifies javac when it is initialized as an annotation processor. We change only one thing: The AST (the raw source code, parsed into a tree form) is first handed off to Lombok, which generates whatever needs to be generated, before javac continues.

and in How does lombok work?

It's also possible to extend Project Lombok to your needs

There is someone who already wrote a small C-like preprocessor ant plugin in python/jython. You can find it here. Note that I have never used it, but perhaps it can be a good starting point for your needs.

There is also a java-comment-preprocessor maven plugin (similar style) in google code that might also be a helpful starting point.

Good luck. Sounds like a fun challenge. Of course, keep in mind that obfuscation is just making it a little more challenging to extract the string, but still not impossible. If you really want to make it significantly more difficult, then you might want to look at encrypting your strings and/or using AOP.

Josh S

If the C preprocessor would suffice, I did just manage to get it working with Eclipse in Windows. It only works on Juno though.

https://stackoverflow.com/a/10497206/1137626

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!