简体   繁体   English

java未使用的形式参数:避免使用未使用的构造函数参数,例如“ upper”

[英]java Unused Formal parameter: Avoid unused constructor parameters such as 'upper'

We have this application called sonar to analyze code for proper coding practice. 我们有一个名为sonar的应用程序来分析代码,以进行正确的编码实践。 One of the violations that it's indicting is : Unused Formal parameter: Avoid unused constructor parameters such as 'upper'. 它指示的违反之一是:未使用的形式参数:避免使用未使用的构造函数参数,例如“ upper”。

 //Private inner class to set the input to a max length.
 public class TextLimiter extends PlainDocument
{
 private int limit;

 public TextLimiter(int limit)
{
    super();
    this.limit = limit;
}

TextLimiter(int limit, boolean upper)
{
    super();
    this.limit = limit;
}

public void insertString(int offset, String str, AttributeSet attr)
        throws BadLocationException
{
    if (str == null)
    {
        return;
    }

    if ((getLength() + str.length()) <= limit)
    {
        super.insertString(offset, str, attr);
    }
  }
}

I am not exactly sure how to correct this. 我不确定如何解决这个问题。

You can fix this by making use of that parameter, like this: 您可以通过使用该参数来解决此问题,如下所示:

public TextLimiter(int limit) {
    this(limit, false);
}

TextLimiter(int limit, boolean upper) {
    super();
    if (upper) {
        this.limit = limit;
    } else {
        this.limit = -limit;
    }
}

This is just an example of how to use upper . 这只是如何使用upper的示例。 It is impossible to decide without knowing the purpose behind your class. 不知道班级目的是不可能做出决定的。

If you have no use for the upper parameter I don't think you should keep it just because it was in the original code. 如果您没有使用upper参数,我认为您不应该仅仅因为它在原始代码中就保留它。 To stop the warning about duplicated code, just remove TextLimiter(int limit, boolean upper) and keep public TextLimiter(int limit) . 要停止有关重复代码的警告,只需删除TextLimiter(int limit, boolean upper)并保留public TextLimiter(int limit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM