使用注释@SuppressWarnings忽略Checkstyle警告

时间:2014-12-29 11:58:10

标签: java eclipse checkstyle

我尝试通过带有注释@SuppressWarnings的checkstyle在eclipse中忽略或禁用一些无用的警告 How to disable a particular checkstyle rule for a particular line of code?

但这对我不起作用。

这是checkstyle.xml

<module name="Checker">
  <property name="severity" value="warning"/>
  <module name="SuppressWarningsFilter"/>
  <module name="TreeWalker">
    <property name="tabWidth" value="4"/>
    <module name="FileContentsHolder"/>
    <module name="SuppressWarningsHolder"/>
    <module name="CyclomaticComplexity"/>
    ...

这里是java代码:

@SuppressWarnings("checkstyle:CyclomaticComplexity")
public void doSomeThing() {
 ...
}

同时将警告值设置为“全部”@SuppressWarnings("all")@SuppressWarnings("CyclomaticComplexity")@SuppressWarnings("cyclomaticcomplexity")没有任何结果。

checkstyle的文档很差。一些想法?

3 个答案:

答案 0 :(得分:61)

:一种。抑制警告过滤器

使用checkstyle 6.5.0我可以使用@SuppressWarnings。请考虑以下几点:

  • 必须在Checkstyle设置中启用启用
  • 在SuppressWarnings注释的标记中,您必须使用全部小写中的checkstyle模块的名称。
  • 可选 前缀&#34; checkstyle:&#34;可以在标签中使用。
  • 如果注释无法按预期工作,请尝试更改其位置。需要将MagicNumber模块的注释放在方法之前。某些注释必须直接位于显示问题的位置,而其他注释必须位于类定义的前面。

checkstyle模块的一些示例&#34; MagicNumber&#34;:

<强>使用:

@SuppressWarnings("checkstyle:magicnumber")
public example(){
    int g = 5;
}

@SuppressWarnings("magicnumber")
public example(){
    int g = 5;
}

不起作用:

@SuppressWarnings("MagicNumber")
public example(){
    int g = 5;
}

@SuppressWarnings("magicNumber")
public example(){        
    int g = 5;
}

public example(){
    @SuppressWarnings("magicnumber")
    int g = 5;
}

进一步说明

  • 我收到了警告不支持的抑制警告,我在Eclipse偏好设置中禁用了Java =&gt;编译器=&gt;错误/警告=&gt;注释=&gt;&#中未处理的令牌39; @ SuppressWarnings&#39;:忽略

  • 相应的checkstyle模块的名称(在xml文件中定义)未显示在违规消息中,当鼠标悬停在代码问题上时会弹出。我在违规消息&#34;中启用了选项&#34; 包含模块ID (如果可用)并手动更改所有模块ID与xml文件中相应的模块名称相同,但小写。例如,有一个模块&lt; name =&#34; AnonInnerLength&#34;&gt;它在Eclipse checkstyle设置中显示为&#34;匿名内部类长度&#34;。该模块没有模块ID。我将模块ID更改为 checkstyle:anoninnerlength ,以便我的同事更容易抑制警告:

    &lt; module name =&#34; AnonInnerLength&#34;&gt;
      &lt; property name =&#34; id&#34;值=&#34; CheckStyle的:anoninnerlength&#34; /&GT;
    &LT;模块&GT;

  • 我使用前缀&#34; checkstyle:&#34;在模块ID以及SuppressWarnings标记中,使其明确表示警告不是标准的Eclipse警告&#34;。 (可选前缀&#34; checkstyle:&#34;已经可以在标记中使用而不更改模块ID。但是,前缀不会显示在违规消息中。将其包含在模块ID中会使消息更多透明并激励我的同事在标签中包含前缀。)

<强> B中。抑制评论过滤器

  • checkstyle过滤器Filters =&gt;抑制注释过滤器使用在xml文件中指定的模块名称。
  • 如果使用前缀&#34; checkstyle:&#34;,模块名称也可以小写使用。

<强>使用:

//CHECKSTYLE:OFF: checkstyle:magicnumber        
public example(){
    int g = 5;
}   
//CHECKSTYLE:ON: checkstyle:magicnumber

//CHECKSTYLE:OFF: MagicNumber           
public example(){
    int g = 5;
}   
//CHECKSTYLE:ON: MagicNumber

不起作用:

//CHECKSTYLE:OFF: magicnumber           
public example(){
    int g = 5;
}   
//CHECKSTYLE:ON: magicnumber

<强>℃。 checkstyle settings.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">


<!--
    Checkstyle-Configuration with module ids that can be used as    
    tags in @SuppressWarnings
