Java 类和方法的依赖关系图
我有一个 Java 项目,我已经研究了一段时间了。设计一开始相当不错,但随着更改而慢慢退化。我正在寻找某种可以分析项目的工具。拥有不同类/方法的依赖关系图真是太好了。我觉得某些方法只是为了实现一个非常具体的目标。我想消除不必要的代码并使我的设计更好。
任何建议都会很棒!谢谢!
I have a Java project that I've been working on for a while. The design started out pretty good but slowly degraded as changes were made. I'm looking for some sort of tool that will analyze the project. It'd be really nice to have a map of the dependencies of different classes/methods. I feel like certain methods are only in there to fulfill a very specific goal. I'd like to eliminate unnecessary code and make my design better.
Any suggestions would be great! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能需要查看 JDepend。它是专门为解决您面临的问题而开发的。
作为一名顾问,对我来说,被扔进巨大的遗留代码库是很常见的。通常我做的第一件事就是运行 JDepend 来尝试了解代码库以及所有的依赖项。
祝你好运。
You may want to check out JDepend. It was specifically developed to solve the problem you are facing.
Working as a consultant it is very common for me to be thrown in to a huge legacy code base. Typically the first thing I do is run JDepend on it to try to get my bearing on the code base and all of the dependencies.
Good luck.
有许多工具可用于此目的,具体取决于系统的大小。它们的鼻祖是 Structure101,一种商业产品。
http://structural101.com
我在一些大型项目中使用过它 - 它很复杂,但很容易成为所有项目中最全面的我知道的工具。它可以绘制一个巨大的架构,并让您理清一些复杂的东西。它有一个名为 reStructure101 的姊妹产品,您可以在其中进行假设场景。
Stan4j 是一个不错的竞争对手。它远没有那么完整,但在一些方面做得更好,特别是在类级别显示依赖关系上下文。
http://www.stan4j.com
(我拥有这两种产品,但与其中任何一个都没有商业关系)
另一种方式查看复杂依赖关系(特别是在项目级别)的方法是依赖结构矩阵 (DSM)。虽然 S101 可以做 DSM,但 Lattix 有更多支持 - 它可以处理 Spring 和 Hibernate 依赖项:
http://lattix.com/
(我没有使用过 lattix - 它们使得评估它变得太困难,这很奇怪,因为我在一些非常大型系统上工作)
如果您的软件相当小,并且您更感兴趣在简单的类级别,那么像 Architexa 这样的东西可以帮助您更好地理解结构:
http://www.architexa.com/
最后,如果您想自己开发,可以使用 ASM 轻松实现。它甚至包括一个在包级别进行跟踪的依赖关系矩阵工具:
http://asm .ow2.org/doc/tutorial-asm-2.0.html
(请参阅页面下方的 nify 依赖关系矩阵)
这是它的源代码 - 它非常简单,但很聪明。它基于字节码工作。
http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/asm/asm/examples/dependency/src/org/objectweb/asm/depend/
Many tools are available for this, depending on the size of your system. The grandaddy of them all is Structure101, a commercial product.
http://structure101.com
I've used this on some big projects - it's complex but easily the most comprehensive of all the tools I know. It can graph a huge architecture, and lets you untangle some complex stuff. It has a sister product called reStructure101 where you can do what-if scenarios.
A nice competitor is Stan4j. It's nowhere near as complete, but does some stuff better, particularly showing the dependency context at the class level.
http://www.stan4j.com
(I own both products, but have no commercial affiliation with either)
Another way of viewing complex dependencies, particularly at the project level, is a Dependency Structure Matrix (DSM). Although S101 can do a DSM, Lattix has more support - it can handle Spring and Hibernate dependencies:
http://lattix.com/
(I've not used lattix - they make it too hard to evaluate it, which is bizarre because I work on some very big systems)
If your software is fairly small, and you are more interested in the simple class level, then something like Architexa may help you understand the structure better:
http://www.architexa.com/
Finally, if you want to roll your own, you can easily do this with ASM. It even includes a dependency matrix tool which tracks at the package level:
http://asm.ow2.org/doc/tutorial-asm-2.0.html
(see down the page for the nify dependency matrix)
Here's the source for it - it's very simple, but clever. It works off bytecodes.
http://cvs.forge.objectweb.org/cgi-bin/viewcvs.cgi/asm/asm/examples/dependencies/src/org/objectweb/asm/depend/
这不是一个完美的答案,但可能有助于快速检查。如果您使用 Eclipse 进行开发,则可以通过选择方法名称并按 Ctrl-Alt-H(或右键单击并选择“打开调用层次结构”)来查看方法的调用图。您可以使用打开的视图来查看什么调用了该方法,以及该方法调用了哪些方法。
This is not a perfect answer, but it may be helpful for quick checks. If you are using Eclipse for your development, you can view the call graph for a method by selecting its name and pressing Ctrl-Alt-H (or right-click and select "Open Call Hierarchy"). You can use the view that opens to see what calls the method, and what methods are called by the method.
有几个很好的 Java 分析器。到目前为止,我一直在尝试 PMD,并且运气不错。
There are several good analyzers for Java. I have been playing around with PMD so far and have had decent luck with it.
您可能还想尝试 FindBugs。对解决这些问题和许多其他问题有很大帮助。
You may also want to try FindBugs. Helps a lot with these and many other issues.
Doxygen 可以提供帮助,特别是在类和方法之间的依赖关系的图形表示方面。
Doxygen can help, especially with graphical representations of the dependencies between classes and methods.
看看 Apache Ivy
Have a look at Apache Ivy