Is there a Java library to generate class files from an AST?

后端 未结 2 789
孤独总比滥情好
孤独总比滥情好 2021-02-14 07:47

This page describes how I can use the code generator in javac to generate code given that I can build an AST (using a separate parser which I wrote). The technique involves edit

相关标签:
2条回答
  • 2021-02-14 08:11

    I think what you might be interested in is a java library like BCEL(ByteCode Engineering Library)

    I played around with it back when I took a class on compiler construction, basically, it has a nice wrapper for generating the constant pool, inserting named bytecode instructions into a method and whatnot, then when you are done, you can either load the class at runtime with a custom classloader, or write it out to a file in the normal way.

    With BCEL, it should be relatively easy to go from the syntax tree to the java bytecodes, albeit a bit tedious, but you may want to just use BCEL to generate the raw bytecode without building the tree as well in some cases.

    0 讨论(0)
  • 2021-02-14 08:12

    Another cool framework is ASM, a bytecode analysis and manipulation framework.

    In case you do not want to use a framework, as of now (2014), it is not possible to generate bytecode from a tree using the arbitrary representations of com.sun.source.tree.* as said here.

    0 讨论(0)
提交回复
热议问题