如何向 Java 添加(次要的)语法糖?
假设我想向 Java 添加一些小的语法糖。只是一些小事情,比如添加正则表达式模式文字,或者可能是基数 2 文字,或多行字符串等。语法上没有什么大不了的(至少现在是这样)。
人们会如何去做这件事呢?
我需要扩展字节码编译器吗? (这可能吗?)
我可以编写 Eclipse 插件来执行简单的源代码转换,然后再将其提供给标准 Java 编译器吗?
Suppose I want to add minor syntactic sugars to Java. Just little things like adding regex pattern literals, or perhaps base-2 literals, or multiline strings, etc. Nothing major grammatically (at least for now).
How would one go about doing this?
Do I need to extend the bytecode compiler? (Is that possible?)
Can I write Eclipse plugins to do simple source code transforms before feeding it to the standard Java compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会看一下 Project Lombok 并尝试重用他们所做的尝试。他们使用 Java 5 注释来挂钩 Java 代理,该代理可以在编译代码之前操作抽象语法树。他们目前正在致力于创建一个 API,以允许编写可与 javac 或 Eclipse 和 NetBeans 等主要 IDE 一起使用的自定义转换器。除了触发生成代码的注释之外,他们还计划添加语法更改(可能是 mixin 或 Java 7 之前的闭包语法)。
(我可能有一些细节略有偏差,但我认为我已经非常接近了)。
Lombok 是开源的,因此研究他们的代码并尝试在此基础上进行构建可能是一个好的开始。
如果失败,您可以尝试更改 javac 编译器。不过据我所知,对于非编译器和 Java 专家的人来说,这可能是一项令人沮丧的令人沮丧的工作。
I would take a look at Project Lombok and try to reuse the attempt they take. They use Java 5 annotations to hook in a Java agent which can manipulate the abstract syntax tree before the code is compiled. They are currently working on creating an API to allow custom transformers to be written which can be used with javac, or the major IDEs such as Eclipse and NetBeans. As well as annotations which trigger code to be generated, they are also planning on adding syntax changes (possibly mixin or pre-Java 7 closure syntax).
(I may have some of the details slightly off, but I think I'm pretty close).
Lombok is open source so studying their code and trying to build on that would probably be a good start.
Failing that, you could attempt to change the javac compiler. Though from what I've heard that's likely to be a hair-pulling exercise in frustration for anyone who is not a compiler and Java expert.
您可以使用 JSR 269 破解
javac
(可插入注释处理)尤其如此。您可以挂钩遍历源代码中的语句的访问者并对其进行转换。例如,以下是添加 java 中对罗马数字的支持(当然请阅读完整的帖子 了解更多详细信息)。看起来比较容易。
以下是其他资源:
我不知道 Lombok 项目是否(在另一个答案中引用) )使用相同的技术,但我想是的。
You can hack
javac
with JSR 269 (pluggable annotation processing) notably. You can hook into the visitor that traverse the statements in the source code and transform it.Here is for instance the core of a transformation to add support for roman number in java (read of course the complete post for more details). It seems relatively easy.
Here are additional resources:
I don't know if project Lombok (cited in the other answer) uses the same technique, but I guess yes.
JRuby 的技术主管 Charles Nutter 用文字正则表达式扩展了 Javac。据我记得,他必须更改大约 3 行大小写。
请参阅 http://twitter.com/headius/status/1319031705
这是一个很棒的教程关于如何向 javac 添加新运算符,http://www.ahristov.com/ Tutorial/java-compiler.html
有关更多类似链接,请参阅我的链接列表对于 javac 黑客。
Charles Nutter, the tech lead of JRuby, extended Javac with literal regular expressions. He had to change about 3 lines of case, as far I recall.
See http://twitter.com/headius/status/1319031705
And here is an awesome tutorial on how to add a new operator to javac, http://www.ahristov.com/tutorial/java-compiler.html
For more links like that, see my list of Links for javac hackers .
JRuby 的技术主管 Charles Nutter 用文字正则表达式扩展了 Javac。据我记得,他必须更改大约 3 行大小写。
请参阅http://twitter.com/headius/status/1319031705
Charles Nutter, the tech lead of JRuby, extended Javac with literal regular expressions. He had to change about 3 lines of case, as far I recall.
See http://twitter.com/headius/status/1319031705