Class Dependency Tools for Java

2019-04-06 06:33发布

问题:

I know a lot of related questions have been given on SO, but I've not been able to find anything fully satisfactory - probably because my requirement is slightly different from each of those questions raised.

I have a gigantic codebase that I'd like to break down into smaller logic units. To do this, I need to find all the tangled/circular dependencies and resolve them. Due to the size of the existing spaghetti, the only feasible strategy appears to be divide-and-conquer, so I need to divide the huge project into smaller pieces first or peel off independent parts and tackle the smaller pieces separately.

Most of the tools I can find out there seems to only work on the class or package level, but they don't seem to support the concept of sub-packages. For example, If I have a root package, 3 packages under that, and then 5 subpackages under each of the 3, then I'll get 1+3+15=19 packages, which will give me a crazily complicated graph. Now what I hope to be able to do is to analyze the dependency relationship between the 3 top level packages first (number of incoming/outgoing packages, and which classes originate these dependencies), clean it up on that level, before diving into these top level packages to work on the next level.

Now, I've used Structure 101 for this purpose, and it works great, but when the trial expires, the price of the product is a little steep for my budget. Is there a free/open source solution out there?

Thanks in advance!

回答1:

To my knowledge there is no free tool that is remotely comparable to structure 101. (I am not affiliated!)

So you already have a solution, but you say the pricing is too high. This may be a fallacy.

I have a gigantic codebase that I'd like to break down into smaller logic units.

Presumably you're not doing this for fun but somebody is paying you to do it. The USD 900 for structure 101 is around 3-4 days of work - assuming you make something like HKD 40k (USD 5200) per month as a senior software developer in Hong Kong. It should be possible to make the argument that this will actually save a lot of money in the end.



回答2:

a trial of JAVADepend offers the possibility to analyse the dependency relationship you want with no limitation in time.

it's a nice tool.



回答3:

Have a look at CodePro Analytix. It has several tools for dependency analysis plus tons of other things that you should be aware of.

For offline analysis, look at SonarQube. It has no dependency graph but has a module for dependency cycles in your classes plus a ton of quality measurement modules.



回答4:

project: https://github.com/lukehutch/fast-classpath-scanner

how to use latest tips/news: https://github.com/lukehutch/fast-classpath-scanner/releases

pre-compiled jars+source: https://oss.sonatype.org/#nexus-search;quick~fast-classpath-scanner

example: https://github.com/lukehutch/fast-classpath-scanner/wiki/3.9.-Generating-a-GraphViz-dot-file-from-the-classgraph

ScanResult scanResult = new FastClasspathScanner(
  MyClass.class.getPackage().getName()).scan();
String str = scanResult.generateClassGraphDotFile(9.2f, 8.0f);

Files.write(str, new File("GraphViz.dot"), StandardCharsets.UTF_8);

System.out.println("now run this at terminal: dot -Tsvg < GraphViz.dot > GraphViz.svg");


回答5:

Have you looked at either ObjectAID or PlantUML plug-ins for eclipse? Not as robust as what you're asking about, but definitely provide some good UML modelling mechanisms for free.



回答6:

Disclaimer: I'm the author of coffea

I taught I should share, since I'm facing a similar problem, again (i.e. a gigantic codebase with multilevel, complex package dependencies and hardly any structure), and I'm not aware of any free alternatives available for the moment.

In my opinion, nothing beats scripting and/or explicit grouping rules when dealing with a chaotic package structure, and since I didn't find any reasonable way to do this earlier, I rolled out my own tool.

Of course, this will not beat any commercial alternative, but it's a fairly simple approach, that should allow you to investigate the interesting parts without the surrounding noise.



回答7:

Try FastClasspathScanner: https://github.com/fast-classpath-scanner/fast-classpath-scanner (Disclaimer, I am the author)

For example:

try (PrintWriter out = new PrintWriter("/tmp/classgraph.dot")) {
    out.print(new FastClasspathScanner()
        .whitelistPackages("com.xyz")
        .enableAllInfo()
        .scan()
        .getAllClasses()
        .generateGraphVizDotFile(
            sizeX, sizeY, showFields, showMethods, showAnnotations
        ));
}