Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out used classes and methods from Java source code

Tags:

java

For Java source files, I would like to find out:

  • Which classes use which other classes (fully qualified names)?
  • Which methods call which other methods (fully qualified names)?

What would be a reasonable way to achieve that?

EDIT:

To clarify: I want a list of source code files as input. The output should be (as specified above) which class uses which other class and which method calls which other method. I do not want to inspect other loaded classes at runtime, like when using reflection.

like image 394
J Fabian Meier Avatar asked Mar 27 '19 15:03

J Fabian Meier


People also ask

How to get the methods of a class in Java?

The getMethods () method of java.lang.Class class is used to get the methods of this class, which are the methods that are public and its members or the members of its member classes and interfaces. The method returns the methods of this class in the form of an array of Method objects.

Where can I download the source code for a Java class?

It's available for public download on the official download page. If you're using Eclipse whenever you use the class you could right click > View Source (or simply click the class > F3) and it'll open a new tab with the source. Show activity on this post. So what you're trying to do is get the Java class at execution.

How do you use multiple classes in Java?

Using Multiple Classes. You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.

How do I view the source code of a class?

If you're using Eclipse whenever you use the class you could right click > View Source (or simply click the class > F3) and it'll open a new tab with the source. Show activity on this post.


2 Answers

You need to use static analysis tool as STAN standalone mode:

The standalone application is targeted to architects and project managers who are typically not using the IDE.

Or JArchitect (available also using command line)

JArchitect is a powerful tool for static code analysis. It can provide a lot of insight into complex code bases. Using custom code queries you are able to build your own rule sets in a very comfortable way.

In the Class Browser right-click menu, JArchitect proposes to explore the graph of dependencies between members (methods + fields) of a type.

Another option is SourceTrail

The graph visualization provides a quick overview of any class, method, field, etc., of interest and all its relations. The graph is fully interactive. Use it to move through the codebase by focusing on related nodes and edges.


(source: sourcetrail.com)

like image 159
user7294900 Avatar answered Oct 20 '22 09:10

user7294900


Unfortunately, reflection doesn't give you all the information you need to do this.

I've done it with ASM (https://asm.ow2.io/).

It provides the ability to walk the byte code of all of your classes using the visitor pattern, including the actual method implementations, from which you can extract the references to other classes.

I'm sorry that I cannot provide the implementation, because it's proprietary.

Note that this works from your .jar files, not your sources. If you really need to work from sources, then have a look at https://github.com/javaparser . Really, though, it's better to use the byte code, since the java language changes frequently, while the byte code specification does not.

like image 30
Matt Timmermans Avatar answered Oct 20 '22 08:10

Matt Timmermans