Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing the Current Method Name

 
Michael Arnett
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to print the current method name to a log file for debugging purposes and was wondering how exactly to do this. Essentially, I need a method that will return the current Method's name as a String.
Thanks in advance for any ideas,
-MLA
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael
You want to be able to do something like this in every method:
...
printMethodName(...);
...
And the printMethodName method would print the method that called it to the log file? If you do that then the current method would be the printMethodName mehtod not the method that called it. From what I can think of, since you have to add a line of code to each of your methods anyway, just have it send its own name to the print method:
in method myMethod
...
printMethodName("myMethod");
...
Or you could create your own exception class and in each of your methods throw that exception, catch it and then just print the stack trace.
Someone else might know of a more elegant way to do this though...
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
You want to look at the Class class (hey is there an echo in here?) Also in the java.lang.reflect package there are various classes which encapsulate Fields, Methods, etc. In the Class class (there's that echo again!) there are various methods to return objects of the aforementioned classes for example:

Then in the Method class there are methods that return information about a method for example:

And of course you can get the Class of any Object with a call to getObject like this:

Hope this helps
Michael Morris
SCJP2
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be I'm trying to drive a nail with a sledge hammer here. Sorry if my post is overkill
Michael Morris
SCJP
 
Michael Arnett
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick responses. What I was hoping to have was a single line of code at the top of each method that would look something like:
logPrint(getMethodName(),getTimeStamp());
where getTimeStamp returns a String that reflects the current time (that part is easy) and getMethodName() returns a String that represents the current Method name. Of course, I can just replace that method call with a hard coded "myMethod", but that would make maintenance a bit of a headache. Is there a way to print the current method (only) from the stack trace (similarly to the Exception Handling solution suggested?) I will play with that some and let all know if I find anything useful, but please keep the great ideas and suggestions flowing
Thanks again,
-MLA
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Michael,
Gee it looks like my post wasn't overkill after all
As I said earlier look at the javadocs for the java.lang.reflect package and the Class class (echo again?) That should get you headed in the right dircetion. Be warned that the coding can get pretty tough though
Hope this helps
Michael Morris
SCJP2
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
I've fought with a similar concept for some time now. Unfortunately, I don't think reflection is going to be that helpful for you. Indeed, reflection can tell you all about a class and its members, but I don't think there's a way, through reflection, to get the name of the method that you're currently in. If someone knows of a way to get this, I'd love to hear it, but I don't know of one.
I really wish I could give you the answer but, even though I've worked on a similar problem for some time, I've never come up with a really elegant solution. Generally, whatever solution I come up with doesn't provide any extra benefit over doing this:

I realize that this can be a maintenance headache, but I haven't found a nice way to handle it.
Corey
 
Russ Lear
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little new around here, so some allowance ought to be made...
You might try a utility method that does something along the lines of:
Throwable t = new Throwable();
// t = t.fillInStackTrace(); // ???
StackTraceElement ste[] = t.getStackTrace();
StringBuffer sb = new StringBuffer();
sb.append(ste[1].getClassName());
sb.append('.');
sb.append(std[1].getMethodName());
return sb.toString();
Creating a Throwable isn't too neat but it does get you the stack trace.
(depending on where/how you call it, the index might be 0, 1, ...).
Anyway, that's my thoughts.
Russ.
 
Michael Arnett
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for all the great responses. I am still working at a solution, but have not found one yet. Russ, your solution is one I would pursue, but I do not believe that Throwable elements have a getStackTrace method in JDK 1.3x and unfortunately moving to 1.4 is not currently a possible solution. Is there a parallel solution for 1.3?
Thanks again!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
Throwable definitely has three methods named printStackTrace() in 1.3. The ones you would be interested in are printStackTrace(PrintStream s) and printStackTrace(PrintWriter w).
I still think reflection can be used here. Unfortunately, I can't yet prove it But if I do figure a clean solution, I'll certainly let you know.
Michael Morris
SCJP2
 
Russ Lear
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about my reference to 1.4 APIs - I should have checked the "Since:" line in the javadocs.
I was curious how the jakarta log4j does this, so I looked in the source. They seem to generate a throwable, use a StringWriter to capture the printStackTrace, and then inspecting the resulting string (using indexOf / lastIndexOf). Not hugely elegant, but I suspect the best they could do with the APIs available.
See the source in the download file at http://jakarta.apache.org/log4j/docs/download.html
Good luck!
Russ.
 
Keivin Xu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, log4j can do it. but it is a very expensive operation, if there are too many log messages, it can reduce your performance greatly. i remember there was a table (can't find it now) comparing the different parameters used in log4j. that table showed that to obtain the method name is very slow.
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
orax
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic