0

I have the following scenario where both testOne() and testTwo calls same callMe() method.

How do I decide inside callMe() method who called callMe().

public void testOne(){
    callMe();
}

public void testTwo(){
    callMe();
}

public void callMe(){
    System.out.println("I was called by following method."+methodName);     

}


Any sort of help is appreciated.
user234194
  • 1,683
  • 10
  • 38
  • 56
  • 6
    In what context are you trying to figure this out? If you're in a debugger, look at the call stack. If you're trying to do this for some sort of program design reason, tell us what that reason is, and we'll talk you out of doing it that way. – Marvo Jul 21 '11 at 18:46

4 Answers4

7

Any solution that has you generating a stacktrace and looking at the second frame is one that is going to lead to pain - what you are essentially doing is bypassing the idea of passing what a function needs to it in order for the function to do it's work.

If you need the name of the caller method, then just pass it as a parameter. If you need some other piece of data to decide what to do in the callMe() method, pass it (as a boolean, int, etc.).

It will confuse other developers working on your code why callMe() has what are essentially secret parameters.

public void testOne(){
    callMe("testOne");
}

public void testTwo(){
    callMe("testTwo");
}

public void callMe(String methodName){
    System.out.println("I was called by following method."+methodName);     
}
matt b
  • 138,234
  • 66
  • 282
  • 345
2

My best answer is to query the stack trace.

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String previousMethodName = null;

for (int i = 0; (i < stackTrace.length) && (previousMethodName == null); i++)
{
    if (stackTrace[i].getMethodName().equals("callMe") && (i < stackTrace.length - 1))
    {
        previousMethodName = stackTrace[i + 1].getMethodName();
    }
}

if (previousMethodName != null)
{
    System.out.println("Previous method = " + previousMethodName);
}
Kainsin
  • 447
  • 2
  • 8
1

sorry, i meant to answer your question and not comment :( so here it is

i think this already answered question may help you out: Get current stack trace in Java

Community
  • 1
  • 1
marcelog
  • 7,062
  • 1
  • 33
  • 46
1

The simplest approach is to use a parameter

public static void testOne(){
    callMe("testOne");
}

public static void testTwo(){
    callMe("testTwo");
}

public static void callMe(){
    System.out.println("I was called by following method."+methodName);     
}

However, you can use the call stack.

public static void callMe(){
    String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
    System.out.println("I was called by following method."+methodName);
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Why `[2]`? The first element (`[0]`) would be the `callMe` method. Wouldn't the caller be the second element rather than the third? – Ted Hopp Jul 21 '11 at 18:51
  • 1
    No, the first element holds the `getStackTrace` method from `Thread.java`. – Kainsin Jul 21 '11 at 18:56
  • The method name of the first element `[0]` is `getStackTrace` as you have to call it to get the stack trace. :) The second name `[1]` is `callMe`, the third `[2]` is the callee. – Peter Lawrey Jul 21 '11 at 19:00