Performance and scalability: Retrieve Calling Class name(with Package structure)

  1. Hi, We have a requirement where we would like to retrieve Calling Class name(with Package structure)and use it inside the called class. I have searched and found out two ways ... 1) String callingClassName = new Throwable().fillInStackTrace().getStackTrace()[1].getClassName(); and 2) String callingClassName = sun.reflect.Reflection.getCallerClass(2).getName(); but these two approaches pose performance issue as both take arround 15-16 millisecond to execute, which is not acceptable in large application. Any other suggestions or enhancement to above two approaches would be highly appreciated.

    Threaded Messages (2)

  2. Reflection is the way[ Go to top ]

    Hi, I dont think u can do without using reflection unless u main a call trace programmatically which will have a memory foot print. So choose one of them Thanx and Regards KR Kumar
  3. Re: Reflection is the way[ Go to top ]

    Hi Kumar, I was thinking of using a static ThreadLocal variable and setting a unique value to it in the application's main thread. then accessing the same in my class where ever i would like to distinguish calling threads and control my flow. I tried this approach now and its working fine.