time4tea
asked on
How to determine current procedure/class name
In perl i can user the caller() function to give me the name of the current (or even better the callers) class and procedure name, in gcc theres __PRETTY_FUNCTION__, is there any way of doing this in java.
e.g. I want to do this
String x = "I got called by class " + getcallerclass() + " method " + getcallermethod()
e.g. I want to do this
String x = "I got called by class " + getcallerclass() + " method " + getcallermethod()
ASKER
This is quite a nice solution.. however would be (I guess) quite slow...especially if I'm gonna call the function lots of times... I'm looking for something than can tell me where my debug statements are coming from...
However, I'll check this out - So thanks for the pointer!
I'd be interested if there are any other possible ideas before closing this one....
However, I'll check this out - So thanks for the pointer!
I'd be interested if there are any other possible ideas before closing this one....
The easiest way to do this is to just pass in the callers class as a parameter to the method:
//call method
c.calling("bbb",this.class );
then at the other end you will receive a class object as a parameter, so define your method as:
public class c {
calling (String s, Class c) {
String x = "I got called by class " + c.toString();
}
this.class gets you the class object for the class you are currently in. It is returned as a class object and you can use as you would any other object.
//call method
c.calling("bbb",this.class
then at the other end you will receive a class object as a parameter, so define your method as:
public class c {
calling (String s, Class c) {
String x = "I got called by class " + c.toString();
}
this.class gets you the class object for the class you are currently in. It is returned as a class object and you can use as you would any other object.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hmmm.... if u're gonna do that y dun u just do something like
public static void a(Object b, String n) {
System.out.println(b.getCl ass(), n);
}
and instead of passing in the method name and param list, just pass in the whole thing as a string? seems alot more convenient...
ie.
test.a(this, "b()");
public static void a(Object b, String n) {
System.out.println(b.getCl
}
and instead of passing in the method name and param list, just pass in the whole thing as a string? seems alot more convenient...
ie.
test.a(this, "b()");
Difficult to judge the points winner
there is support for such debugging in JDI (Java Debug Interface)
http://java.sun.com/products/jpda/doc/jdi/overview-summary.html
look at com.sun.jdi.*
it provides api to access the stack frames
http://java.sun.com/products/jpda/doc/jdi/overview-summary.html
look at com.sun.jdi.*
it provides api to access the stack frames
ASKER
Theres a lot of info here... so thanks to you all.... !
refer to
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10222663
look at the last comment made so far (by heyhey_)
special emphasis goes to the fact that the stack trace may vary between jvms...