11

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?

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 2
    I am just curious. Why do you want to do this? Would not be easier to decompile the classes, modify them and compile them again? – Luixv Feb 10 '12 at 12:53
  • @Luixv: I want the manipulation process to happen automatically at runtime - modifying the actual source code manually is not an option, because the transformations are not fixed. – thkala Feb 10 '12 at 12:57
  • If you place the class in a `jar`, the redundant `nop` will be compressed quite well and the compressed size won't be much bigger. – Peter Lawrey Feb 10 '12 at 13:05
  • I have used ObjectWeb's ASM. I believe it would do the job. While very fast and powerful, its not the simplest library to use as its relatively low level. – Peter Lawrey Feb 10 '12 at 13:12
  • @PeterLawrey: I am considering using ASM, but at this time the steep learning curve and its inherent complexity seem to outweigh the potential benefits for this particular project... – thkala Feb 10 '12 at 13:48
  • if there are calls to method you want to delete, then you may end up modifying full class and probably other classes (if it is public). could be very interesting to see the answers +1 – Jayan Feb 10 '12 at 13:50

2 Answers2

3

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.

Go Dan
  • 15,194
  • 6
  • 41
  • 65
  • +1 I haven't looked at BCEL yet, but it does sound promising. I'll see how easy it would be to incorporate in my existing code... – thkala Feb 10 '12 at 13:53
0

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.

user1205938
  • 269
  • 1
  • 2
  • I do not want to remove a method - I want to remove instructions from *within* a method... – thkala Feb 13 '12 at 07:02
  • Oops, my bad! For completeness' sake though, the tutorial also states : 'To remove a field or a method, call removeField() or removeMethod() in CtClass.' :S – user1205938 Feb 13 '12 at 22:45