7

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.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • What are you trying to do? Maybe static source code tools like SonarQube could give you that information, but I don't know if you can query this using an API or would have to click through the SonarQube web page. – Robert Mar 27 '19 at 16:03
  • @Robert For analysis purposes, I would like to have a database for our company that shows which class and which methods are used where. This is especially interesting for refactoring. – J Fabian Meier Mar 27 '19 at 16:11
  • Do you know https://github.com/classgraph/classgraph It should be possible for solve your first point, but for the second I am not sure. – Christian Apr 01 '19 at 10:09
  • Have you ever try another answer in this site? https://stackoverflow.com/questions/442747/getting-the-name-of-the-currently-executing-method/8592871#8592871 – tommybee Apr 01 '19 at 14:24
  • @ChristianLutz Looks like a promising start. – J Fabian Meier Apr 01 '19 at 14:31
  • @tommybee Could you explain how to solve my question with this? The other question seems to be about inspecting the running code. – J Fabian Meier Apr 01 '19 at 14:32
  • @JF Meier, you mean rational rose like a reverse engineering tool? I'm not sure it helsp you but there is a javadesigner of star uml, It is an open source project and you can check the reverse package in the source code..https://store.modelio.org/resource/modules/java-designer-open-source.html – tommybee Apr 01 '19 at 14:48
  • An idea, not sure if this would scale. Assuming that you would iterate over all class files one by one, you can think on this: 1) Classes used in other classes: 1a) Classes used from other packages: This you can achieve by checking "import" statements. (with few flaws) 1b) Classes used from same package: For this, you can iterate over file names from same package and do a String search in class being processed. 2) Methods calling other methods: 2a) You could think to use regex for this. Regex with capture-group (1) as method name and capture-groups (1,2,3..n) as called method names. – Sumit A Apr 02 '19 at 17:30
  • @SumitA Thank you, but this is too brittle for me. – J Fabian Meier Apr 02 '19 at 18:59

3 Answers3

4

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)

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
2

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.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
-1

I am not sure how to get a listing, but for identifying refactoring opportunities, you might try IntelliJ IDEA. It will dull out the signature line of any methods that are not accessed in the project. It will also detect code segments that are repeated elsewhere in the project, so you can extract common code.

Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
  • 1
    Thank you, but this is not what I am looking for. We have more than 1500 Java projects and we want to scan them all in an automated fashion, analysing their interdependencies. This cannot be done from an IDE. – J Fabian Meier Apr 01 '19 at 14:50