Java包循环检测:如何找到涉及的具体类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/62276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java package cycle detection: how do I find the specific classes involved?
提问by VonC
What tool would you recommend to detect Java package cyclic dependencies, knowing that the goal is to list explicitly the specific classes involved in the detected 'across-packages cycle'?
你会推荐什么工具来检测Java 包循环依赖,知道目标是明确列出检测到的“跨包循环”中涉及的特定类?
I know about classycleand JDepend, but they both fail to list the classes involved in a cyclic package dependency. Metricshas an interesting graphical representation of cycles, but it is again limited to packages, and quite difficult to read sometime.
我知道classycle和JDepend,但它们都没有列出循环包依赖项中涉及的类。Metrics有一个有趣的周期图形表示,但它再次仅限于包,有时很难阅读。
I am getting tired to get a:
我越来越累了:
" you have a package cycle dependency between those 3 packages
you have xxx classes in each
good luck finding the right classes and break this cycle "
“你在这 3 个包之间有一个包循环依赖,
你有 xxx 个类,
祝你好运找到正确的类并打破这个循环”
Do you know any tool that takes the extra step to actually explain to you why the cycle is detected (i.e. 'list the involved classes')?
你知道有什么工具可以采取额外的步骤来实际向你解释为什么检测到循环(即“列出所涉及的类”)?
Riiight... Time to proclaim the results:
Riiight... 是时候宣布结果了:
@l7010.de: Thank you for the effort. I will vote you up (when I will have enough rep), especially for the 'CAP' answer... but CAP is dead in the water and no longer compatible with my Eclipse 3.4. The rest is commercial and I look only for freeware.
@l7010.de:感谢您的努力。我会投票给你(当我有足够的代表时),特别是对于“CAP”的回答……但是 CAP 已经死了,不再与我的 Eclipse 3.4 兼容。其余的是商业的,我只寻找免费软件。
@daniel6651: Thank you but, as said, freeware only (sorry to not have mentioned it in the first place).
@daniel6651:谢谢,但如前所述,仅限免费软件(抱歉,一开始没有提到它)。
@izb as a frequent user of findbugs (using the latest 1.3.5 right now), I am one click away to accept your answer... if you could explain to me what option there is to activate for findbug to detect any cycle. That feature is only mentioned for the 0.8.7 version in passing(look for 'New Style detector to find circular dependencies between classes'), and I am not able to test it. Update: It works now, and I had an old findbugs configuration file in which that option was not activated. I still like CADthough ;)
@izb 作为 findbugs 的频繁用户(现在使用最新的 1.3.5),我只需单击一下即可接受您的答案......如果您能向我解释有哪些选项可以激活 findbug 以检测任何周期。该功能仅在0.8.7 版本中提及(查找“新样式检测器以查找类之间的循环依赖关系”),我无法对其进行测试。更新:它现在可以工作了,我有一个旧的 findbugs 配置文件,其中该选项未激活。不过我还是喜欢CAD;)
THE ANSWER is... see my own (second) answer below
答案是……见下面我自己的(第二个)答案
采纳答案by izb
Findbugs can detect circular class dependencies and has an Eclipse plugin too.
Findbugs 可以检测循环类依赖项,并且也有一个 Eclipse 插件。
回答by squiddle
回答by squiddle
There is also Structure101which should do this.
还有Structure101应该这样做。
回答by squiddle
And you can use the open source tool CAPwhich is an Eclipse plugin.
您可以使用开源工具CAP,它是一个 Eclipse 插件。
CAP has a graphical package view which will show you the lines to the classes so after some clicks (depending on the size of the circle) you will find the culprit.
CAP 有一个图形包视图,它会向您显示类的线条,因此在单击几下(取决于圆圈的大小)后,您将找到罪魁祸首。
回答by izb
There are some commercial tools: Structure101 & Lattix which can be used for this purpose.
有一些商业工具:Structure101 & Lattix 可用于此目的。
回答by VonC
A first possible answer is... not pretty. But it does begin to do what I am after (a better solution is presented below).
第一个可能的答案是……不漂亮。但它确实开始做我所追求的(下面提供了更好的解决方案)。
Dependency Finder! Download it, unzip it.
It is not the most modern or active project ever, but if you edit [Dependency Finder]/bin/DependencyFinder.bat, add its path for DEFAULT_DEPENDENCYFINDER_HOME, set a JAVA_HOME, you can launch it.
它不是有史以来最现代或最活跃的项目,但是如果您编辑 [Dependency Finder]/bin/DependencyFinder.bat,为 DEFAULT_DEPENDENCYFINDER_HOME 添加其路径,设置一个 JAVA_HOME,您可以启动它。
Then you click on the 'Extract' button (CTRL-E - first button), enter your classes path, and let it scan away.
然后单击“提取”按钮(CTRL-E - 第一个按钮),输入您的类路径,然后让它扫描。
The tricky part is to click exactly the right set of 'programming elements' and 'closures' items, in order to not been swamped by the level of details in the result.
棘手的部分是准确单击正确的“编程元素”和“闭包”项集,以免被结果中的细节级别所淹没。
- Select only 'classes' in the left side ('programming elements').
- Select only 'classes' in the right side ('closures').
- Add "/javax?./,/org./,/sun./" as exclusion pattern (for both programming elements and closures).
- Click on the wheels (last button - Compute all - Ctrl+ A).
- 仅选择左侧的“类”(“编程元素”)。
- 仅选择右侧的“类”(“闭包”)。
- 添加“/javax?./,/org./,/sun./”作为排除模式(对于编程元素和闭包)。
- 单击轮子(最后一个按钮 - 全部计算 - Ctrl+ A)。
And here you go.
给你。
Whenever you see '<->', you have got yourself a nice cyclic dependency. (If you select 'features' on the 'closure' side, you can even know what function does trigger the cycle - awesome.)
每当您看到 '<->' 时,您就有了一个很好的循环依赖项。(如果您在“关闭”一侧选择“功能”,您甚至可以知道触发循环的功能 - 太棒了。)
I am ready to test any other suggestions.
我准备测试任何其他建议。
回答by VonC
Well... after testing DepFinder presented above, it turns out it is great for a quick detection of simple dependencies, but it does not scale well with the number of classes...
嗯……在测试了上面介绍的 DepFinder之后,结果证明它非常适合快速检测简单的依赖关系,但它不能很好地扩展类的数量……
So the REAL ACTUAL ANSWER is: CDA - Class Dependency Analyzer
所以真正的实际答案是: CDA - 类依赖分析器
It is fast, up-to-date, easy to use and provides with graphical representation of classes and their circular dependencies. A dream come true ;)
它快速、最新、易于使用,并提供类及其循环依赖的图形表示。梦想成真 ;)
You have to create a workset in which you enter only the directory of your classes (.class) (no need to have a complete classpath)
The option "Detect circular dependencies - ALT-C" works as advertise and does not take 100% of the CPU for hours to analyze my 468 classes.
Note: to refresh a workspace, you need to open it again(!), in order to trigger a new scan of your classes.
您必须创建一个工作集,在其中您只输入类的目录 (.class)(无需具有完整的类路径)
选项“检测循环依赖 - ALT- C”作为广告工作,不会占用 100% 的 CPU几个小时来分析我的 468 个课程。
注意:要刷新工作区,您需要再次打开它(!),以触发对您的类的新扫描。
回答by Stijn Van Bael
We use Sonarto detect package cycles. It draws a nice graph of the dependencies and shows which ones go in the wrong direction. You can even navigate to the source where the dependency is used.
我们使用声纳来检测包装周期。它绘制了一个很好的依赖关系图,并显示了哪些方向错误。您甚至可以导航到使用依赖项的源。
See http://www.sonarsource.org/fight-back-design-erosion-by-breaking-cycles-with-sonar/
见http://www.sonarsource.org/fight-back-design-erosion-by-break-cycles-with-sonar/
回答by henry
Highwheeldetects class and package cycles and reports the source of the dependencies down to the class/method/field level indicating the type of the relationship (inheritance, composition, part of a method signature, etc.).
Highwheel检测类和包循环,并将依赖源报告到类/方法/字段级别,指示关系的类型(继承、组合、方法签名的一部分等)。
It also breaks large cycles down into their sub-elements which can be understood/tackled individually.
它还将大循环分解为可以单独理解/处理的子元素。
The output is HTML with embedded SVG content that requires a modern browser.
输出是带有嵌入式 SVG 内容的 HTML,需要现代浏览器。