堵到Java编译 [英] Plugging in to Java compilers

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

问题描述

我有一个编译后的步骤,操纵生成的类的Java字节code。我想使生活尽可能无痛图书馆消费者,所以我在寻找方法,我可以让这个过程自动化,(如果可能)编译器无关。

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的支持)。不幸的是,这是针对code发电机的不支持现有的文物操纵的:

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:

的初始输入到工具是
  认为是由零来创建
  回合;因此,试图创建
  相应的源文件或类文件
  到的那些输入将导致一个
  一个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的?有没有人成功地颠覆了注解处理器来处理字节code,不管医生说什么?

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编译器是唯一一个字节code编译器,它允许挂接到编译过程(例如:<一href=\"http://svn.$c$chaus.org/groovy/trunk/groovy/groovy-core/src/main/org/$c$chaus/groovy/transform/SingletonASTTransformation.java\"相对=nofollow>生成字节code:支持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并不意味着改变code。正如你已经发现了,你所能做的就是安装一个类加载器,检查在运行时字节code和操纵它。这是新空房禁地,但它的工作原理。在此之前,一般的我们害怕,开发者可以尝试一些愚蠢的事的主题,你会发现整个Java的。有没有办法来扩展的javac。相关类是私人,最终还是会与Java的下一个版本改变。

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。然后,您可以使用precompiler它扩展该文件中的注释,以获得Example.java。在code的其余部分,可以使用示例,而忽略 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,有一个 bug报告这个步骤自动化 。我不知道这方面的任何其他的工作。

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天全站免登陆