使用FindBug對項目的代碼進行了檢查html
從官方的說明中獲得了啓發。官網連接以下:
http://findbugs.sourceforge.net/manual/filter.htmljava
在這個連接了它講解了FindBug的filter使用:正則表達式
將相應的規則做成一份 XML 文檔,而後,在配置頁面中導入這份配置文檔便可。api
再次運行FindBug,果真,不出所料,全部的scala 文件被排除了ide
從概念上講,過濾器將針對一組條件的錯誤實例進行匹配。經過定義過濾器,您能夠選擇錯誤實例進行特殊處理; 例如,將其排除或包含在報告中。oop
過濾器文件是具備頂級元素的XML文檔,FindBugsFilter
其中有一些Match
元素做爲子元素。每一個Match
元素表示一個應用於生成的bug實例的謂詞。一般,將使用過濾器來排除錯誤實例。例如:測試
$ findbugs -textui -exclude myExcludeFilter.xml myApp.jar
可是,也可使用篩選器來選擇錯誤實例來專門報告:ui
$findbugs -textui -include myIncludeFilter.xml myApp.jar
Match
元素包含子句,它們是謂詞的鏈接詞。換句話說,每一個孩子必須對謂詞是真實的。spa
此元素指定要匹配的特定錯誤模式。該pattern
屬性是以逗號分隔的錯誤模式類型列表。您能夠經過查看-xml 輸出選項( 元素的type
屬性BugInstance
)或錯誤描述文檔生成的輸出來查找特定警告的錯誤模式類型。.net
要進行更粗粒度的匹配,請使用code
屬性。它須要以逗號分隔的bug縮寫列表。對於大多數粗粒匹配使用 category
attriute,即須要bug目錄名稱的逗號分隔的列表: CORRECTNESS
,MT_CORRECTNESS
, BAD_PRACTICICE
,PERFORMANCE
,STYLE
。
若是在同一<Bug>
元素上指定了上述多個屬性,則將 匹配與指定模式名稱或縮寫或類別之一匹配的全部錯誤模式。
做爲向後兼容的措施,<BugPattern>
而且 <BugCode>
元件能夠被用來代替 <Bug>
元素。其中每一個都使用一個 name
屬性來指定接受的值列表。在未來的版本中可能會刪除對這些元素的支持。
此元素匹配具備特定錯誤信心的警告。該value
屬性應爲整數值:1表示高置信度警告,2表示匹配正常置信度警告,3表示匹配低置信度警告。<Confidence>在2.0.0版本中替換了<Priority>。
相同<Confidence>
,爲了向後兼容而存在。
此元素匹配具備特定錯誤等級的警告。該value
屬性應該是1到20之間的整數值,其中1到4是最可怕的,5到9個可怕的,10到14個使人煩惱的,以及15到20個涉及錯誤的。
此元素匹配與使用name
attribute 指定的包中的類關聯的警告。不包括嵌套包(沿着Java import語句行)。可是,使用正則表達式名稱匹配能夠輕鬆實現匹配多個包。
此元素匹配與特定類關聯的警告。該 name
屬性用於指定類名的精確或正則表達式匹配模式。該role
屬性是類角色。
做爲向後兼容性度量,您可使用元素class
上的屬性Match
來指定正確的類名稱或classregex
屬性,以指定與類名稱匹配的正則表達式,而不是此類型 的元素。
若是Match
元素既不包含Class
元素也不包含class
/ classregex
屬性,則謂詞將應用於全部類。這樣的謂詞可能會匹配比您想要的更多的bug實例,除非使用適當的方法或字段謂詞進一步細化。
此元素匹配與特定源文件關聯的警告。該 name
屬性用於指定源文件名的精確或正則表達式匹配模式。
此元素指定方法。的name
用於指定對方法名的確切或正則表達式匹配的圖案。該params
屬性是以逗號分隔的方法參數類型列表。該returns
屬性是方法的返回類型。該role
屬性是方法角色。在params
和中returns
,類名必須是徹底限定的。(例如,「java.lang.String」而不只僅是「String」。)若是指定了後一個屬性之一,則建立方法簽名須要另外一個屬性。請注意,您能夠提供兩種name
屬性或者params
與returns
屬性或他們三個。這樣您就能夠提供各類基於名稱和簽名的匹配。
該元素指定一個字段。該name
屬性用於指定字段名稱的精確或正則表達式匹配模式。您還能夠根據其簽名 - 使用type
屬性過濾字段以指定字段的徹底限定類型。您能夠指定eiter或這兩個屬性以執行基於名稱/簽名的匹配。該role
屬性是字段角色。
這個元素指定一個局部變量。該name
屬性用於指定局部變量名稱的精確或正則表達式匹配模式。局部變量是在方法中定義的變量。
該元素將Match
子句合併爲分離符。也就是說,您能夠將兩個 Method
元素放在一個Or
子句中以匹配任一方法。
這個元素結合Match
了必須評估爲真的子句。即,您能夠在子句中放置 Bug
和Confidence
元素,And
以便僅在給定的置信度下匹配特定的錯誤。
<Bug> <Confidence> <Priority> <Rank> <Package> <Class> <Source> <Method> <Field> <Local> <Or> <And> <Not>
這個元素顛倒了包含的孩子Match
。即,您能夠Bug
在Not
子句中放置一個 元素,以匹配排除給定的一個錯誤。
若是name
屬性Class
,Source
, Method
或Field
用〜字符的屬性內容的其他部分被解釋爲是對有問題的Java元素的名稱相匹配的Java正則表達式開始。
請注意,該模式與整個元素名稱匹配,所以須要在模式開始和/或結束時使用。*子句來執行子字符串匹配。
請參閱java.util.regex.Pattern
模式語法的文檔。
Match
子句只能匹配實際包含在bug實例中的信息。每一個bug實例都有一個類,因此一般狀況下,按類排除錯誤將起做用。
一些bug實例有兩個(或更多)類。例如,DE(丟棄的異常)錯誤會報告包含發生已刪除異常的方法的類以及表示已丟棄異常的類型的類。只有第一個(主要)類與Match
子句匹配。所以,例如,若是要抑制類「com.foobar.A」和「com.foobar.B」的IC(初始化循環)報告,則可使用兩個Match
子句:
<Match> <Class name =「com.foobar.A」/> <Bug code =「IC」/> </ Match> <Match> <Class name =「com.foobar.B」/> <Bug code =「 IC「/> </ Match>
經過明確地匹配兩個類,確保不管在循環中涉及的哪一個類剛好在bug實例中首先列出,均可以確保IC bug的實例匹配。(固然,這種方法可能會意外地壓制包含「com.foobar.A」或「com.foobar.B」和第三類的循環。)
許多類型的錯誤報告了它們出現的方法。對於這些錯誤實例,能夠將Method
子句放在Match
元素中,而且它們應該按預期工做。
1.匹配課程的全部錯誤報告。
<Match> <Class name="com.foobar.MyClass" /> </Match>
2.經過指定他們的縮寫來匹配某個班級的某些考試。
<Match> <Class name="com.foobar.MyClass"/ > <Bug code="DE,UrF,SIC" /> </Match>
3.經過指定縮寫來匹配全部類別的某些測試。
<Match> <Bug code="DE,UrF,SIC" /> </Match>
4.經過指定類別來匹配全部類的某些測試。
<Match> <Bug category="PERFORMANCE" /> </Match>
5.經過它們的縮寫匹配一個類的指定方法中的錯誤類型。
<Match> <Class name="com.foobar.MyClass" /> <Or> <Method name="frob" params="int,java.lang.String" returns="void" /> <Method name="blat" params="" returns="boolean" /> </Or> <Bug code="DC" /> </Match>
6.匹配特定方法中的特定錯誤模式。
<!-- A method with an open stream false positive. --> <Match> <Class name="com.foobar.MyClass" /> <Method name="writeDataToFile" /> <Bug pattern="OS_OPEN_STREAM" /> </Match>
7.以特定的方法將特定的錯誤模式與給定的優先級進行匹配。
<!-- A method with a dead local store false positive (medium priority). --> <Match> <Class name="com.foobar.MyClass" /> <Method name="someMethod" /> <Bug pattern="DLS_DEAD_LOCAL_STORE" /> <Priority value="2" /> </Match>
8.匹配由AspectJ編譯器引入的小錯誤(除非您是AspectJ開發人員,不然您可能對這些錯誤不感興趣)。
<Match> <Class name="~.*\$AjcClosure\d+" /> <Bug pattern="DLS_DEAD_LOCAL_STORE" /> <Method name="run" /> </Match> <Match> <Bug pattern="UUF_UNUSED_FIELD" /> <Field name="~ajc\$.*" /> </Match>
9.在代碼庫的特定部分中匹配錯誤
<!-- match unused fields warnings in Messages classes in all packages --> <Match> <Class name="~.*\.Messages" /> <Bug code="UUF" /> </Match> <!-- match mutable statics warnings in all internal packages --> <Match> <Package name="~.*\.internal" /> <Bug code="MS" /> </Match> <!-- match anonymoous inner classes warnings in ui package hierarchy --> <Match> <Package name="~com\.foobar\.fooproject\.ui.*" /> <Bug pattern="SIC_INNER_SHOULD_BE_STATIC_ANON" /> </Match>
10.使用特定簽名匹配字段或方法上的錯誤
<!-- match System.exit(...) usage warnings in void main(String[]) methods in all classes --> <Match> <Method returns="void" name="main" params="java.lang.String[]" /> <Bug pattern="DM_EXIT" /> </Match> <!-- match UuF warnings on fields of type com.foobar.DebugInfo on all classes --> <Match> <Field type="com.foobar.DebugInfo" /> <Bug code="UuF" /> </Match>
11.使用Not過濾器運算符來匹配錯誤
<!-- ignore all bugs in test classes, except for those bugs specifically relating to JUnit tests --> <!-- i.e. filter bug if ( classIsJUnitTest && ! bugIsRelatedToJUnit ) --> <Match> <!-- the Match filter is equivalent to a logical 'And' --> <Class name="~.*\.*Test" /> <!-- test classes are suffixed by 'Test' --> <Not> <Bug code="IJU" /> <!-- 'IJU' is the code for bugs related to JUnit test code --> </Not> </Match>
12.徹底排除過濾器文件,以匹配從Groovy源文件生成的全部類。
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <Source name="~.*\.groovy" /> </Match> </FindBugsFilter>
<FindBugsFilter> <Match> <Class name="com.foobar.ClassNotToBeAnalyzed" /> </Match> <Match> <Class name="com.foobar.ClassWithSomeBugsMatched" /> <Bug code="DE,UrF,SIC" /> </Match> <!-- Match all XYZ violations. --> <Match> <Bug code="XYZ" /> </Match> <!-- Match all doublecheck violations in these methods of "AnotherClass". --> <Match> <Class name="com.foobar.AnotherClass" /> <Or> <Method name="nonOverloadedMethod" /> <Method name="frob" params="int,java.lang.String" returns="void" /> <Method name="blat" params="" returns="boolean" /> </Or> <Bug code="DC" /> </Match> <!-- A method with a dead local store false positive (medium priority). --> <Match> <Class name="com.foobar.MyClass" /> <Method name="someMethod" /> <Bug pattern="DLS_DEAD_LOCAL_STORE" /> <Priority value="2" /> </Match> <!-- All bugs in test classes, except for JUnit-specific bugs --> <Match> <Class name="~.*\.*Test" /> <Not> <Bug code="IJU" /> </Not> </Match> </FindBugsFilter>
來源: http://findbugs.sourceforge.net/manual/filter.html#d0e1930