0

I have a classes in my project with the following structure

Class A Class B Class C

Under Class C.... I have the function mention below

public static String getCallingClassname() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    StackTraceElement e = stacktrace[4];
    String classname = e.getClassName();
    return classname;   }

Class A is my main file from where I execute the script. Now, from Class A, I calls the function of Class B which in turn calls the getCallingClassname() method of Class C. In order to get the classname of Class A, I have used the index no as 4. Problem is, there are many times when Class A directly calls the getCallingClassname() method of Class C, so in that case index 4 gives a wrong value.

Is there any method, through which I can always get the Parent Classname i.e. Class A in my example above ?

Thanks

Naseem
  • 911
  • 5
  • 18
  • 48
  • See this answer on another thread: [http://stackoverflow.com/a/1069150/365188](http://stackoverflow.com/a/1069150/365188) – Ozair Kafray May 18 '14 at 07:21
  • The 'parent class name' doesn't appear to have any relevance. What you need is the name of the calling class, whatever it may be, and you can get all that from the stack trace. – user207421 May 18 '14 at 10:11
  • Apologies for not able to make myself clear. I tried to edit my question. Please take a look. Thanks – Naseem May 18 '14 at 15:05

0 Answers0