0

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()).

user3250354
  • 101
  • 10
  • Its hard to understand what you're asking. You're asking how to figure out what class a section of code is in? – James McDonnell Mar 22 '16 at 08:19
  • the normal stacktrace should already tell you the method chain that is called – SomeJavaGuy Mar 22 '16 at 08:19
  • Have a look at http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java, that/s one way to get the stack trace during runtime. – Philipp Mar 22 '16 at 08:22
  • I don't want to make it to do something else, I want to make a connection between the test class and the code it tested.. i am hopoing that the use of the class loader will help me with that.. – user3250354 Mar 22 '16 at 08:42
  • Class-loader is responsible for class metadata upload and not in any kind of profiling or instrumentation. Please refer to my updated answer. – nukie Mar 22 '16 at 09:15
  • I think you are planning to use the wrong tool for the job. Have you considered aspect oriented programming. This would let you modify the thread local storage in all methods annotated with test and track the flow of the program trough all called methods. without having access to the sourcecode. Giving you access to everything. I would try out AspectJ. – Espen Brekke May 04 '16 at 12:21

1 Answers1

1

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.

nukie
  • 691
  • 7
  • 14