Java代码在编译时转换 [英] Java code transform at compile time

查看:162
本文介绍了Java代码在编译时转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将源代码传递给编译器之前在编译时 转换 java源代码。换句话说,我想创建一个预处理器来转换

 bla bla bla

添加到任何其他代码中,例如:

  new MyClass(bla,3)



是做字符串加密,如解释这里



有些人建议写自定义注释处理器,但据我理解注释:




  • 它们可以用于生成新的类文件,但不能在传递给编译器之前转换现有代码

  • 它们似乎在包,类或方法级别工作



有些人建议使用像Spoon或ObjectsWeb ASM这样的框架,但这些框架似乎很复杂,部署在现有的代码库。



我发现一个简单的java代码预处理的两个方法的例子。



有没有人看到任何聪明的方式做代码转换,没有完全改变现有的大代码基地与多个ivy模块?注释似乎是最好的方法,但我不明白如何做。

解决方案

我想你可以尝试 Project Lombok 中使用的相同技术



这是由作者在这次采访


在引擎盖下?即

Reinier :注释处理器API只允许您创建新文件,它不允许您修改其中有注释的文件。这是Lombok所做的,所以Lombok不使用注释处理器API。



而是,Lombok使用注释处理器API作为一种机制,将自己注入到编译过程。所有注释处理器在编译过程的早期初始化,Lombok在初始化为注释处理器时修改javac。我们只改变一件事情:在javac继续之前,AST(原始源代码,解析成树形式)首先传递给Lombok,它生成需要生成的任何东西。




lombok如何运作?



还可以将Project Lombok扩展到您的需要




I would like to transform java source code at compile time just before passing the source code to the compiler. In other word I would like to create a preprocessor able to transform

"bla bla bla" 

into any other code such as:

new MyClass("bla", 3) 

My actual motivation is to do string encryption, as explained here

Some people suggest writing custom annotation processors but as far as I understand annotations:

  • they can be used to generate new class file, but not to transform existing code before being passed to compiler
  • they seem to work at package, class or method level, but not method body/implementation.

Some people suggest using frameworks like Spoon or ObjectsWeb ASM, but these frameworks seem complicated to learn and deploy on an existing code base.

I thrive to find a simple example of java code preprocessing for both approaches.

Does anybody see any smart way of doing code transform, without completely changing an existing large code base with multiple ivy module? Annotations seem to be the best way, but I don't understand how to do that.

解决方案

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 lombok works?

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

这篇关于Java代码在编译时转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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