Checkstyle:如何解决“隐藏字段”问题错误

发布于 2024-12-10 00:28:05 字数 236 浏览 1 评论 0原文

我收到此 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 技术交流群。

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

发布评论

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

评论(7

魂牵梦绕锁你心扉 2024-12-17 00:28:05

已经定义了一个可用于此方法的变量 serverURL(除了您接受的形式参数之外)。这称为“阴影”。

我认为大多数 Java 程序员都会关闭此检查,因为它并不是那么令人困惑。

例如,这会触发错误:

public class Foo {
  private int bar = 0;

  public void someMethod(int bar) {
    // There are two bars!  All references in this method will use the parameter bar,
    // unless they are explicitly prefixed with 'this'.
    this.bar = bar;
  }
}

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:

public class Foo {
  private int bar = 0;

  public void someMethod(int bar) {
    // There are two bars!  All references in this method will use the parameter bar,
    // unless they are explicitly prefixed with 'this'.
    this.bar = bar;
  }
}
愿得七秒忆 2024-12-17 00:28:05

我认为在构造函数和设置器中,设置字段名称与设置器参数名称相同是很常见的。这就是我推荐这种配置的原因:

<module name="HiddenField" >
    <property name="ignoreSetter" value="true" />
    <property name="ignoreConstructorParameter" value="true" />
</module>

这样其他隐藏字段的情况仍然被禁止。

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:

<module name="HiddenField" >
    <property name="ignoreSetter" value="true" />
    <property name="ignoreConstructorParameter" value="true" />
</module>

This way the other hidden field cases are still forbidden.

花期渐远 2024-12-17 00:28:05

参数和静态字段具有相同的名称。只需重命名其中之一即可。
有些人遵循命名约定,在所有参数前添加 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 have serverURL as field name and pServerURL as parameter name.
Or you could simply turn off the check.

山色无中 2024-12-17 00:28:05

我通过在 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.

柳絮泡泡 2024-12-17 00:28:05

只需将方法中的参数名称更改

private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}

private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}

Enjoy...

Jigar Patel

Just change ur param name in ur method

private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}

to

private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}

Enjoy...

Jigar Patel

夜血缘 2024-12-17 00:28:05

有点愚蠢的错误,但这是我在设置器中放置的简单修复程序来修复它:

public void setName(String name) {
  this.name = name;
}

更新:

public void setName(String val) {
  this.name = val;
}

Kinda dumb error, but here is the easy fix I put in my setters to fix it:

was:

public void setName(String name) {
  this.name = name;
}

updated:

public void setName(String val) {
  this.name = val;
}
安静 2024-12-17 00:28:05

我阅读了文档:

https://checkstyle .sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.html

这是解决方案:

<module name="HiddenField">
   <property name="tokens" value="VARIABLE_DEF"/>
 </module>

如果已经有HiddentField 标签,然后添加 tokens 属性。

我遇到了类似的问题,通过此修复一切正常。

I read the documnetation :

https://checkstyle.sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.html

Here is the solution:

<module name="HiddenField">
   <property name="tokens" value="VARIABLE_DEF"/>
 </module>

If already have a HiddentField tag, then add the tokens property.

I had a similar issue, with this fix everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文