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

后端 未结 2 790
孤独总比滥情好
孤独总比滥情好 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.

提交回复
热议问题