2016-04-11 37 views
3

在我正在處理的項目中,我們有很多自定義的Checkstyle檢查。例如,我們使用RegexpSingleline模塊排除所有類型的東西。我們的CheckStyle規則是這樣的:重複使用checkstyle檢查來定義基於xml的自定義模塊

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE module PUBLIC 
     "-//Puppy Crawl//DTD Check Configuration 1.3//EN" 
     "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> 
<module name="Checker"> 

    <!-- Disable this --> 
    <module name="RegexpSingleline"> 
     ... 
    </module> 

    <!-- Disable that --> 
    <module name="RegexpSingleline"> 
     ... 
    </module> 

    ... 
</module> 

不幸的是,這意味着,當我們要禁用一個檢查一個文件,我們通常最終會禁用很多檢查,例如,所有那些基於RegexpSingleline 。

我本來期望,但似乎無法找到的可能性來定義「複合」的檢查,是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE module PUBLIC 
     "-//Puppy Crawl//DTD Check Configuration 1.3//EN" 
     "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> 
<module name="Checker"> 

    <!-- Disable this --> 
    <myCheck name="DisableNastyThing1"> 
     <module name="RegexpSingleline"> 
      ... 
     </module> 
    </myCheck> 

    <!-- Disable that --> 
    <myCheck name="DisableNastyThing2"> 
     <module name="RegexpSingleline"> 
      ... 
     </module> 
    </myCheck> 

    ... 
</module> 

抑制DisableNastyThing1DisableNastyThing2將工作完全像抑制的模塊。換句話說,myCheck XML元素名稱將表示檢查未映射到實現抽象檢查的實際Java類的事實,而是映射到執行嵌套檢查的通用檢查。

是否有已經在Checkstyle的這樣一個概念,我沒能找到它還是什麼?在suppressions.xml

<!-- Disable this --> 
<module name="RegexpSingleline"> 
    <property name="id" value="DisableNastyThing1"/> 
    ... 
</module> 

<!-- Disable that --> 
<module name="RegexpSingleline"> 
    <property name="id" value="DisableNastyThing2"/> 
    ... 
</module> 

然後,你可以參考的ID:

回答

0

您可以在id屬性添加到每個檢查,像這樣

<suppress files="some/files/" id="DisableNastyThing1" /> 
<suppress files="some/other/files/" id="DisableNastyThing2" /> 

在通過這種方式,您只能抑制一些RegexpSingleLine實例,而不是所有實例。

但是,不可能定義檢查實例組。換句話說,沒有用於將DisableNastyThing1DisableNastyThing2分組爲DisableNastyThings的語法,並且在抑制中引用該語法。