Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get the method name inside the same method?

Status
Not open for further replies.

nabla

Programmer
Aug 15, 2000
11
US
hi<br><br>i'm trying to write a log class so all the class and methods can log their names in a file. it's easy to do so for the class name, but there is no trivial way to return the method name of a method.<br><br>java.lang.reflect provides a feature to show all the methods available inside a class, but i can't know the method name inside the method itself.<br><br>could you give me some clues on how to do that?<br><br>thanx a lot
 
if you have a Method object called myMethod, just return myMethod.getName(). Is that what you want? If not, please let me know. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
hi,<br><br>thanx for replying<br><br>my problem is that the methods in a certain class are not Method objects. and i can't use the getName() method to get the name.<br><br>for example:<br><br>public class Ex<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;public void myMethod()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//how can i get the name of this method from here?<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br>again, thank u for replying
 
I don't understand why you'd need to do that. If you're in myMethod(), you know at compile-time what method you're in. Reflection is more for discovering at run-time what you can not discover at compile-time.<br><br>public class Ex {<br>&nbsp;&nbsp;public void myMethod() {<br>&nbsp;&nbsp;&nbsp;&nbsp;String methodName = &quot;myMethod&quot;;<br>&nbsp;&nbsp;}<br>}<br><br>why doesn't that work?<br><br>Reflection is probably not what you want in this case. Or at least I don't see why you'd need it. If you wanted to print all public methods of class Ex, you could do this:<br><br>Class c = Ex.class;<br>Method[] methods = c.getMethods();<br>for (int i=0; i&lt;methods.length; i++)<br>&nbsp;&nbsp;System.out.println(methods<i>.getName());<br><br>Is that what you want? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
i'm writting a log class so all the methods will log their names in a file. <br><br>public class Ex {<br>&nbsp;&nbsp;public void myMethod() {<br>&nbsp;&nbsp;&nbsp;&nbsp;String methodName = &quot;myMethod&quot;;<br>&nbsp;&nbsp;}<br>}<br><br>will certainly do the job. <br>but in this case all the methods need to declare a string containing their names. there could be thousands of methods. the method name in the string could be misspelled and it's not easy to maintain i suppose.<br><br>that's why i'm trying to use an automatic way for getting the method names such as getClass() for the classname. <br><br>thank you
 
well then why not use Class.getMethods()? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Class.getMethods() returns all the methods in that class in an array of Method objects. i know the calling method is one element of it but i still don't know which element it is exactly.<br><br>
 
Ok, I think I understand. The reason you can get an object's class is because you're dealing on an object level, in an object-oriented language. As the method you're running is only part of an object, there's no way of saying that. Plus, Reflection (which can't really give you what you want) uses up a lot of overhead.<br><br>Maybe if I understood exactly how this was interfacing with the log, I might be able to help more. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
i just want to log <br>time:classname:methodname<br>in a file for every method that runs.<br><br>time and classname are easy to get, but not the method name<br><br>actually printStackTrace knows which method get executed, probably i need to query the Virtual Machine.<br>but i have no idea how to do it.<br>
 
Is this for servlets, or what? What kind of classes are you writing? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
i'm writing a basic class that has a method called logDebug() to write information to a log file. all other classes will extend this behaviour.
it's not servlet.
i think there's no cheap way to get the method name inside the method itself, and i'll declare a string storing the method name.
anyway thanx a lot for ur help and interest in my question.
 
imotic: you are thick headed. nabla was pretty clear in asking for what was needed.

nabla: I know what you are talking about. There are a few ways to do it. You don't need reflection however. You can get the stacktrace and parse it out. There are two ways to get the stack trace. 1) Throw an exception and catch it right away. Get the stacktrace and figure out the method. 2) Second way to get trace including current method is do Thread.dumpStack(). Unlike printStackTrace(out), Thread.dumpStack() won't let you send the output to a writer, so the only way to use the second method is to redirect System.err right before Thread.dumpStack() and restoring System.err right after. In multithreaded apps, make sure you are careful in locking the right parts in case some other thread tries to write to System.err while you have redirected it.
Anyway, the first method is the best way to do it but it is not the only way to do it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top