我可以为不赞成使用的方法和类禁用CheckStyle投诉吗? [英] Can I disable CheckStyle complaints for deprecated methods and classes?

查看:75
本文介绍了我可以为不赞成使用的方法和类禁用CheckStyle投诉吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个API,该API已弃用了一些公共静态字段.

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

CheckStyle大声抱怨这些,但是我宁愿它完全忽略它们,因为我已经通过将字段标记为已弃用来解决该问题.

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.

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

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.

我的计划是将它们标记为已弃用,然后在以后将其删除.但是,将它们标记为不推荐使用并不能将它们从CheckStyle报告中删除.

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.

推荐答案

我为您提供两个选择:

  1. 手动取消每行的警告

此方法灵活性较差,因为每次移线时都必须维护抑制配置.但是您可以单独处理每种情况.

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"/>

我不知道是哪个支票引起了您的问题,因此您必须用适当的名称替换YOURCHECK.YOURCLASS将Java文件命名为包含不赞成使用的代码的Java文件,但是您可以插入.* 以将其应用于每个文件.YOURLINES是一个用逗号分隔的值列表,其中每个值都是一个整数或由integer-integer表示的整数范围.

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.

使用注释建议checkstyle忽略警告

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

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";

此解决方案需要在您的配置文件中进行更改.首先,您必须将 FileContentsHolder 作为子级添加到 TreeWalker .

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>

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

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>

如果您决定仅忽略特定检查,请调整 checkFormat 属性.或者,如果您要使用其他注释,请更改 commentFormat 属性.但是非常重要的是,您将 influenceFormat 设置为正确的值.它会在注释后的几行中告知checkstyle,以忽略这些检查.

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 .:请注意,当您通过用户界面更改配置时,Eclipse CheckStyle插件会删除 FileContentsHolder 模块,因此一定不要使用它.

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.

这篇关于我可以为不赞成使用的方法和类禁用CheckStyle投诉吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