-->

<module name="Checker">

  <property name="severity" value="warning"/>

  <module name="TreeWalker">
    <property name="tabWidth" value="4"/>
    <module name="FileContentsHolder"/>
    <module name="SuppressWarningsHolder"/>

    <module name="JavadocMethod">
      <property name="id" value="checkstyle:javadocmethod"/>   
      <property name="severity" value="ignore"/>
      <property name="allowMissingParamTags" value="true"/>
      <property name="allowMissingThrowsTags" value="true"/>
      <property name="allowMissingReturnTag" value="true"/>
      <property name="suppressLoadErrors" value="true"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="JavadocType">
      <property name="id" value="checkstyle:javadoctype"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="JavadocVariable">
      <property name="id" value="checkstyle:javadocvariable"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="JavadocStyle">
      <property name="id" value="checkstyle:javadocstyle"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="ConstantName">
      <property name="id" value="checkstyle:constantname"/>
    </module>

    <module name="LocalFinalVariableName">
      <property name="id" value="checkstyle:localfinalvariablename"/>
    </module>

    <module name="LocalVariableName">
      <property name="id" value="checkstyle:localvariablename"/>
    </module>

    <module name="MemberName">
      <property name="id" value="checkstyle:membername"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="MethodName">
      <property name="id" value="checkstyle:methodname"/>
    </module>

    <module name="PackageName">
      <property name="id" value="checkstyle:packagename"/>
    </module>

    <module name="ParameterName">
      <property name="id" value="checkstyle:parametername"/>
    </module>

    <module name="StaticVariableName">
      <property name="id" value="checkstyle:staticvariablename"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="TypeName">
      <property name="id" value="checkstyle:typename"/>
    </module>

    <module name="AvoidStarImport">
      <property name="id" value="checkstyle:avoidstarimport"/>
    </module>

    <module name="IllegalImport">
      <property name="id" value="checkstyle:illegalimport"/>
    </module>

    <module name="RedundantImport">
      <property name="id" value="checkstyle:redundantimport"/>
    </module>

    <module name="UnusedImports">
      <property name="id" value="checkstyle:unusedimports"/>
    </module>

    <module name="ParameterNumber">
      <property name="id" value="checkstyle:parameternumber"/>
    </module>

    <module name="EmptyForIteratorPad">
      <property name="id" value="checkstyle:emptyforiteratorpad"/>
    </module>

    <module name="MethodParamPad">
      <property name="id" value="checkstyle:methodparampad"/>
    </module>

    <module name="NoWhitespaceAfter">
      <property name="id" value="checkstyle:nowhitespaceafter"/>
      <property name="tokens" value="BNOT,DEC,DOT,INC,LNOT,UNARY_MINUS,UNARY_PLUS"/>
    </module>

    <module name="NoWhitespaceBefore">
      <property name="id" value="checkstyle:nowhitespacebefore"/>
    </module>

    <module name="OperatorWrap">
      <property name="id" value="checkstyle:operatorwrap"/>
      <property name="option" value="eol"/>
    </module>

    <module name="ParenPad">
      <property name="id" value="checkstyle:parenpad"/>
    </module>

    <module name="TypecastParenPad">
      <property name="id" value="checkstyle:typecastparenpad"/>
    </module>

    <module name="WhitespaceAfter">
      <property name="id" value="checkstyle:whitespaceafter"/>
    </module>

    <module name="WhitespaceAround">
      <property name="id" value="checkstyle:whitespacearound"/>
    </module>

    <module name="ModifierOrder">
      <property name="id" value="checkstyle:modifierorder"/>
    </module>

    <module name="RedundantModifier">
      <property name="id" value="checkstyle:redundantmodifier"/>
    </module>

    <module name="LeftCurly">
      <property name="id" value="checkstyle:leftcurly"/>
    </module>

    <module name="NeedBraces">
      <property name="id" value="checkstyle:needbraces"/>
    </module>

    <module name="RightCurly">
      <property name="id" value="checkstyle:rightcurly"/>
    </module>

    <module name="AvoidInlineConditionals">
      <property name="id" value="checkstyle:avoidinlineconditionals"/>
    </module>

    <module name="EmptyStatement">
      <property name="id" value="checkstyle:emptystatement"/>
    </module>

    <module name="HiddenField">
      <property name="id" value="checkstyle:hiddenfield"/>
      <property name="tokens" value="VARIABLE_DEF"/>
    </module>

    <module name="IllegalInstantiation">
      <property name="id" value="checkstyle:illegalinstantiation"/>
    </module>

    <module name="InnerAssignment">
      <property name="id" value="checkstyle:innerassignment"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="MagicNumber">
      <property name="id" value="checkstyle:magicnumber"/>
    </module>

    <module name="MissingSwitchDefault">
      <property name="id" value="checkstyle:missingswitchdefault"/>
    </module>

    <module name="RedundantThrows">
      <property name="id" value="checkstyle:redundantthrows"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="SimplifyBooleanExpression">
      <property name="id" value="checkstyle:simplifybooleanexpression"/>
    </module>

    <module name="SimplifyBooleanReturn">
      <property name="id" value="checkstyle:simplifybooleanreturn"/>
    </module>

    <module name="DesignForExtension">
      <property name="id" value="checkstyle:designforextension"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="FinalClass">
      <property name="id" value="checkstyle:finalclass"/>
    </module>

    <module name="HideUtilityClassConstructor">
      <property name="id" value="checkstyle:hideutilityclassconstructor"/>
    </module>

    <module name="VisibilityModifier">
      <property name="id" value="checkstyle:visibilitymodifier"/>
    </module>

    <module name="ArrayTypeStyle">
      <property name="id" value="checkstyle:arraytypestyle"/>
    </module>

    <module name="UpperEll">
      <property name="id" value="checkstyle:upperell"/>
    </module>

    <module name="AnnotationUseStyle">
      <property name="id" value="checkstyle:annotationusestyle"/>
    </module>

    <module name="MissingDeprecated">
      <property name="id" value="checkstyle:missingdeprecated"/>
    </module>

    <module name="MissingOverride">
      <property name="id" value="checkstyle:missingoverride"/>
    </module>

    <module name="PackageAnnotation">
      <property name="id" value="checkstyle:packageannotation"/>
    </module>

    <module name="AbstractClassName">
      <property name="id" value="checkstyle:abstractclassname"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="AnonInnerLength">
      <property name="id" value="checkstyle:anoninnerlength"/>     
    </module>

    <module name="ExecutableStatementCount">
      <property name="id" value="checkstyle:executablestatementcount"/>
      <property name="max" value="20"/>
      <property name="tokens" value="INSTANCE_INIT,STATIC_INIT,METHOD_DEF,CTOR_DEF"/>
    </module>

    <module name="LineLength">
      <property name="id" value="checkstyle:linelength"/>
      <property name="max" value="120"/>
      <property name="tabWidth" value="4"/>
    </module>

    <module name="MethodLength">
      <property name="id" value="checkstyle:methodlength"/>
      <property name="severity" value="ignore"/>
      <property name="max" value="20"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="OuterTypeNumber">
      <property name="id" value="checkstyle:outertypenumber"/>
    </module>

    <module name="RegexpSinglelineJava">
      <property name="id" value="checkstyle:regexpsinglelinejava"/>
      <property name="format" value="^.*System.out.println.*$"/>
      <property name="ignoreComments" value="true"/>
    </module>

    <module name="AvoidNestedBlocks">
      <property name="id" value="checkstyle:avoidnestedblocks"/>
      <property name="allowInSwitchCase" value="true"/>
    </module>

    <module name="CovariantEquals">
      <property name="id" value="checkstyle:covariantequals"/>
    </module>

    <module name="DefaultComesLast">
      <property name="id" value="checkstyle:defaultcomeslast"/>
    </module>

    <module name="DeclarationOrder">
      <property name="id" value="checkstyle:declarationorder"/>
    </module>

    <module name="EqualsHashCode">
      <property name="id" value="checkstyle:equalshashcode"/>
    </module>

    <module name="ExplicitInitialization">
      <property name="id" value="checkstyle:explicitinitialization"/>
    </module>

    <module name="FallThrough">
      <property name="id" value="checkstyle:fallthrough"/>
    </module>

    <module name="IllegalCatch">
      <property name="id" value="checkstyle:illegalcatch"/>
    </module>

    <module name="IllegalThrows">
      <property name="id" value="checkstyle:illegalthrows"/>
    </module>

    <module name="MissingCtor">
      <property name="id" value="checkstyle:missingctor"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="ModifiedControlVariable">
      <property name="id" value="checkstyle:modifiedcontrolvariable"/>
    </module>

    <module name="MultipleStringLiterals">
      <property name="id" value="checkstyle:multiplestringliterals"/>
       <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="MultipleVariableDeclarations">
      <property name="id" value="checkstyle:multiplevariabledeclarations"/>
    </module>

    <module name="NestedForDepth">
      <property name="id" value="checkstyle:nestedfordepth"/>
      <property name="max" value="2"/>
    </module>

    <module name="NestedIfDepth">
      <property name="id" value="checkstyle:nestedifdepth"/>
      <property name="max" value="2"/>
    </module>

    <module name="NestedTryDepth">
      <property name="id" value="checkstyle:nestedtrydepth"/>
    </module>

    <module name="NoClone">
      <property name="id" value="checkstyle:noclone"/>
    </module>

    <module name="NoFinalizer">
      <property name="id" value="checkstyle:nofinalizer"/>
    </module>

    <module name="ParameterAssignment">
      <property name="id" value="checkstyle:parameterassignment"/>
    </module>

    <module name="StringLiteralEquality">
      <property name="id" value="checkstyle:stringliteralequality"/>
    </module>

    <module name="OneStatementPerLine">
      <property name="id" value="checkstyle:onestatementperline"/>
    </module>

    <module name="InnerTypeLast">
      <property name="id" value="checkstyle:innertypelast"/>
    </module>

    <module name="InterfaceIsType">
      <property name="id" value="checkstyle:interfaceistype"/>
    </module>

    <module name="MutableException">
      <property name="id" value="checkstyle:mutableexception"/>
    </module>

    <module name="BooleanExpressionComplexity">
      <property name="id" value="checkstyle:booleanexpressioncomplexity"/>
    </module>

    <module name="ClassFanOutComplexity">
      <property name="id" value="checkstyle:classfanoutcomplexity"/>
      <property name="max" value="10"/>
    </module>

    <module name="JavaNCSS">
      <property name="id" value="checkstyle:gavancss"/>
      <property name="methodMaximum" value="20"/>
    </module>

    <module name="NPathComplexity">
      <property name="id" value="checkstyle:npathcomplexity"/>
      <property name="max" value="1200"/>
    </module>

    <module name="TrailingComment">
      <property name="id" value="checkstyle:trailingcomment"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="UncommentedMain">
      <property name="id" value="checkstyle:uncommentedmain"/>
    </module>

    <module name="CyclomaticComplexity">
      <property name="id" value="checkstyle:cyclomaticcomplexity"/>
      <property name="max" value="7"/>
    </module>

    <module name="StrictDuplicateCode">
      <property name="id" value="checkstyle:strictduplicatecode"/>
      <property name="severity" value="ignore"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
    </module>

    <module name="SuppressWarnings">
      <property name="id" value="checkstyle:suppresswarnings"/>
    </module>

  </module>



  <module name="JavadocPackage">
    <property name="id" value="checkstyle:javadocpackage"/>
    <property name="severity" value="ignore"/>
    <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
  </module>

  <module name="Translation">
      <property name="id" value="checkstyle:translation"/>
  </module>

  <module name="FileLength">
    <property name="id" value="checkstyle:filelength"/>   
    <property name="max" value="1000"/>
  </module>

  <module name="NewlineAtEndOfFile"> 
    <property name="id" value="checkstyle:newlineatendoffile"/>   
    <property name="fileExtensions" value="*.java"/>
  </module>

  <module name="SuppressWithNearbyCommentFilter"/>

  <module name="SuppressWarningsFilter"/>

  <module name="SuppressionCommentFilter">
    <metadata name="net.sf.eclipsecs.core.comment" value="Single warning"/>
    <property name="offCommentFormat" value="CHECKSTYLE\:OFF\: ([\w\|]+)"/>
    <property name="onCommentFormat" value="CHECKSTYLE\:ON\: ([\w\|]+)"/>
    <property name="checkFormat" value="$1"/>
  </module>

</module>

答案 1 :(得分:6)

我的checkstyle版本为8.1

它适用于这样的gradle配置:

build.gradle

apply plugin: 'checkstyle'

checkstyle {
    configFile = file('config/checkstyle.xml')
    toolVersion = '8.1'
}

并忽略这样的幻数:

@SuppressWarnings("checkstyle:MagicNumber")
private String f(String a) {
    String b = a.substring(0, 7);
    String c = a.substring(a.length() - 3);
    return b + "-" + c;
}

注意:前缀checkstyle:是可选的。 希望这可以帮助别人。

答案 2 :(得分:1)

只是强调Caio Faustino的评论,该评论至少在checkstyle 8.19上解决了该问题。

  1. $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $Uid, ($pwd | ConvertTo-SecureString -AsPlainText -Force) Start-Process -FilePath C:\windows\system32\system32\notepad.exe -Credential -Credential Get-Credential -Argumentlist $Args 添加到<module name="SuppressWarningsHolder"/>元素。
  2. 添加Treewalker作为常规模块。
<module name="SuppressWarningsFilter"/>