The Artima Developer Community
Sponsored Link

Java Answers Forum
get Class and Method name of calling class

15 replies on 2 pages. Most recent reply: Jan 12, 2012 12:06 PM by Ilane Walberg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 15 replies on 2 pages [ 1 2 | » ]
Rahul Chaudhary

Posts: 1
Nickname: jawanrc
Registered: Jul, 2002

get Class and Method name of calling class Posted: Jul 18, 2002 5:30 AM
Reply to this message Reply
Advertisement
Hello All,
I have a simple query. How do I get the name of the calling method and class.

Eg. I have a class called MathUtils which has a method called getInt() which returns a number.
INeedInt is class that calles MathUtils.getInt().

Now in getInt() method how do I get the calling class's (i.e. INeedInt) method name.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: get Class and Method name of calling class Posted: Jul 18, 2002 3:41 PM
Reply to this message Reply
There's a less clandestine way of doing this, using the APIs that debuggers use, but I forget what it was, so here is a quick 'n' dirty trick:
class ExTrick 
{
   public static void main(String args[])
   {
      new ExTrick().callMeAnyTime();
   }
   
   void callMeAnyTime()
   {
      try
      {
         throw new Exception("Who called me?");
      }
      catch( Exception e )
      {
         System.out.println( "I was called by " + 
                             e.getStackTrace()[1].getClassName() + 
                             "." +
                             e.getStackTrace()[1].getMethodName() + 
                             "()!" );
      }
   }
}


I will leave the details of error handling and whatnot to you, since you know better what you want to do with this information than I do.

Coden

Posts: 2
Nickname: coden
Registered: Sep, 2005

Re: get Class and Method name of calling class Posted: Sep 20, 2005 6:53 AM
Reply to this message Reply
Does anybody know a way of doing this using JDK 1.3.1?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: get Class and Method name of calling class Posted: Sep 20, 2005 11:16 PM
Reply to this message Reply
Why should this not work with Java 1.3?

wendy spray

Posts: 1
Nickname: wend
Registered: Oct, 2005

Re: get Class and Method name of calling class Posted: Oct 5, 2005 8:32 PM
Reply to this message Reply
Hi,
I'm running 1.3.1 and when I throw the exception e.getStackTrace() is not an available option, not sure why. Do you have any other suggestions on how to find the calling class?

Thanks

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: get Class and Method name of calling class Posted: Oct 5, 2005 11:33 PM
Reply to this message Reply
> Hi,
> I'm running 1.3.1 and when I throw the exception
> e.getStackTrace() is not an available option, not sure
> why. Do you have any other suggestions on how to find the
> calling class?
>
That's coz there is neither such a method in Java 3, nor
4, nor 5. What you are looking for is:

e.printStackTrace();

Make use of the Java API (Java Doc) on the Sun website,
it is your friend.

> Thanks

Kondwani

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: get Class and Method name of calling class Posted: Oct 5, 2005 11:36 PM
Reply to this message Reply
> > Hi,
> > I'm running 1.3.1 and when I throw the exception
> > e.getStackTrace() is not an available option, not sure
> > why. Do you have any other suggestions on how to find
> the
> > calling class?
> >
> That's coz there is neither such a method in Java 3, nor
> 4, nor 5. What you are looking for is:
>
> e.printStackTrace();
>
> Make use of the Java API (Java Doc) on the Sun website,
> it is your friend.
>
Oops my baad, I took the trouble of going through the
Java 5 API considering I noted the code was posted by
Matt, and to dispute code written by him, you really
need your facts aligned. So my bad, getStackTrace()
does exist in Java 5, so it might be that it din't exist
in the older versions - I'm too lazy to check, but if
you're the one writing the application, you should
check.

> > Thanks
>
> Kondwani

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: get Class and Method name of calling class Posted: Oct 5, 2005 11:43 PM
Reply to this message Reply
OK, confirmed! The message is post-Java 3.

http://java.sun.com/j2se/1.3/docs/api/

The link:

http://java.sun.com

