Checkstyle:如何解决“隐藏字段”问题错误
我收到此 checkstyle 错误:
'serverURL' hides a field
可能
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
是什么原因,以及如何解决它?
I am getting this checkstyle error:
'serverURL' hides a field
in this
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
What could be the reason, and how to resolve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
已经定义了一个可用于此方法的变量
serverURL
(除了您接受的形式参数之外)。这称为“阴影”。我认为大多数 Java 程序员都会关闭此检查,因为它并不是那么令人困惑。
例如,这会触发错误:
There is already a variable defined
serverURL
which is available to this method (additional to the formal parameter you are accepting). This is called "shadowing".I think most Java programmers turn this check off, because it's not really that confusing.
For example, this would trigger the error:
我认为在构造函数和设置器中,设置字段名称与设置器参数名称相同是很常见的。这就是我推荐这种配置的原因:
这样其他隐藏字段的情况仍然被禁止。
I think it is very common in constructors and setters that the set field name is the same as the setter parameter name. This is why i recommend this configuration:
This way the other hidden field cases are still forbidden.
参数和静态字段具有相同的名称。只需重命名其中之一即可。
有些人遵循命名约定,在所有参数前添加
p
前缀。然后,您将使用serverURL
作为字段名称,使用pServerURL
作为参数名称。或者您可以简单地关闭检查。
The parameter and the static field have the same name. Just rename one of them.
Some people follow a naming convention that prefixes all parameters with
p
. Then you would haveserverURL
as field name andpServerURL
as parameter name.Or you could simply turn off the check.
我通过在 Eclipse 中禁用它来解决它。当我登陆此页面时,我正在寻找如何做到这一点。我没有在前 10 名谷歌查询中找到答案,所以我不得不以艰难的方式找出答案。对于正在寻找的人,我是这样做的:
打开
Eclipse>Preferences>Checkstyle
找到您正在使用的 checkstyle 配置(您可能已经设置了该配置,或者您正在使用默认值,在这种情况下,最好创建一个副本你自己的,然后编辑它)。选择它,然后单击右侧的配置按钮。在列表中找到以下配置:
编码问题>隐藏字段
打开配置(UI 中有一个名为“打开”的按钮)。
取消选择“参数声明”。单击“确定”,然后单击“确定”,然后单击“确定”。
I resolved it by disabling it in eclipse. I was looking for how to do that when I landed on this page. I didn't find the answer in top 10 google query so I had to figure it out the hard way. For someone who is looking for that, here is how I did it:
Open
Eclipse>Preferences>Checkstyle
Find the checkstyle configuration you are using (you might have set that or your are using default in which case its a better idea to create a copy of your own and then edit that). Select that and then click the configure button on the right side. Find the following config in the list:
Coding Problems>Hidden Field
Open the configuration (there is button called 'open' in the UI).
Unselect 'Parameter Declaration'. Click OK then Click OK and then Click OK.
只需将方法中的参数名称更改
为
Enjoy...
Jigar Patel
Just change ur param name in ur method
to
Enjoy...
Jigar Patel
有点愚蠢的错误,但这是我在设置器中放置的简单修复程序来修复它:
已
更新:
Kinda dumb error, but here is the easy fix I put in my setters to fix it:
was:
updated:
我阅读了文档:
https://checkstyle .sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.html
这是解决方案:
如果已经有HiddentField 标签,然后添加 tokens 属性。
我遇到了类似的问题,通过此修复一切正常。
I read the documnetation :
https://checkstyle.sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.html
Here is the solution:
If already have a HiddentField tag, then add the tokens property.
I had a similar issue, with this fix everything works fine.