Search code examples
checkstyle

Supress DesignForExtension for toString usign supression.xml


CheckStyle is running on my code and gives the following warning:

Class 'MyClass' looks like designed for extension (can be subclassed), but the method 'toString' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'CosemObis' final or making the method 'toString' static/final/abstract/empty, or adding allowed annotation for the method. (172:5) [DesignForExtension]

For most methods this is a valid check but toString doesn't need any java doc IMO and I don't want to make it final. I also don't want to exclude @Override because the check does apply to other methods with this annotation. My suppressions.xml looks like this but this disables the check for all methods:

<?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 files=".java" checks="DesignForExtension" />
</suppressions>

How do I suppress the check DesignForExtension just for the method 'toString'?


Solution

  • The docs for SuppressionFilter list those parameters:

    • files - a Pattern matched against the file name associated with an audit event. It is optional.
    • checks - a Pattern matched against the name of the check associated with an audit event. Optional as long as id or message is specified.
    • message - a Pattern matched against the message of the check associated with an audit event. Optional as long as checks or id is specified.
    • id - a String matched against the check id associated with an audit event. Optional as long as checks or message is specified.
    • lines - a comma-separated list of values, where each value is an int or a range of integers denoted by integer-integer. It is optional.
    • columns - a comma-separated list of values, where each value is an int or a range of integers denoted by integer-integer. It is optional.

    You could use message to further filter and suppress this check to toString methods only. Like in the example:

    Suppress naming violations on variable named 'log' in all files:

    <suppress message="Name 'log' must match pattern"/>
    

    Solution: add a message filter

    To suppress the check DesignForExtension just for the method 'toString' because it doesn't need any JavaDoc, add a filter for the message-part like:

    <suppressions>
        <suppress 
           files=".java"
           checks="DesignForExtension"
           message="but the method 'toString' does not have javadoc"
        />
    </suppressions>
    

    Alternative: configure the module with ignoredAnnotations

    Found related example in JavaDocs of DesignForExtensionCheck. It shows the presented warning for overridden toString.

    Example:

    public abstract class Foo {
       // omitted for focus
    
       @Override
       public String toString() {  // Violation. No javadoc for  @Override method.
         return "";
       }
    }
    

    To configure the check to allow methods which have @Override annotations to be designed for extension.

    <module name="DesignForExtension">
      <property name="ignoredAnnotations" value="Override"/>
    </module>
    

    Afterwards the DesignForExtension violation warning for the method would be gone.