简体   繁体   中英

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

We have this application called sonar to analyze code for proper coding practice. One of the violations that it's indicting is : Unused Formal parameter: Avoid unused constructor parameters such as '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 . 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. To stop the warning about duplicated code, just remove TextLimiter(int limit, boolean upper) and keep public TextLimiter(int limit) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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