Leads you to various versions via the link documentation,
then API Specifications - you can checkout Java 3, 4, 5
APIs.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: get Class and Method name of calling class Posted: Oct 5, 2005 11:43 PM
Reply to this message Reply
> OK, confirmed! The message is post-Java 3.
>
> http://java.sun.com/j2se/1.3/docs/api/
>
> The link:
>
> http://java.sun.com
>
> Leads you to various versions via the link documentation,
> then API Specifications - you can checkout Java 3, 4, 5
> APIs.

Sorry I meant the method is post-Java 3

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: get Class and Method name of calling class Posted: Oct 6, 2005 12:53 AM
Reply to this message Reply
Sorry at closer examination of the code, you won't
get very far incorporating printStackTrace() into
what was given because it doesn't return anything.

Signature: public void ...

Why don't you download either Java 4 or 5, it will
cause you much less pain in the long run.

rocko

Posts: 2
Nickname: rocko
Registered: Jun, 2003

Re: get Class and Method name of calling class Posted: Oct 12, 2005 5:03 AM
Reply to this message Reply
You can use this in java 1.4 and upwards to get the classname.
The key is, you can use this in static blocks/methods/initializers too, when you don't have an instance of the class:

System.out.println(new Throwable().getStackTrace()[0].getClassName());

If you already have an instance, better use this:
void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}

(from the API-doc, works at least with java 5.0)

J. R.

Posts: 2
Nickname: syrion
Registered: Mar, 2005

Re: get Class and Method name of calling class Posted: Sep 5, 2006 9:55 AM
Reply to this message Reply
> Hello All,
> I have a simple query. How do I get the name of the
> calling method and class.
>
> Eg. I have a class called MathUtils which has a method
> called getInt() which returns a number.
> INeedInt is class that calles MathUtils.getInt().
>
> Now in getInt() method how do I get the calling class's
> (i.e. INeedInt) method name.

In Java 1.3, you have 2hacks :
1st) new Throwable().printStackTrace(p) where p is a PrintStream or a PrintWriter. Using a StringWriter you'll be able to get its content as a String and parse the stacktrace
2nd) Use sun.* package, but I don't remember its name. in this package is the class you need (perhaps sun.misc.reflect or so)

In Java 1.4/1.5, no hack anymore, here's how it works :
new Throwable().fillInStackTrace().getStackTrace()[1].getMethodName()


index [0] retrieves the first StackTraceElement of the stack trace, i.e the method you are in (getInt)
index [1] retrieve the 2nd StackTraceElement , i.e getInt's calling method.
StackTraceElement usefull methods :
getClassName()
getMethodName()
getLineNumber()
getFileName()

robin jacob

Posts: 1
Nickname: robijac
Registered: Aug, 2007

Re: get Class and Method name of calling class Posted: Aug 7, 2007 4:08 AM
Reply to this message Reply
hi

thanks a lot....

Wow its nice code.....

its help me a lot.....

Sekhar Duggirala

Posts: 1
Nickname: sekhar1260
Registered: Apr, 2011

Re: get Class and Method name of calling class Posted: Apr 14, 2011 3:26 AM
Reply to this message Reply
Thanks a lot!!! That was really helpful.

In case an exception is thrown, I would like to call the caller method or run the failed transaction. Is there anyway I can do that?

Example Scenario:
I have to read data from a dynamic web page. Sometimes, when I read the value, the method may receive null value. In such cases, instead of throwing an exception, I would like to run the failed transaction again. Please let me know the way if Java supports us to do.

asim khan

Posts: 1
Nickname: asimkhan
Registered: Jul, 2011

Re: get Class and Method name of calling class Posted: Jul 6, 2011 5:58 AM
Reply to this message Reply
Dear All,

I want the URL of a servlet.
My problem is to get the URL of the servlet which is calling my java class.
(I have a simple class MyClass.java in which I have a method show().
And a servlet MyServlet.java is calling my show() function.
Is there any mechanism by which I get the URL of MyServlet.java in show() function.)

Please help me out.
Thanks in advance.:)

Flat View: This topic has 15 replies on 2 pages [ 1  2 | » ]
Topic: Java IO - Set File Hidden Previous Topic   Next Topic Topic:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use