Hooking an existing method in Java

Nowayz picture Nowayz · May 3, 2011 · Viewed 16.7k times · Source

I want to hook the method System.out.print in Java and have the ability to read/change the variables used in the method before the part of the method is called that actually adds the string to whatever the output stream is.

In C++ I would just detour the function, or set an int3 instruction so I could access the registers but in java I have no idea how to accomplish something similar.

Answer

sbridges picture sbridges · May 3, 2011

You can rewrite the byte code of the methods, and in the process capture/change the local variables. It is not trivial. See some notes here.

Maybe what you really want is a java debugger? You can connect a debugger to a remote process, add a breakpoint, and capture/change the local variables pretty easily using eclipse.

What is the real problem you are trying to solve?