Is there a Java library that generates class files from ast?

This page describes how to generate code using the code generator in javac, because I can build an ast (using a separate parser I wrote) This technique involves editing the source code of javac to basically bypass the Java parser so that you can provide your own AST to the code generator It works, but I want to do it in a slightly cleaner way I want to include the javac code generation part in my project as a library project, so I can use it to generate code without bringing the rest of the javac source code

Is there a way to do this with javac, or is there a better library?

In addition, please feel free to change the title of the question I can't think of a better one, but it's a little vague If you suggest editing a better title, I will accept it

Solution

I think you may be interested in a Java library like BCEL (bytecode Engineering Library),

When I was in the compiler construction course, I played it again. Basically, it has a good wrapper for generating constant pool, inserting an instruction named bytecode into a method, and then after completion, you can load the class, use the custom class loader at runtime, or write it to the file in the normal way

Using BCEL, it should be easier to go from syntax tree to Java bytecode. Although it is a little boring, in some cases, you may only want to use BCEL to generate non generated bytecode instead of building tree

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>