哪个库/程序可用于生成Java字节码? [英] Which library/program can be used to generate Java-bytecode?

查看:80
本文介绍了哪个库/程序可用于生成Java字节码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 BCEL ,但是该项目似乎已经死了,因为它没有发布两年Java世界在继续发展.例如,JDK 1.6具有新的类文件格式.

I know about BCEL, but this project seems to be dead, as it had no releases for two years. And the Java-world moves on. For example JDK 1.6 has a new class-file-format.

那么什么库可用于为JVM创建字节码.如果没有库,那么如果我可以详细操作生成的代码(例如字节码汇编器),则程序也可以.

So what library can be used to create bytecode for the JVM. If no library, a program is ok too, if I can manipulate the generated code in detail, for example a bytecode-assembler.

您可以推荐哪种软件?容易使用吗?有很好的例子/教程吗?

Which software can you recommend? Is it easy too use? has good examples/tutorials?

对于所有询问:是的,javac很好.但是,要在运行时生成某些类,直接使用字节码的路径会更干净.

For all asking: Yes, the javac is fine. But for generating some classes at runtime, a path directly to bytecode would be cleaner.

推荐答案

ASM

http://asm.objectweb.org/

它比BCEL快得多,并且支持泛型和注释. 关于其体系结构的一点是:为了确保高性能,ASM是围绕引发事件的解析器构建的(与BCEL解析器构建数据结构的方式相反).这有点类似于SAX和DOM解析器之间的区别.需要一些练习才能习惯这种思维.

It is much faster than BCEL and supports generics and annotations. One point about its architecture: in order to ensure high performance ASM is built around a parser that throws events (in contrast to BCEL where the parser builds a data structure). This is somewhat similar to the difference between SAX and DOM parsers. It takes some practice to get used to this kind of thinking.

编辑(遵循McDowell的评论):确实,访问者在ASM中得到了广泛使用,但它比普通访问者更多:解析器懒惰地构建了访问的数据结构,因此,如果您对类文件的某些部分不感兴趣(例如,您想知道方法的名称,但不必关心它们的主体),则可以从visitMethod()方法返回null.这将使解析器跳过方法主体部分,从而防止完全描述该方法的对象网的(昂贵)构造.

EDIT (Following McDowell's comment): Indeed visitors are heavily used in ASM, but it's more than plain visitors: the visited data structure is lazily built by the parser, so if you're not interested in certain parts of the classfile (for example, you want to know the names of the methods but you don't care about their body), you can return a null from the visitMethod() method. This will make the parser skip the method body sections thereby preventing the (expensive) construction of the net of objects fully describing the method.

这篇关于哪个库/程序可用于生成Java字节码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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