Is it possible to spy on the method calls being made to instances of a Java class. I know I could achieve this using java.lang.reflect.InvocationHandler. I’m wondering if there are other easier ways which don’t involve modifying the source code of the classes?
EDIT:
For example java.util.HashMap. The source code for this class is not available – at least for me it’s not when running under Oracle’s JDK. How would I intercept the calls made to the put(…) method. I would like to inspect the parameters in the call.
You might do this with Aspect Oriented Programming.
You’re just have to declare pointcuts of your code, which you’re want to ‘spy’. In a few words – you’re might declare annotations, put them above methods signatures, and all annotated methods might been modified by compiler in a compile time (or been modified in time of loading bytecode to jvm) without needs to modify their source code.
AspectJ is useful for this.