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

6
  • Its hard to understand what you're asking. You're asking how to figure out what class a section of code is in? Commented Mar 22, 2016 at 8:19
  • the normal stacktrace should already tell you the method chain that is called Commented Mar 22, 2016 at 8:19
  • Have a look at stackoverflow.com/questions/1069066/…, that/s one way to get the stack trace during runtime. Commented Mar 22, 2016 at 8: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.. Commented Mar 22, 2016 at 8:42
  • Class-loader is responsible for class metadata upload and not in any kind of profiling or instrumentation. Please refer to my updated answer. Commented Mar 22, 2016 at 9:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.