Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Suppress Warnings in CheckStyle? [duplicate]

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).

like image 625
Tom Tresansky Avatar asked Aug 25 '10 12:08

Tom Tresansky


People also ask

How do I stop checkstyle warnings?

It is possible to suppress all the checkstyle warnings with the argument "all" . You can also use a checkstyle: prefix to prevent compiler from processing these annotations. You can also define aliases for check names that need to be suppressed.

How do I exclude files from checkstyle?

To skip all checks in all files under directories (packages) named "generated", you must use regex pattern <suppress checks="[a-zA-Z0-9]*" files="[\\/]generated[\\/]" /> It was really little hard for me to find out the right pattern for files parameter.

What is @SuppressWarnings unchecked?

@SuppressWarnings("unchecked") is used when Java generics just don't let you do what you want to, and thus, you need to explicitly specify to the compiler that whatever you are doing is legal and can be executed at the time of execution.


2 Answers

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.

like image 123
Aaron Avatar answered Oct 01 '22 23:10

Aaron


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>
like image 45
Synthesis Avatar answered Oct 01 '22 23:10

Synthesis