如何在java项目中查找未使用/死代码 [英] How to find unused/dead code in java projects

查看:167
本文介绍了如何在java项目中查找未使用/死代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您使用哪些工具在大型Java项目中查找未使用/死代码?我们的产品已经开发了几年,手动检测不再使用的代码变得非常困难。但我们会尝试尽可能多地删除未使用的代码。

What tools do you use to find unused/dead code in large java projects? Our product has been in development for some years, and it is getting very hard to manually detect code that is no longer in use. We do however try to delete as much unused code as possible.

对于一般策略/技术(特定工具除外)的建议也不胜感激。

Suggestions for general strategies/techniques (other than specific tools) are also appreciated.

编辑请注意,我们已经使用了代码覆盖率工具(Clover,IntelliJ),但这些工具没什么用处。死代码仍然有单元测试,并显示为覆盖。我想一个理想的工具可以识别出具有很少其他代码的代码集,具体取决于它,允许docues手动检查。

Note that we already use code coverage tools (Clover, IntelliJ), but these are of little help. Dead code still has unit tests, and shows up as covered. I guess an ideal tool would identify clusters of code which have very little other code depending on it, allowing for docues manual inspection.

推荐答案

我会检测正在运行的系统以保存代码使用日志,然后开始检查未使用数月或数年的代码。

I would instrument the running system to keep logs of code usage, and then start inspecting code that is not used for months or years.

例如,如果您对未使用的类,可以检测所有类以在创建实例时进行记录。然后一个小脚本可以将这些日志与完整的类列表进行比较,以找到未使用的类。

For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.

当然,如果你进入方法级别,你应该保持性能心神。例如,这些方法只能记录它们的首次使用。我不知道如何用Java做得最好。我们在Smalltalk中完成了这项工作,Smalltalk是一种动态语言,因此允许在运行时进行代码修改。我们使用日志记录调用来检测所有方法,并在首次记录方法后卸载日志记录代码,因此在一段时间后不再出现性能损失。也许类似的事情可以在Java中使用静态布尔标志...

Of course, if you go at the method level you should keep performance in mind. For example, the methods could only log their first use. I dont know how this is best done in Java. We have done this in Smalltalk, which is a dynamic language and thus allows for code modification at runtime. We instrument all methods with a logging call and uninstall the logging code after a method has been logged for the first time, thus after some time no more performance penalties occur. Maybe a similar thing can be done in Java with static boolean flags...

这篇关于如何在java项目中查找未使用/死代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