"Gilles A." <Gi********************@bpr-cso.com> wrote in message news:<5W*****************@charlie.risq.qc.ca>...
Hi all,
Does anyone know how to get the class.method name of the calling method?
Example
public class A
{
public void m()
{
new B().method();
}
}
public class B
{
public void method()
{
// Here I need to know who called me.
// I need to print "A.m()" in a stream.
}
}
I tought JDK 1.4 was offering that functionnality but I can't find it
anywhere.
Does anyone have an idea?
Thanks in advance,
Gilles
Hi,
The best thing you can do is avoid that functionality, it's just not
meant to be in Java.
If you, however, really really need it, you can use a dirty hack like
throwing an exception where you need to get the caller name, catching
it immediately, and then use Throwable.getStackTrace() to get your
callers from your catched exception.
Bear in mind that using exceptions to handle non-exceptional
conditions is a bad practice, and shall be discouraged!
Regards,
Andrey Andreev
Software developer