Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ASM Visiting a method multiple times

Tags:

java

assembly

I am working through many classes and transforming them. There are some classes that I do not have enough information for when I first visit them, and as such I need to revisit them. Since I do not know at the time of the first pass if I need to revisit, I copy the complete class with the first pass of modifications.

What I want to know is if it is possible to revisit a method and overwrite the method in the ClassWriter

byte[] b...
ClassReader cr = new ClassReader(b);
ClassWriter cw = new ClassWriter(read,0);
ClassAdapter ca = new ClassAdapter(cw);//First pass
cr.accept(ca,0);
ClassAdapter ca2 = new ClassAdapter(cw);//Second Pass
cr.accept(ca2,0);

The result of this code will give me verification errors due to duplicate field&method declarations.

like image 935
howso57 Avatar asked Mar 09 '26 10:03

howso57


1 Answers

You would have to initialise a new ClassReader using the new bytecode read from cw.toByteArray(). From there, you would repeat the other steps (new ClassWriter, new ClassAdapter etc.)

like image 61
someguy Avatar answered Mar 11 '26 00:03

someguy