Wednesday, April 2, 2014

Adding sysouts to external classes in Eclipse

Yesterday, I was debugging through some unpleasant code (man, I was like 40 minutes deep into debugging some recursive reflection) and was getting really irritated with tweaking my conditional breakpoint so I could see the value of something right before it blew up.  The problem, as suggested by the title of this post, was that the code wasn't mine - it was a 3rd party jar (the struts2-json-plugin jar, not to name names).
I wanted to add a sysout, but I didn't have access to change the code.  Finally I googled, and in December 2011, Jonas Helming answered my problem.

Put a sysout in a conditional breakpoint.  And then return false, so the breakpoint never breaks.


Also of course, instead of return false, you can do whatever normal conditional breakpointing you'd like.

Even cooler, through this I discovered the code was blowing up when trying to access a non-public field.  So while later today I will come up with a test case to demonstrate the problem and find a real solution, etc. etc, in the meantime, I added field.setAccessible(true);.  Boom, reflection issue resolved.  Printout, resolved.  As long as I run my code in debug for the time being, it works with no action on my part.



This kind of reminds me of AOP, in the sense that code's getting executed and you don't see where from.  But it fits - debug statements are an aspect of programming that shouldn't be in your code.  At least because this is in your IDE, it guarantees the magic doesn't end up in production code.

Maybe this isn't new to you.  I mean, I learned it from another blog that says it got it from other blogs, but I wanted to share my new trick.  Hopefully it's helpful to you.

No comments:

Post a Comment