Checkstyle 和泛型
我正在尝试纠正我的项目的所有 Checkstyle 警告,但是当我需要构造一个采用泛型参数的类时,我总是遇到一些非常烦人的事情。以下面一行为例:
labels = new HashSet < String >();
然后 Checkstyle 抱怨 '">"后面没有空格'。所以我写了空格:
labels = new HashSet < String > ();
现在它抱怨 '"(" 前面有空格'。
这是一个错误吗?有没有办法在不添加 @SupressWarnings 注释的情况下绕过它?
I'm trying to correct all Checkstyle warnings for my project, but I keep stumbling over something very annoying when I need to construct a class that takes a Generics argument. Take the following line, for example:
labels = new HashSet < String >();
Then Checkstyle complains that '">" is not followed by whitespace'. So I write the whitespace:
labels = new HashSet < String > ();
And now it complains that '"(" is preceded by whitespace'.
Is this a bug? Is there a way to bypass it without adding a @SupressWarnings annotation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您至少运行 5.0 版本的 Checkstyle,因为它修复了泛型的处理。请参阅发行说明http://checkstyle.sf.net/releasenotes.html。
Make sure you are running at least the 5.0 version of Checkstyle as it fixed the handling of generics. See the release notes http://checkstyle.sf.net/releasenotes.html.
它可能认为
>
是大于运算符,因为它前面有一个空格。空间泛型声明的常见方法是这样的:
除非你的检查样式规则被调整,否则上面的内容很可能会通过。
It probably thinks the
>
is a greater than operator because there is a space before it.The common way to space generics declarations is like this:
Unless your checkstyle rules are tweaked, the above should most likely pass.
我正在使用 Checkstyle 版本 6.17,通过从 WhitespaceAround 规则。
I have Checkstyle version 6.17 in use and I got this resolved by removing
GENERIC_START
andGENERIC_END
from thetokens
field in WhitespaceAround rule.