允许使用 Checkstyle 的单行访问器(getter/setter)语法
我们希望拥有使用单行语法的简单 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用 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!
我建议使用 SuppressionCommentFilter 禁用代码块的 Checkstyle 检查。
然后可以执行以下操作来关闭 Checkstyle,如下所示:
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: