Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating .class file

Tags:

java

bytecode

I have defined a grammar through which I produce a series of abstract syntax trees of sorts. For example

x = 7
println + * x 2 5

becomes

    (assign)
    /      \
   x        7

    (println)
        |
       (+)
      /   \
    (*)    5
   /   \
  x     2

These trees are made from various Node classes representing values and operations. Now, these trees are easy to interpret but my goal is to generate Java bytecode representing these processes. My question is, what would be the best way to approach that? Should I just literally write various bytecode instructions to a .class file, or is there some library or interface that can help with this sort of thing?

like image 245
arshajii Avatar asked May 03 '26 14:05

arshajii


1 Answers

The answer is yes. ASM and BCEL are two Java libraries designed to assist with runtime generation of classfiles.

like image 144
Antimony Avatar answered May 06 '26 05:05

Antimony