插入Java编译器 [英] Plugging in to Java compilers

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

问题描述

我有一个后编译步骤,操作生成的类的Java字节码。我想让图书馆消费者尽可能无痛苦的生活,所以我正在寻找一种方法,我可以使这个过程自动和(如果可能的话)编译器不可知。

I have a post-compilation step that manipulates the Java bytecode of generated classes. I'd like to make life as painless as possible for library consumers, so I'm looking at ways I can make this process automatic and (if possible) compiler agnostic.

注释处理API 提供了许多所需的功能(自动服务发现;由Eclipse支持)。不幸的是,这是针对代码生成器和不支持对现有工件的操作

The Annotation Processing API provides many of the desired features (automatic service discovery; supported by Eclipse). Unfortunately, this is aimed at code generators and doesn't support manipulation of existing artefacts:


工具的初始输入是
, zeroth
轮;因此,尝试创建
a源或类文件对应
到其中一个输入将导致
a FilerException。

The initial inputs to the tool are considered to be created by the zeroth round; therefore, attempting to create a source or class file corresponding to one of those inputs will result in a FilerException.

API推荐的Decorator模式不是一个选项。

The Decorator pattern recommended by the API is not an option.

我可以看到如何使用运行时代理/工具执行这个步骤,但这是比手动构建步骤更糟糕的选项,因为它将需要任何人甚至外部接触的API来以非显而易见的方式配置其JVM。

I can see how to perform the step with a runtime agent/instrumentation, but this is a worse option than a manual build step as it would require anyone even peripherally touched by the API to configure their JVMs in a non-obvious manner.

有办法插入或包装编译器工具作为由 javac 调用?

Is there a way to plug into or wrap the compiler tool as invoked by javac? Has anyone successfully subverted the annotation processors to manipulate bytecode, no matter what the doc says?

推荐答案

Groovy编译器是唯一的字节码编译器,允许钩入编译过程(例如:生成字节码以支持Singleton模式

The Groovy compiler is the only bytecode compiler which allows to hook into the compilation process (example: Generate bytecode to support the Singleton pattern)

注释处理API不意味着更改代码。正如你已经发现的,你可以做的是安装一个类加载器,在运行时检查字节码并操作它。这是braindead,但它的工作。这遵循一般的我们害怕一个开发人员可以尝试一些愚蠢的主题,你会发现在Java。没有办法扩展javac。

The Annotation Processing API is not meant to change the code. As you have already found out, all you can do is install a classloader, examine the bytecode at runtime and manipulate it. It's braindead but it works. This follows the general "we're afraid that a developer could try something stupid" theme which you will find throughout Java. There is no way to extend javac. The relevant classes are either private, final or will change with the next version of Java.

另一个选择是编写注释的Java,例如你编写一个类ExampleTpl。 java。然后,使用预编译器扩展该文件中的注释以获取Example.java。在其余代码中,您使用示例并忽略 ExampleTpl

Another option is to write annotated Java, for example you write a class "ExampleTpl.java". Then, you use a precompiler which expands the annotations in that file to get "Example.java". In the rest of the code, you use Example and ignore ExampleTpl.

对于Eclipse,有一个错误报告自动执行此步骤。我不知道在这方面的任何其他工作。

For Eclipse, there is a bug report to automate this step. I'm not aware of any other work in this area.

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

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