Question

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?

Was it helpful?

Solution

Apache BCEL allows you to delete instructions:

Deletion of instructions is also very straightforward; all instruction handles and the contained instructions within a given range are removed from the instruction list and disposed. The delete() method may however throw a TargetLostException when there are instruction targeters still referencing one of the deleted instructions. The user is forced to handle such exceptions in a try-catch clause and redirect these references elsewhere.

You can find an example in the manual as well.

OTHER TIPS

From the javassist tutorial :

Javassist does not allow to remove a method or field, but it allows to change the name. So if a method is not necessary any more, it should be renamed and changed to be a private method by calling setName() and setModifiers() declared in CtMethod.

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