Cycle Finder 工具

用量

以下的簡單範例將具有 Foo 和 Bar 兩個類別循環。

cat Foo.java
package mypkg;
public class Foo {
  Bar myBar;
}

cat Bar.java
package mypkg;
public class Bar {
  Foo myFoo;
}

cycle_finder Foo.java Bar.java
acceptAST: mypkg/Bar.java
acceptAST: mypkg/Foo.java

***** Found reference cycle *****
Bar -> (field myFoo with type Foo)
Foo -> (field myBar with type Bar)
----- Full Types -----
Lmypkg/Bar;
Lmypkg/Foo;

1 CYCLES FOUND.

在輸出結果中,您會先看到每個剖析的 Java 檔案 acceptAST。這項記錄僅為參考資訊。

系統會針對每個週期,顯示兩份清單。第一個清單包含循環說明。每一行都列出參考圖中的邊緣。邊緣說明會顯示來源類型,然後說明起點類型如何參照目標類型。

第二個清單 Full Types 下方顯示週期內每種類型的專屬類型鍵。它提供了每種型別的完整套件,因此非常實用。

隱藏清單

某些偵測到的週期不需要任何修正行動。這可能是因為該週期包含的長期物件不需要取消分配。或者,此工具可能會根據特定領域類型偵測理論循環,其中相關物件永遠不會形成循環。在這些情況下,我們可以使用隱藏清單檔案不要回報這些循環。

這項工具接受使用 --suppress-list 選項禁止清單的檔案。隱藏清單檔案是包含單行項目的純文字檔案。隱藏清單項目可採用 4 種形式中的其中一種,如以下範例所示。建議您在新增受限清單項目時,盡可能具體,以免干擾合法的循環。你可以使用「#」新增註解字元。

# Specifies that "fieldA" in ClassA will not refer to any object that is a subtype of ClassB.
FIELD my.pkg.ClassA.fieldA my.pkg.ClassB

# Suppresses all cycles containing "fieldA" in ClassA.
FIELD my.pkg.ClassA.fieldA

# Suppresses all cycles containing any field of ClassA.
TYPE my.pkg.ClassA

# Suppress all cycles containing the Inner's outer reference to ClassA.
OUTER my.pkg.ClassA.Inner

# Suppress all cycles containing the outer reference from an anonymous class declared in myMethod.
OUTER my.pkg.ClassA.myMethod.$

# Suppresses all cycles containing any type in package "my.pkg".
NAMESPACE my.pkg