开发者

Can I disable CheckStyle complaints for deprecated methods and classes?

开发者 https://www.devze.com 2022-12-08 08:15 出处:网络
I\'m maintaining an API that has deprecated some public static fields. CheckStyle complains loudly about these but I\'d rather have it ignore them completely since I\'ve dealt with the problem by ma

I'm maintaining an API that has deprecated some public static fields.

CheckStyle complains loudly about these but I'd rather have it ignore them completely since I've dealt with the problem by marking the fields as deprecated.

Specifically, the library has constants for enumeration (public static final) b开发者_如何转开发ut the they are not marked as final. CheckStyle will complain about them, but I can't just change them to final without breaking the contract.

My plan is to mark them as deprecated and then delete them later. But marking them as deprecated doesn't remove them from the CheckStyle Report.


I have two options for you:

  1. suppress the warning for each line manually

    This approach is less flexible as you have to maintain the suppression configuration each time you are shifting lines. But you can handle each occurrence individually.

    <suppress checks="YOURCHECK" files=".*YOURCLASS\.java" lines="YOURLINES"/>
    

    I don't know which check is causing your problem, so you have to replace YOURCHECK with the proper name. YOURCLASS names the java file, which contains the deprecated code, but you can insert .* to apply it to every file. YOURLINES is a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer.

  2. use a comment to advise checkstyle to ignore warnings

    This solution allows you to deactivate checks for all deprecated members at once. But you have to follow a strict convention. You have to add a @deprecated comment to those members (, what you possibly do already) at the very last position, because this filter has a strict range of lines.

    /**
    * @deprecated I don't like this anymore.
    */
    public static String EXAMPLE = "example";
    

    This solutions needs a change within your configuration file. First you have to add FileContentsHolder as a child to TreeWalker.

    <module name="TreeWalker">
        ...
        <module name="FileContentsHolder"/>
        ...
    </module>
    

    Now you may configure the SuppressWithNearbyCommentFilter which is part of the Checker module.

    <module name="Checker">
        ...
        <module name="SuppressWithNearbyCommentFilter">
            <property name="commentFormat" value=".*deprecated.*"/>
            <property name="checkFormat" value=".*"/>
            <property name="influenceFormat" value="2"/>
        </module>
        ...
    </module>
    

    If you decide to ignore only specific checks, adjust the checkFormat attribute. Or if you want to use another comment, change the commentFormat attribute. But it is very important, that you set influenceFormat to the right value. It tells checkstyle within how many lines after the comment it is due to ignore those checks.

P.S.: Note that the Eclipse CheckStyle plugin removes the FileContentsHolder module, when you change the configuration with its user interface, so you must not use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消