我可以禁用对已弃用的方法和类的 CheckStyle 投诉吗?

发布于 2024-08-07 12:38:51 字数 249 浏览 2 评论 0原文

我正在维护一个已弃用某些公共静态字段的 API。

CheckStyle 大声抱怨这些,但我宁愿让它完全忽略它们,因为我已经通过将字段标记为已弃用来处理问题。

具体来说,该库具有用于枚举的常量(公共静态最终),但它们没有标记为最终的。 CheckStyle 会抱怨它们,但我不能在不违反合同的情况下将它们更改为最终版本。

我的计划是将它们标记为已弃用,然后删除它们。但将它们标记为已弃用并不会将它们从 CheckStyle 报告中删除。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-08-14 12:38:51

我有两个选项供您选择:

  1. 手动抑制每行的警告

    这种方法不太灵活,因为每次转移线路时都必须维护抑制配置。但您可以单独处理每个事件。

    <抑制检查=“YOURCHECK”文件=“.*YOURCLASS\.java”行=“YOURLINES”/>
    

    我不知道哪个检查导致了您的问题,因此您必须将 YOURCHECK 替换为正确的名称。 YOURCLASS 命名 java 文件,其中包含已弃用的代码,但您可以插入 .* 将其应用到每个文件。 YOURLINES 是一个以逗号分隔的值列表,其中每个值都是一个整数或由整数整数表示的整数范围。

  2. 使用注释建议 checkstyle 忽略警告

    此解决方案允许您立即停用对所有已弃用成员的检查。但你必须遵循严格的约定。您必须在最后一个位置向这些成员添加 @deprecated 注释(您可能已经这样做了),因为此过滤器具有严格的行范围。

    <前><代码>/**
    * @deprecated 我不再喜欢这个了。
    */
    公共静态字符串示例=“示例”;

    此解决方案需要更改您的配置文件。首先,您必须将 FileContentsHolder 作为子项添加到 TreeWalker

    <模块名称=“TreeWalker”>
        ...
        <模块名称=“FileContentsHolder”/>
        ...
    
    

    现在您可以配置 SuppressWithNearbyCommentFilter,它是 Checker 模块的一部分。

    <模块名称=“检查器”>
        ...
        <模块名称=“SuppressWithNearbyCommentFilter”>
            
            <属性名称=“checkFormat”值=“.*”/>
            <属性名称=“influenceFormat”值=“2”/>
        
        ...
    
    

    如果您决定仅忽略特定检查,请调整 checkFormat 属性。或者,如果您想使用其他注释,请更改 commentFormat 属性。但将 influenceFormat 设置为正确的值非常重要。它告诉 checkstyle 在注释后的多少行内忽略这些检查。

PS:请注意,当您使用其用户界面更改配置时,Eclipse CheckStyle 插件会删除 FileContentsHolder 模块,因此您一定不要使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文