lundi 18 juin 2018

Log any method call of an instance

Is it possible to log any method call of a class instance? I have the following instance:

InputStream serverInputStream = this.serverProcess.getInputStream();

I would like to see at a given time during runtime what method has been called in serverInputStream. I believe the solution for that is reflection, specifically a proxy. I already tried to make this example work, but yet unable to make it running.

I thought of a code similar to this:

MyInterceptor myInterceptor = new MyInterceptor(this.serverProcess.getInputStream());
InputStream serverInputStream = myInterceptor.getInterceptedInstance();

serverInputStream.methodOne();
serverInputStream.methodTwo();
serverInputStream.methodThree();

myInterceptor.printIntercepts();

Having a result similar to this:

1. InputStream.InputStream();
2. InputStream.methodOne();
3. InputStream.methodTwo();
4. InputStream.methodThree();

Is that possible?





Aucun commentaire:

Enregistrer un commentaire