• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do i find parent caller of a method using stacktrace or reflection  RSS feed

 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example

public class Temp {

public static void main(String[] args){
Temp temp = new Temp();
temp.getPearl();
}

public void getPearl(){
eat();
}

public void eat(){
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace() ;
}
}

i can use StackTraceElement to get the parent caller, but how can i return it immediately, because if there are some other methods in get pearl, then it would be difficult to find out in call stack.

thak you
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim. What's difficult about retrieving the caller frame from the stack trace? It should be the second last array element.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!