ugly hack for finding the caller of a method
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 2k
loc 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(LoginAction.java:3689) called from: com.spaghetti.backend.LaunchMissilesAction.execute(LaunchMissilesAction.java:1062) called from: com.spaghetti.StringHelper.getTime(StringHelper.java:501) called from: com.spaghetti.TimeUtils.capitaliseString(TimeUtils.java:1723)
Good luck, and don’t forget to remove the print statement before committing. You don’t want the next maintainers to freak out ;)
October 23rd, 2010 at 0:12
watch this hack
http://www.javaspecialists.eu/archive/Issue168.html