How to obtain class and method name (again)

C

coltrane

I have seen this question asked many times, but I have yet to see an
answer.

Is it possible to get the class and or method name in a java app?

thank you for your help

john
 
K

kaeli

I have seen this question asked many times, but I have yet to see an
answer.

Is it possible to get the class and or method name in a java app?

Of what?
Class.forName might help you.


--
 
A

Andrew McDonagh

kaeli said:
Of what?
Class.forName might help you.


Some example code, which will work no matter what type of object
'anObject' really is - the example uses String as the type for 'anObject'.


String anObject = "someValue";

Class theObjectsClass = anObject.getClass();


String className = theObjectsClass.getName()

Method[] methodsOftheObjectsClass = theObjectsClass.getDeclaredMethods();



For the remaining details of what can be done with theObjectsClass
object, take a look at the API for the java.lang.Class class.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html

HTH

Andrew
 
V

Virgil Green

coltrane said:
I have seen this question asked many times, but I have yet to see an
answer.

Is it possible to get the class and or method name in a java app?

thank you for your help

john

Here's one way for each. Don't know if it's the only way.

class name:
String name = object.getClass().getName()

method name (as of 1.4):
Exception e = new Exception();
String name = e.fillInStackTrace().getStackTrace()[0].getMethodName();
 
C

coltrane

thanks,

Is there a way to determine the name of the current method .

thanks again

john
 
C

christopher

I have never heard of a java concept called "the current method", so I
can't imagine why such a thing would have a name.
 
T

Tor Iver Wilhelmsen

coltrane said:
Is there a way to determine the name of the current method .

Yes, at "write time". Just look where the method starts.

Why would you need to know it dynamically at runtime?
 
K

kaeli

I have never heard of a java concept called "the current method", so I
can't imagine why such a thing would have a name.

The method currently executing.
I have wanted something like this for making debug tracing methods, myself
(prior to the lovely assert added in 1.4).
It would be easier than having to hard-code the method name.
Copy...paste...change method name in debug statement...repeat 50 times?!?!
LOL

--
 
T

Tor Iver Wilhelmsen

kaeli said:
I have wanted something like this for making debug tracing methods, myself
(prior to the lovely assert added in 1.4).

Stack trace - including current method - is in exceptions/Throwable.
That's the reasonable place to go look for it.
 
K

kaeli

Stack trace - including current method - is in exceptions/Throwable.
That's the reasonable place to go look for it.

Not when you're doing things like

public String myMethod()
{
System.out.println("MyClass.myMethod(): Starting.");
...
// some stuff I think works
System.out.println("MyClass.myMethod(): Point 1.");
// some stuff I wonder if it works
System.out.println("MyClass.myMethod(): Point 2.");
System.out.println("MyClass.myMethod(): Done.");
}

(except I write them to a file and include variable values and such since the
code is running on another machine)

--
 
V

Virgil Green

kaeli said:
Not when you're doing things like

public String myMethod()
{
System.out.println("MyClass.myMethod(): Starting.");
...
// some stuff I think works
System.out.println("MyClass.myMethod(): Point 1.");
// some stuff I wonder if it works
System.out.println("MyClass.myMethod(): Point 2.");
System.out.println("MyClass.myMethod(): Done.");
}

(except I write them to a file and include variable values and such
since the code is running on another machine)

--

Why would it be unreasonable in that case?
 
A

Andrew McDonagh

kaeli said:
Not when you're doing things like

public String myMethod()
{
System.out.println("MyClass.myMethod(): Starting.");
...
// some stuff I think works
System.out.println("MyClass.myMethod(): Point 1.");
// some stuff I wonder if it works
System.out.println("MyClass.myMethod(): Point 2.");
System.out.println("MyClass.myMethod(): Done.");
}

(except I write them to a file and include variable values and such since the
code is running on another machine)

If this is the kind of thing you are actually trying to do, then I'd
suggest using the logging API either Java's inbuilt Logger or log4J's
logger. Either does exactly what you want, but in a much, much better way.

Also, if you want to get a stack trace, then you can simply do
Thread.dumpStack(); rather than using an exception object.

see...

http://java.sun.com/j2se/1.3/docs/api/java/lang/Thread.html#dumpStack()
 
Joined
Jun 12, 2007
Messages
1
Reaction score
0
Thread.dumpStack simply prints the entire stack trace. If you want to access a StackTraceElement, do so by:

StackTraceElement ste = new Exception().getStackTrace()[0];

The zero indicates that we want the top of the stack trace, which contains context about current execution. Once you have this information, you can ask about class name, method name, etc. Hint: put this in a utility method so that it can be reused. To access the caller's StackTraceElement, just change the above to:

StackTraceElement ste = new Exception().getStackTrace()[1];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top