import java.util.Stack;
public class Solution {
public static void main(String[] args) throws Exception {
method1();
}
public static String method1() {
method2();
//write your code here
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int stackTraceLength = stackTraceElements.length;
int callingMethodIndex = stackTraceLength - 1;
String callingMethodName = stackTraceElements[callingMethodIndex].getMethodName();
System.out.println(callingMethodName);
return callingMethodName;
}
public static String method2() {
method3();
//write your code here
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int stackTraceLength = stackTraceElements.length;
int callingMethodIndex = stackTraceLength - 2;
String callingMethodName = stackTraceElements[callingMethodIndex].getMethodName();
System.out.println(callingMethodName);
return callingMethodName;
}
public static String method3() {
method4();
//write your code here
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int stackTraceLength = stackTraceElements.length;
int callingMethodIndex = stackTraceLength - 3;
String callingMethodName = stackTraceElements[callingMethodIndex].getMethodName();
System.out.println(callingMethodName);
return callingMethodName;
}
public static String method4() {
method5();
//write your code here
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int stackTraceLength = stackTraceElements.length;
int callingMthodIndex = stackTraceLength - 4;
String callingMethodName = stackTraceElements[callingMthodIndex].getMethodName();
System.out.println(callingMethodName);
return callingMethodName;
}
public static String method5() {
//write your code here
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int stackTraceLength = stackTraceElements.length;
int callingMthodIndex = stackTraceLength - 5;
String callingMethodName = stackTraceElements[callingMthodIndex].getMethodName();
System.out.println(callingMethodName);
return callingMethodName;
}
}
My display is correct, I'm popping the last element in the stack trace and getting the calling method name. But why is the Test still failing.
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Ramen
17 March 2021, 04:48
Okay, I solved it. I should always be grabbing the 3rd index in the stack trace.
[0] gives the thread
[1] gives the current method name
[2] gives the previous method name
0
Guadalupe Gagnon
17 March 2021, 13:36useful
Just a little further explanation:
The getStackTrace() method returns an array of StackTraceElements. This will be populated with a path from the calling method all the way back through the program to the main method. So, as you have figured out, the first element is the call the the getStackTrace() method; the next element is the line of code where that method was called from; the next would be where that one was called from, and so on back to main:
In this quick example pseudo code. The resulting array of the getStackTace() call would be:
[getStackTrace, method2, method1, main]
Which is just the path as described above. If I had more methods in between then they would be in this list as well. This example is abbreviated, but the real elements in code would have full class names plus line numbers. +1
Ramen
25 March 2021, 04:29
Thanks for the breakdown. This explanation is clear. Didn't realize the call stack works this way.
0
Ramen
25 March 2021, 04:47
[0] The first element is the call to the getStackTrace() method
[1] The next element is where the function calls end and the function is returned or the bottom of the stack trace
[2] The next element is the line of code where that one was called from
[3] And so on back to main or the beginning or the top of the stack trace
0