56

To fix a test case I need to identify whether the function is called from a particular caller function. I can't afford to add a boolean parameter because it would break the interfaces defined. How to go about this?

This is what I want to achieve. Here I can't change the parameters of operation() as it is an interface implementation.

operation()
{
   if not called from performancetest() method
       do expensive bookkeeping operation
   ...       

}
4
  • 15
    I'd just emphasize that in almost every circumstance this is a really really bad idea. Make sure you have a really really good reason before implementing this. Consider any and all possible alternatives. For example: can you add an interface? Commented Oct 31, 2010 at 22:46
  • I found out a way without it. Thanks a lot for the suggestion. Commented Oct 31, 2010 at 23:30
  • 4
    One reason this is a really bad idea is that the only viable implementation approach is Expensive.
    – Stephen C
    Commented Oct 31, 2010 at 23:34
  • 3
    And it's a particularly terrible idea to do it just to fix a test case.
    – user207421
    Commented Nov 1, 2010 at 2:28

8 Answers 8

103

You could try

StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
StackTraceElement e = stacktrace[2];//maybe this number needs to be corrected
String methodName = e.getMethodName();
6
  • 4
    new Throwable().fillInStackTrace().getStackTrace()[1].getMethodName() Commented Oct 31, 2010 at 22:47
  • 1
    @mootinator: I think that getStackTrace() is faster.
    – thejh
    Commented Oct 31, 2010 at 23:08
  • 1
    if correctly implemented, they are both the same: thread.getStackTrace() does return (new Exception()).getStackTrace(); if the thread is the current one.
    – Stephen C
    Commented Oct 31, 2010 at 23:30
  • 1
    @mootinator - the fillInStackTrace() call is redundant.
    – Stephen C
    Commented Oct 31, 2010 at 23:31
  • 1
    how to get fully qualified method name ?
    – Paresh
    Commented Jun 22, 2015 at 5:28
20

Here is code that is more modern (available in Java 9+) and better performing.

private static String getCallerMethodName()
{
   return StackWalker.
      getInstance().
      walk(stream -> stream.skip(1).findFirst().get()).
      getMethodName();
}

Change skip(1) to a larger number as needed to go higher on the stack.

This performs better than Thread.currentThread().getStackTrace() because it does not walk the entire stack and allocate all of the stack frames. It only walks two frames on the stack.

This method can be adapted to return StackWalker.StackFrame that has a lot of information about the method.

3

I tweaked the code that is being discussed here and customized it to get the invoking method. What the code does here is to iterate over the stack trace elements and as soon as it finds the name of the method being invoked, it gets the name of the previous method, which in turn will be the method that is invoking this method.

private String method() {
    String methodName = null;
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    for (int i = 0; i < stacktrace.length - 1; i++) {
        if (stacktrace[i].getMethodName().equals("method")) {
            methodName = stacktrace[i + 1].getMethodName();
            break;
        }
    }
    return methodName;
}
1
  • Like this way, I am currently using it to debug into some RXJava stuff to figure who is calling a method twice! Thank you!
    – JPM
    Commented May 19, 2020 at 17:46
2

Here's a function I wrote to Log the function name of the function that calls it. It runs up the stack trace until it finds a function named logIt, then displays the next name. It's a dirty hack, so don't do it unless you're using it to debug.

private static void logIt() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    boolean logged = false;
    boolean foundMe = false;
    for(int i=0; i<stacktrace.length; i++) {
        StackTraceElement e = stacktrace[i];
        String methodName = e.getMethodName();
        if (foundMe) {
            if (!methodName.startsWith("access$")) {
                Log.i(TAG, String.format(Locale.US, "%s.%s", e.getClassName(), methodName));
                logged = true;
                break;
            }
        } else {
            if (methodName.equals("logIt")) {
                foundMe = true;
            }
        }
    }
    if (!logged)
        Log.e(TAG, "unlogged call");
}
0

Another sample for android usage:

//package your.package.name;
import android.util.Log;
/*
 File name: MyDebugLog.java
*/
public class MyDebugLog {
    private static final int    index      = 4;     // <== Index in call stack array
    private static final String methodName = "Log"; // <== Name of method for public call
    private static String getCallerName() {
        String caller = "NONE";
        final StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
        for (int i = 0; i < stacktrace.length; i++) {
            Log.e("Method ", "[" + i + "]" + stacktrace[i].getMethodName());
        }
        if (stacktrace.length >= index){
            caller = stacktrace[index].getMethodName();
        }
        return caller;
    }

    private static String getTag() {
        String tag = "NONE";
        final StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
        for (int i = 0; i < stacktrace.length; i++) {
            Log.e("Method ", "[" + i + "]" + stacktrace[i].getMethodName());
            if (stacktrace[i].getMethodName().equals(methodName)) {
                tag = "("+stacktrace[i + 1].getFileName() + ":" + stacktrace[i + 1].getLineNumber()+")";
                return tag;
            }
        }
        return tag;
    }

    public static void Log(String message){
        Log.v(getTag(), getCallerName() + " " + message);
    }
}

Usage:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sample_main);
            MyDebugLog.Log("XXXXX");
    }

Output:

    V/(MainActivity.java:117): onCreate XXXXX

Sample of arrays:

 getTag Sample of stacktace array:

    Method: [0]getThreadStackTrace
    Method: [1]getStackTrace
    Method: [2]getTag
    Method: [3]Log                 <== Method for external call
    ...
 getName Sample of stacktace array:
    Method: [0]getThreadStackTrace
    Method: [1]getStackTrace
    Method: [2]getCallerName
    Method: [3]Log
    Method: [4]onCreate            <== Our external method
    Method: [5]performCreate
    ...
0

I sometimes want to make some outputs to the logcat. So I wrote a tiny class with some testing-methods:

public class Common {

    // can be used as test-flag anywhere in the app (set to false, when release the app)
    public static boolean b_TEST_MODE = true;

    public static void echo(String message) {
        if (b_TEST_MODE) {
            StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
            // subtring(25) is to cut off app-name from output
            System.out.println(">>> " + stackTraceElements[3].toString().substring(25) + ": " + message);
        }
    }
}

now you can call it from anywhere in the app to get some infos:

String sSQLQuery = "SELECT * FROM database WHERE id=23";
Common.echo(sSQLQuery);

The logcat prints out:

>>> MainActivity.onCreate(MainActivity.java:46): SELECT * FROM dateabase WHERE id=23
0

I have no idea why but in my shop the develop system differs from the test and production environments shifting position in the stack. I was forced to loop through the stack to find and get the calling method from the next element in the stack trace. A little clunkier but so far has been consistently returning the desired method. I use this as part of my error handling to identify where an exception was caught.

    List<String> list = new ArrayList<String>();
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    for (int i = 0; i < Thread.currentThread().getStackTrace().length; i++) {
        System.out.println("Stack: "
                + i
                + " Class: "
                + elements[i].getClassName()
                + " Method: "
                + elements[i].getMethodName());
        if (elements[i].getMethodName().equals("<init>")) {
            list.add(elements[i + 1].getClassName());
            list.add(elements[i + 1].getMethodName());
             break;
        } // if
    } // for
0

Check below lines by changing index value:

System.out.println("Name of Caller method:" 
+ new Throwable().getStackTrace()[1].getMethodName());

    System.out.println("Name of Current method:" 
+ new Throwable().getStackTrace()[0].getMethodName());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.