By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,884 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,884 IT Pros & Developers. It's quick & easy.

Getting the name of the calling method

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"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
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.