How to show a relationship with static call inside a method?
See original GitHub issueHi!
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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 theAnimal
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 thatAnimal
referencesUtil
(becauseUtil
is mentioned as a class name in the constant pool of theAnimal
class), but there is no way for ClassGraph to know exactly what the relationship is betweenAnimal
andUtil
, other than thatAnimal
somehow depends uponUtil
, 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:
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.
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 sinceAnimal
andUtil
are in different packages, that shouldn’t be the problem. But without theUtil
class, I can’t necessarily reproduce this issue.