Call the method tree: how do coverage tools work in Java? Is there an API?

I am looking for a way to list all methods called (call tree) by another method, at java runtime.

I am looking for an api or output that allows me to use data with a script.

Any ideas?

thanks

+6
source share
3 answers

Coverage and profiling tools mainly use two methods: periodically polling the JVM for the status of various threads or toolkit byte-code of the application to supplant the corresponding JVM data.

There is no direct API support in java, but there are many tools you can use:

  • You can catch stream dumps using jstack or similar tools, save them to a file, then analyze (or write a script / program for analysis), this is a survey.
  • Use ASM or BCEL to change the bytecode of your application, it pushes, but it is very difficult to do.
  • Using AspectJ using a boot time triangulator is just a command line option to enable it.

Solution 3 is much simpler, clean and versatile.

For printing at runtime, any method that is called as a result of the method execution is a simple aspect of the trace, something similar to:

public aspect TraceCalls { pointcut aCall : call(* *(..)); pointcut inside : cflow(execution(public MyClass.MyMethod(..))); before() : aCall() && inside() { System.out.println(thisJoinPoint); } } 

Obviously, you can access much more data, print it in a file, format it, etc.

(Note that I wrote this code here, so it might be filled with syntax errors)

+3
source

Well ... this is a good question first of all ... I doubt very much that there is such a utility on the market available on the market ... however, there are ways to get around this ... for example, using the debugger tool in one of your favorite IDEs, such as Eclipse and Netbeans ...

+1
source

The right way to do this is with a static analyzer that looks at your code and determines the set of possible subscribers. This will tell you what the method can call under any (conservative) circumstance. Our DMS Software Reengineering Toolkit with its Java Front End could be customized for this. It supports analysis of computational points; method calls are essentially calls through class vectors.

As a bad person, you can use a profiling tool that captures the call tree. This will only cause calls made by a particular program launch, unlike all possible call contexts.

Our Java Timing Profiler will generate a call tree.

0
source

Source: https://habr.com/ru/post/896170/


All Articles