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!

name of function 1

Status
Not open for further replies.

mmfried

Programmer
Sep 17, 2003
33
IL
Hi all,

Is there anyway that I could get a methods name automatically, In a class it's possible through this, is there anything similar in methods.

thanks

mmfried
 
thanks,

but is there an option of getting the index in this array to the spesific function that I am in.

mmfried
 
I want to design an automatic trace an therefore to get the name of the method, exception will get me out of the method and therefore won't be useful.
If there's a reference to a function with an option of getMethodName or smth, it will be very helpful
 
I assume you are using Java 2 1.4.0 or later.

Looking at the thread I gather you want to know the name of the method you are currently in. You can do this using an exception:

Code:
public class WhereAmI { 
	
	/** tells me which method called me */
	public static String tellMe() {
		Exception e = new Exception();
		StackTraceElement all[] = e.getStackTrace();
		if (all.length < 2) // WFT am I then, should be at least 2! 
			return "";
		return all[1].getMethodName();
	}
	
}

And then to complete the proof off concept, here is a very confused class that has methods that need to know who they are ;-)

Code:
public class Confused { 

	public static void main(String args[]) {
		Confused c = new Confused();
		c.someMethod();
		c.otherMethod();
	}
	
	public void someMethod() {
		System.out.println(WhereAmI.tellMe());
	}
	
	public void otherMethod() {
		System.out.println(WhereAmI.tellMe());
	}

}

Hope this helps!
 
I would bear in mind that any type of program that relys on know what method it is in is very bad programming style.

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj, is there any other idea how to do an automatic trace othewise? (automatic name of class andmethods), the idea is to enable a "print" like class that there's no need to deel with the names of the methods & classes.
 
legrang's code is a perfectly valid way of determining a stack trace, but I was just saying that if your code relys on some condition that is determined by what method you are in, then your program design maybe needs rethinking.

--------------------------------------------------
Free Database Connection Pooling Software
 
Sedj - I think this would be only for debug tracing & logging.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
As I said above, for determining a stack trace, then no worries ...

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj -
I'm still learning Java (again) -- is there some way to generate your own stack frame, which might tell you the method name you're in?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
It seems that's there's no other way for v1.4.2

In v1.5 there's a build in sollution for this (if I'm not mistaken, smth like getMethodName)

mmfried
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top