question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to show a relationship with static call inside a method?

See original GitHub issue

Hi!

classgraph is very cool!

I’m testing it, but I don’t know how to make classgraph show the relationship between Animal and Util below:

package com.lsc.domain;

import com.lsc.util.Util;

public class Animal {
    public Animal() {
        System.out.println(Util.iAmUseful());
    }
}

https://github.com/tivrfoa/java-class-graph

Is there a way to show that, please?


    private static void testClassGraph() {
        String pkg = "com.lsc";
        try (ScanResult scanResult =
                new ClassGraph()
                    .verbose()               // Log to stderr
                    .enableAllInfo()         // Scan classes, methods, fields, annotations
                    .enableInterClassDependencies()
                    .acceptPackages(pkg)     // Scan com.xyz and subpackages (omit to scan all packages)
                    .scan()) {               // Start the scan
            ClassInfoList allClasses = scanResult.getAllClasses();
            try {
                // then dot -Tsvg < classgraph.dot > classgraph.svg
                String generateGraphVizDotFile = allClasses.generateGraphVizDotFile(500, 500);
                Files.write(Paths.get("classgraph.dot"), generateGraphVizDotFile.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lukehutchcommented, Mar 26, 2021

Thanks for providing the project. I worked on this so long ago that I forgot how it worked. The method you want is actually .generateGraphVizDotFileFromInterClassDependencies(), not .generateGraphVizDotFile().

The problem is that in this case, the reference to Util in the Animal class is in the bytecode of the method, which ClassGraph does not parse (because ClassGraph is focused only on class metadata, not code blocks). So ClassGraph is able to find that Animal references Util (because Util is mentioned as a class name in the constant pool of the Animal class), but there is no way for ClassGraph to know exactly what the relationship is between Animal and Util, other than that Animal somehow depends upon Util, short of parsing the bytecode (which I don’t think ClassGraph will ever attempt to do).

Therefore, I created the method .generateGraphVizDotFileFromInterClassDependencies() for showing inter-class dependencies. It’s simpler in its depiction than the regular .generateGraphVizDotFile(), in that it only shows an arrow from a dependant class to a dependency – it doesn’t attempt to show what type of relationship it is, by using an arrow.

Here is what the output of this method is, using the current release of ClassGraph:

graph

So this is working as intended. Apologies for the confusion, and for having you put together the test project when I should have realized it was not needed!

Thanks for your report.

1reaction
lukehutchcommented, Mar 25, 2021

Thanks! Is there any chance you could please put together a tiny but complete testcase project that reproduces the problem? My first thought was that you might need .ignoreClassVisibility(), but since Animal and Util are in different packages, that shouldn’t be the problem. But without the Util class, I can’t necessarily reproduce this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Reveal Static Call Relationships in Your Code
The easiest static method is the one with the least coupling, or with a coupling that is easiest to remove, e.g.. static method...
Read more >
UML relationship of a static call from another class
Show activity on this post. Yes there is an association between these two classes. The association is neither an aggregation nor a ...
Read more >
Static properties and methods - The Modern JavaScript Tutorial
The first way can be implemented by the constructor. And for the second one we can make a static method of the class....
Read more >
What is Static Method in Java [+Examples]? - HubSpot Blog
A static method is a method that belongs to a class rather than an instance of a class. This means you can call...
Read more >
Static Method in Java | Example Program - Scientech Easy
Static method can be called or accessed directly using class name. The syntax to call a static method in Java is as follows:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found