如何抑制枚举常量/值上缺少javadoc checkstyle警告?

时间:2017-01-18 00:26:21

标签: java checkstyle

Checkstyle抱怨枚举值没有附加的javadoc注释。但至少在我的许多枚举中,由于值本身通常是不言自明的,因此添加javadoc似乎只会降低可读性,而且不需要杂乱。请考虑以下示例:

df.assign(val=lambda x: (x.set_index('id2')['val'] / df2['new_val']).values)

df.set_index('id2', drop=False).assign(val=lambda x: x['val'] / df2['new_val']).reset_index(drop=True)
/**
 * Example enum to illustrate the problem.  Each value of this
 * enum represents a day of the week.
 */
public enum DaysOfWeekClean {

    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;

}

如果我的支票中添加了/** * Example enum to illustrate the problem. Each value of this * enum represents a day of the week, with comments added to each * distinct value to make the point. */ public enum DaysOfWeekCluttered { /** * The day of the week named "Sunday". */ SUNDAY, /** * The day of the week named "Monday". */ MONDAY, /** * The day of the week named "Tuesday". */ TUESDAY, /** * The day of the week named "Wednesday". */ WEDNESDAY, /** * The day of the week named "Thursday". */ THURSDAY, /** * The day of the week named "Friday". */ FRIDAY, /** * The day of the week named "Saturday". */ SATURDAY; } 模块,则会标记第一个示例(JavadocVariable),而第二个示例(DaysOfWeekClean)将会通过。

这让我陷入了两难境地。我希望checkstyle标记未注释的普通类成员/变量,但要保留我的枚举常量。经过相当多的搜索Checkstyle文档(以及Checkstyle源代码本身)和几个StackOverflow问题后,我似乎无法弄清楚如何设置它。

我可以在两个枚举常量和类成员/变量中缺少javadoc时发出警告,或者我可以忽略它们,但我似乎无法检查一个而不是另一个。

供参考,以下是我尝试的一些checkstyle配置及其结果:

  1. 简单声明,当类成员或枚举常量都没有javadoc时会发出警告:

    DaysOfWeekDirty
  2. 当类成员或枚举常量没有javadoc时发出警告的声明:

    <module name="JavadocVariable" />
    
  3. 当一个成员或枚举常量没有javadoc时,FAILS会发出警告的声明:

    <module name="JavadocVariable">
        <property name="tokens" value="VARIABLE_DEF" />
    </module>
    

2 个答案:

答案 0 :(得分:4)

为了跳过枚举值,您可以像这样配置检查:

<module name="JavadocVariable">
  <property name="tokens" value="VARIABLE_DEF"/>
</module>

2017-01-18的documentationmissing this information,但计划修复此问题。
我使用Eclipse-CS 6.14测试了这种行为,所以如果它不再适合你,那将是一个错误。

答案 1 :(得分:2)

您可以使用SuppressionCommentFilter或使用@SuppressWarnings注释从Checkstyle 5.7抑制CheckStyle警告。

您需要对checkstyle.xml配置进行一些设置。

通过评论

SuppressionCommentFilter添加到checkstyle.xml:

<module name="SuppressionCommentFilter"/>

然后在您的代码中添加评论以关闭checkstyle&amp;回来。

//CHECKSTYLE:OFF
public enum DaysOfWeek {
    //..
//CHECKSTYLE:ON

按注释

如果您更喜欢注释,可以使用SuppressWarningsFilter。请注意,它需要SuppressWarningsHolder模块。将这两个模块添加到checkstyle.xml中的T​​reeWalker中:

<module name="TreeWalker">
    <!-- make @SuppressWarnings annotations available to Checkstyle, and filter warnings by annotation
    -->
    <module name="SuppressWarningsHolder" />
    <module name="SuppressWarningsFilter" />
</module>

现在,您可以使用要排除的警告为您的枚举添加注释。使用适当的警告代码&amp; checkstyle应该压制它。

@SuppressWarnings("checkstyle:<appropriate warning here>")
public enum DaysOfWeek {
    //..

参考文献: