Question

I am wondering whether it is possible to combine multiple java method bytecode sequences into one method. Assume we have a method A, which invokes another two method B1, and B2.

A bytecode sequences: 

.....
invokevirtual B1 
iload ..
....
invokevirtual B2 
.... 

At runtime, B1 and B2 may be close correlated and we want to combine B1 bytecodes and B2 bytecodes, together with bytecodes between "invokevirtul B1" and "invokevirtual B2", into one method.

I am not sure whether it is possible to implement, I would appreciate if some clue can be provided. THanks.

Was it helpful?

Solution

Yes, it is possible, with a few minor restrictions. The main restriction is that a single method's bytecode is limited to 65535 bytes, but you're unlikely to run into this restriction in practice. The number of exception handlers, local variable slots, and operand stack size in a single method are also limited, though these are even less likely to be reached.

OTHER TIPS

See ASM bytecode manipylation framework code example from my paper "Using ASM framework to implement common bytecode transformation patterns" [1].

[1] http://asm.ow2.org/current/asm-transformations.pdf

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top