允许使用 Checkstyle 的单行访问器(getter/setter)语法

发布于 2024-08-30 11:50:17 字数 445 浏览 12 评论 0原文

我们希望拥有使用单行语法的简单 Java 属性访问器,这样它们占用的空间要少得多,并且更具可读性(就快速“查看”访问器集而言)。但我们确实希望对 checkstyle 配置中的其他所有内容强制执行多行方法语法。但我不确定如何在 Checkstyle 配置中为访问器设置此异常,并怀疑这可能是不可能的。

所以我们希望我们的访问器看起来像这样:

public String getFoo() { return foo; }
public void setFoo(String foo) { this.foo = foo; }

[事实上,我们根本不想有简单的访问器,而只是用 @Property 或其他东西注释私有成员变量并拥有访问器为我们生成的,因为编写无休止的 get 和 set 代码不会带来真正的好处,但就这个问题而言,这是一个普遍的 Java 挫败感和旁白。]

We'd like to have trivial Java property accessors using a single line syntax, so they take up much much less space, and are more readable (in terms of 'seeing' the set of accessors quickly). But we do want to enforce multi-line method syntax for everything else in our checkstyle configuration. But I'm not sure how to make this exception for accessors in Checkstyle config and suspect it may not be possible.

So we'd like our accessors to look something like this:

public String getFoo() { return foo; }
public void setFoo(String foo) { this.foo = foo; }

[In fact we'd rather not have trivial accessors at all and instead just annotate the private member variables with @Property or something and have the accessors generated for us, since writing endless get and set code delivers no real benefit, but that's a general Java frustration and an aside as far as this question goes.]

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

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

发布评论

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

评论(2

过期以后 2024-09-06 11:50:18

如果您想使用 Checkstyle 解决此问题,则必须编写一个或多个自定义检查。您的支票将是您正在修改的支票的子类。然后,对于所有受影响的检查,您将在 checkstyle 配置中输入您自己的子类检查(并删除原始检查)。

您也许能够从 JavadocMethodCheck 中删除一些代码,它已经检测到了简单的访问器。这听起来工作量很大,但代码并不多,因为您在子类中所做的修改始终是相同的,因此可以放入辅助类中。

老实说,我不认为 Checkstyle 可以开箱即用,抱歉!

If you want to solve this using Checkstyle, then you must write one or more custom checks. Your check would be a subclass of the check you are modifying. Then, for all checks that are affected, you would enter your own subclassed check into the checkstyle configuration (and remove the original one).

You might be able to cannibalize some code from the JavadocMethodCheck, which already detects trivial accessors. This sounds like a lot of work, but it's not much code because the modification you make in your subclass is always the same and can thus be put into a helper class.

I honestly don't think that Checkstyle can do it out of the box, sorry!

孤寂小茶 2024-09-06 11:50:18

我建议使用 SuppressionCommentFilter 禁用代码块的 Checkstyle 检查。

然后可以执行以下操作来关闭 Checkstyle,如下所示:

// CHECKSTYLE_OFF: ALL
public String getFoo() { return foo; }
public void setFoo(String foo) { this.foo = foo; }
// CHECKSTYLE_ON: ALL

I would recommend using the SuppressionCommentFilter to disable Checkstyle checks for a block of code.

It would then be possible to do the following to turn off Checkstyle like the following:

// CHECKSTYLE_OFF: ALL
public String getFoo() { return foo; }
public void setFoo(String foo) { this.foo = foo; }
// CHECKSTYLE_ON: ALL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文