Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Java, you can use the Thread.currentThread().getStackTrace() method to get the stack trace of the current thread, which includes information about the call stack. You can then use this information to find the parent function that calls another function in the child function.

Here is an example code snippet that demonstrates how to get the parent function:

public class Example {
    public static void main(String[] args) {
        childFunction();
    }

    public static void childFunction() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        String parentFunction = stackTrace[2].getMethodName();
        System.out.println("Parent function: " + parentFunction);
    }
}

In this example, the main function calls the childFunction, and the childFunction uses the getStackTrace method to get the stack trace. The parentFunction variable is then assigned to the name of the method at index 2 in the stack trace, which is the method that called the childFunction. Finally, the parent function name is printed to the console.