16

I'm using the CheckStyle plugin for Eclipse.

It's great at finding things I didn't intend 99% of the time, but the 1% of the time I actually did intend to knowingly violate a rule, I would like to let CheckStyle know it need not concern itself with flagging a warning.

Example: the Missing a Javadoc comment rule. Most of the time I want Javadoc comments on my methods. However, a method such as:

public boolean isValid() {
  return valid;
}

can probably get by without one.

Is there anything like the @SuppressWarnings annotation which can be used to flag a specific CheckStyle rule violation as acceptable? Some sort of specially formatted comment, maybe? I don't want to disable the rule, I just want to ignore a specific violation.

(I realize in this case I could just write the Javadoc comment, but in other cases fixing the rule violation isn't so simple).

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
  • 3
    See http://stackoverflow.com/questions/1704430/is-there-a-way-to-force-checkstyle-to-ignore-particular-warning-in-the-source-cod – Thierry Roy Aug 25 '10 at 12:43
  • 2
    See http://stackoverflow.com/questions/4023185/how-to-disable-a-particular-checkstyle-rule-for-a-particular-line-of-code which I think is actually more useful as the accepted answer is clearer and more thorough – chrisbunney Dec 20 '11 at 10:44

3 Answers3

6

PhiLho is right - SuppressWithNearbyCommentFilter or SuppressionCommentFilter can help. I have SuppressionCommentFilter configured and adding the comments "CHECKSTYLE:OFF" and "CHECKSTYLE:ON" will disable check style temporarily.

Aaron
  • 61
  • 1
  • 1
  • Can you add an example of how to use these comments? Do they just go inside standard single line comments, can they work using multiline comments, or what about mixing and matching the comment-type for the OFF and ON comment? – chrisbunney Dec 20 '11 at 10:40
  • 2
    Ah, nevermind, I found this answer: http://stackoverflow.com/a/4023351/110255 – chrisbunney Dec 20 '11 at 10:45
2

Synthesis pointed to the Checkstyle configuration page. Skimming it, I found SuppressWithNearbyCommentFilter which seems promising, unless I misunderstood its purpose...

Armand
  • 23,463
  • 20
  • 90
  • 119
PhiLho
  • 40,535
  • 6
  • 96
  • 134
1

Seems pretty tedious but there needs to be explicit XML configuration to ignore it. You can probably find a GUI to do it via using the Checkstyle plugin for Eclipse or Netbeans. The example I've found is on the Checkstyle configuration page.

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress checks="JavadocStyleCheck"
              files="AbstractComplexityCheck.java"
              lines="82,108-122"/>
    <suppress checks="MagicNumberCheck"
              files="JavadocStyleCheck.java"
              lines="221"/>
</suppressions>
Synthesis
  • 230
  • 3
  • 3
  • 12
    I'd advise against using this method, because subsequent changes in the classes referenced would change the line numbers, causing a maintenance nightmare. Better change the settings for warnings that we want to catch or not globally, or use CHECKSTYLE:OFF / CHECKSTYLE:ON comments. – stivlo Aug 25 '11 at 13:46