天天看點

如何在Java項目中查找未使用/無效的代碼

本文翻譯自:How to find unused/dead code in java projects

What tools do you use to find unused/dead code in large java projects?

您使用什麼工具在大型Java項目中查找未使用/無效的代碼?

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.

對于一般政策/技術(除特定工具之外)的建議也将受到贊賞。

Edit: Note that we already use code coverage tools (Clover, IntelliJ), but these are of little help.

編輯:請注意,我們已經使用了代碼覆寫率工具(Clover,IntelliJ),但是這些幫助不大。

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.

我猜一個理想的工具可以識别出幾乎沒有其他代碼依賴的代碼叢集,進而可以對文檔進行手動檢查。

#1樓

參考:https://stackoom.com/question/gHn/如何在Java項目中查找未使用-無效的代碼

#2樓

DCD is not a plugin for some IDE but can be run from ant or standalone.

DCD不是某些IDE的插件,但可以從ant或獨立運作。

It looks like a static tool and it can do what PMD and FindBugs can't .

它看起來像一個靜态工具, 可以完成PMD和FindBugs無法完成的工作 。

I will try it.

我會試試。

PS As mentioned in a comment below, the Project lives now in GitHub .

PS如下面的評論中所述,該項目現在位于GitHub中 。

#3樓

Eclipse can show/highlight code that can't be reached.

Eclipse可以顯示/突出顯示無法通路的代碼。

JUnit can show you code coverage, but you'd need some tests and have to decide if the relevant test is missing or the code is really unused.

JUnit可以向您顯示代碼覆寫率,但是您需要進行一些測試,并且必須确定是否缺少相關測試或代碼是否确實未使用。

#4樓

There are tools which profile code and provide code coverage data.

有一些工具可以分析代碼并提供代碼覆寫率資料。

This lets you see (as code is run) how much of it is being called.

這樣一來,您就可以檢視(在運作代碼時)調用了多少代碼。

You can get any of these tools to find out how much orphan code you have.

您可以使用這些工具中的任何一種來查明您有多少孤立代碼。

#5樓

In theory, you can't deterministically find unused code.

從理論上講,您無法确定性地找到未使用的代碼。

Theres a mathematical proof of this (well, this is a special case of a more general theorem).

有一個數學上的證明(嗯,這是一個更一般的定理的特例)。

If you're curious, look up the Halting Problem.

如果您好奇,請查找“停止問題”。

This can manifest itself in Java code in many ways:

這可以通過多種方式在Java代碼中展現出來:
  • Loading classes based on user input, config files, database entries, etc; 根據使用者輸入,配置檔案,資料庫條目等加載類;
  • Loading external code; 加載外部代碼;
  • Passing object trees to third party libraries; 将對象樹傳遞給第三方庫;
  • etc. 等等

That being said, I use IDEA IntelliJ as my IDE of choice and it has extensive analysis tools for findign dependencies between modules, unused methods, unused members, unused classes, etc. Its quite intelligent too like a private method that isn't called is tagged unused but a public method requires more extensive analysis.

就是說,我将IDEA IntelliJ用作我的首選IDE,它具有用于子產品,未使用的方法,未使用的成員,未使用的類等之間的依賴關系的廣泛分析工具。它非常智能,就像未調用的私有方法是标記為未使用,但公共方法需要更廣泛的分析。

#6樓

We've started to use Find Bugs to help identify some of the funk in our codebase's target-rich environment for refactorings.

我們已開始使用“ 查找錯誤”來幫助識别代碼庫的目标豐富的環境中的某些功能,以進行重構。

I would also consider Structure 101 to identify spots in your codebase's architecture that are too complicated, so you know where the real swamps are.

我還将考慮使用結構101來識别代碼庫體系結構中過于複雜的位置,以便您知道真正的沼澤在哪裡。