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