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

查看:61
本文介绍了如何在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),但这些工具帮助不大.死代码仍然有单元测试,并显示为覆盖.我想一个理想的工具可以识别几乎没有其他代码依赖的代码集群,从而允许手动检查文档.

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 中做到了这一点,它是一种动态语言,因此允许在运行时修改代码.我们使用日志记录调用检测所有方法,并在第一次记录方法后卸载日志记录代码,因此在一段时间后不再发生性能损失.也许可以在 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天全站免登陆