I have been using Javassist to dynamically manipulate classes as they are loaded. While adding code to a method is relatively easy using Javassist, I have not been able to find a way to remove code.
At this time I am simulating the removal of the code by using nop instructions to replace the targeted opcodes and any parameters. However, I consider this to be mostly a hack:
Each opcode has to be treated separately, since the byte length of the parameters differs. In some cases I also need to choose between nop and pop, depending on whether the removed opcode affects the stack or not. This kind of manipulation is starting to get tedious - and the code that does it is becoming accordingly convoluted. So, naturally, I am hoping for an existing solution.
The final result is filled with nop instructions. While the JVM should optimize those out with no performance impact, the resulting bytecode is still quite inelegant and bigger than it should be. This is more of an issue of aesthetics, but it is still something to consider.
Unfortunately, merely shifting parts of the bytecode array to close the gap is not enough - any references to the moved code (e.g. branch instruction indexes) should be updated as well.
Is it possible to remove instructions using Javassist? Alternatively, is there a bytecode manipulation library that would allow me to do that easily, without having to essentially parse the bytecode myself?