Using getStackTrace()






I have recently started working on UI development by using a third-party library. One of the key feature of this library is,  it provides UI components which fetches the backend data just by dragging and dropping on to the page with a little effort.

I have enabled this feature by registering a java method against a UI component (drop down in my case) in the library. So, I expected, whenever the page loads, the drop-down pulls the live data from the backend.

But, I came across a situation where my method is called twice by the framework and it started showing the inconsistent results.

I have no clue what went wrong? The possible reasons for failure could be either my configurations are duplicated or there could be a bug in the framework.  I may not fully troubleshoot the issue, since, this is a third-party code. But, I can do some initial analysis like who is making these 2 calls??  Let's see how did I find out?


package random;

import java.util.List;

public class Test2 {

 public static void main(String []args){
  testMethod();
 }
 
 public static void testMethod(){
  StackTraceElement []elements = Thread.currentThread().getStackTrace();
  for(StackTraceElement elem : elements){
   System.out.println("stacktrace : "+elem.getClassName()+", "+elem.getMethodName());
 }
}
  


stacktrace : java.lang.Thread, getStackTrace
stacktrace : random.Test2, testMethod
stacktrace : random.Test2, main

Code snippet and stack trace are quite clear.
getStrackTrace() method of the Thread class is capable of finding the method callers. So, this code helped me to figure out the two callers for my method.




Comments

Popular posts from this blog

Distributed database design using CAP theorem

LDAP - Basics

Easy approach to work with files in Java - Java NIO(New input output)