【问题标题】:How can I get checkstyle to skip equals() and hashcode() methods generated by eclipse?如何让 checkstyle 跳过 eclipse 生成的 equals() 和 hashcode() 方法?
【发布时间】:2011-05-08 16:20:36
【问题描述】:

我们的项目包含几个类,我们有 Eclipse 生成的 equals() 和 hashCode() 方法(右键单击 -> 源 -> 生成 hashCode() 和 equals())。

例子:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    final MyTO other = (MyTO) obj;
    if (num != other.num)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (table == null) {
        if (other.table != null)
            return false;
    } else if (!table.equals(other.table))
        return false;
    return true;
}

这些方法很适合我们的应用程序,但遗憾的是没有通过 Checkstyle 的圈复杂度检查。由于这些方法是自动生成的,我们不关心它们的复杂性。我们可以从 Checkstyle 中抑制整个类,但我们希望能够仅排除这两种方法。

有谁知道如何在 Checkstyle 中创建自定义规则,允许我们以任何方式排除生成的 equals() 和 hashCode() 方法,而不排除整个类?

【问题讨论】:

    标签: java eclipse equals hashcode checkstyle


    【解决方案1】:

    您应该设置一个SupressionCommentFilter。有关此here 的更多信息。

    有时违反支票是有正当理由的。当这是问题代码而不是个人偏好的问题时,覆盖策略的最佳位置是代码本身。半结构化 cmets 可以与检查相关联。

    【讨论】:

    • 我对抑制过滤器不是很熟悉。我如何根据方法名称来确定每个文件要排除的行号?
    • 看起来像SuppressionCommentFilter(来自同一个链接)是需要的。它甚至包括一个排除生成代码的示例。
    • 是的,马特,我很困惑 :)。在答案中编辑。泰
    • 啊。虽然不是理想的解决方案(我现在需要返回并使用生成的代码将 cmets 添加到每个文件中,并记住将来这样做),但这似乎会起作用。谢谢。
    • 这远非理想,但我认为您现在没有其他选择:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2011-09-25
    • 2013-07-28
    • 2011-06-24
    • 1970-01-01
    相关资源
    最近更新 更多