Archive for the ‘hacks’ Category

ugly hack for finding the caller of a method

Friday, October 22nd, 2010

Imagine the following scenario:

  • you are trying to trace a bug in an old, ugly and improperly constructed legacy application (you know… the application with the 2kloc classes containing 14 nested ifs)
  • access to a debugger is not possible but you can recompile one or more classes and restart the server
  • you need to find who is calling a particular method, but don’t want to insert one thousand print messages in the one thousand places where the method is being called from
  • doing it with AOP gives you a headache

If all the above are true, then maybe the only hope is placing the following line in the beginning of the method in question:

System.out.println("called from: " + 
            new RuntimeException().getStackTrace()[1]);

Yes, this is ugly but it does the job. As the application is running it will print traces such as:

called from: com.spaghetti.backend.LoginAction.execute(
called from: com.spaghetti.backend.LaunchMissilesAction.execute(
called from: com.spaghetti.StringHelper.getTime(
called from: com.spaghetti.TimeUtils.capitaliseString(

Good luck, and don’t forget to remove the print statement before committing. You don’t want the next maintainers to freak out ;)

producing a beep in a Windows Shell

Monday, October 18th, 2010

You can do it by pressing CTRL+G or ALT+7 (numeric keyboard) in a shell.
If you need it in a batch file then do @echo ^G.

Some cases where you’d need this:

  1. You are inside a messy server room with tons of windows desktop boxes (sad) and a KVM switch for interfacing. The boxes have no IP or name stickers on them (sad) and you need to detect which one is the box that the KVM screen and keyboard currently points to. Access to the cables gordian knot is obviously a time consuming task.
  2. You have a batch file you need to execute and want a beep notification when it ends because you are working on something else at the same time.