I am trying to find an answer to a simple question, and no luck so far.
The question is: can i use java class loader (implement a new class loader) to investigate the stack trace ?
To be more precise,
I am talking about scenerio in which I am running a test on some code, and I want to use the stack trace in order to determine which of the test classes run this code ? (one way is to look on the closest test in stack trace)
another advance idea, is to use the class loader in order to find the closest "test Method" that ran current code..
Edit:
I dont have an access to the tests that run the code nor the code itself.. this is why I am thinking about the class loader.. -> I can run a class loader, but I can't change the application code or application test code.. (this is why i can't use Thread.getStackTrace()).
To get current stack-trace you can use Thread.getAllStackTraces()
method - it gives you Map object with Thread stacks. You can refer this map with Thread.currentThread()
key.
UPDATE
If you need to watch stack-traces outside of your host/test application you can simply use jstack util to find out what particular thread is running target code.
Also, you can use byte-code tracing tool like BTrace. With BTrace you can track methods invocation from within other methods using. Please have a look at this tutorial. I think you would be especially interested in the part that describe @Location parameter usage of @OnMethod annotation.