Skip to main content

In Java, how do i find the caller of a method using stacktrace or reflection? [closed]





Possible Duplicate:


Get current stack trace in Java







I need to find the caller of a method. Is it possible using stacktrace or reflection?


Comments

  1. StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()


    According to the Javadocs:


    The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.


    A StackTraceElement has getClassName(), getFileName(), getLineNumber() and getMethodName().

    You will have to experiment to determine which index you want (probably stackTraceElements[1] or [2]).

    ReplyDelete
  2. Sounds like you're trying to avoid passing a reference to this into the method. Passing this is way better than finding the caller through the current stack trace. Refactoring to a more OO design is even better. You shouldn't need to know the caller. Pass a callback object if necessary.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?