java SONAR 抱怨要改变条件,使其不总是评估为“假”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39491377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:25:01  来源:igfitidea点击:

SONAR complaining to change the condition so that it does not always evaluate to "false"

javamavensonarqube

提问by Awa

public String generateURLSafeToken(String username, char[] password) throws CredentialTokenException {
    this.tokenValid = false;

    String token = null;
    if ((username.length() < 1) || (username == null)) {
        throw new CredentialTokenException("Username cannot be an empty string or null.");
    }
    if ((password.length < 1) || (password == null)) {
        throw new CredentialTokenException("Password cannot be an empty or null.");
    }

I am facing this error in line 4 and line 7 (username == null and password == null)

我在第 4 行和第 7 行中遇到此错误(用户名 == 空和密码 == 空)

And I need this part in my code. I am trying isEmpty() instead of null but facing problems in that also . What is an alternate way or the solution to fix this SONAR error

我的代码中需要这部分。我正在尝试 isEmpty() 而不是 null 但也面临着问题。修复此 SONAR 错误的替代方法或解决方案是什么

回答by Tunaki

The conditions which always evaluates to falseare username == nulland password == null.

总是评估为的条件falseusername == nullpassword == null

Let's take the example of username. The operator ||is short-circuitingmeaning it won't evaluate the right hand side if the left hand side is true. Basically, there are 2 cases:

让我们以 为例username。运算符||短路的,这意味着如果左侧是 ,则不会评估右侧true。基本上有2种情况:

  • The usernamegiven is not null. The condition username.length() < 1is evaluated
    • If the result is true, we return directly and enter the ifbranch
    • If the result is false, we try to evaluate username == null. But since the usernamegiven is not null, this alwaysevaluate to false.
  • The usernamegiven is null. The condition username.length() < 1is evaluated. This actually stops right there: it will throw a NullPointerExceptionand will not evaluate the right hand side.
  • username给出的不是null。条件username.length() < 1被评估
    • 如果结果是true,我们直接返回并进入if分支
    • 如果结果是false,我们尝试评估username == null。但由于username给定的不是null,因此始终评估为false
  • username给出的nullusername.length() < 1评估条件。这实际上就停在那里:它会抛出 aNullPointerException并且不会评估右手边。

Therefore, you can see that whenever the username == nullcondition was actually evaluated, the result was always false. This is what the SonarQube warning is telling you.

因此,您可以看到,无论何时username == null实际评估条件,结果始终为false。这就是 SonarQube 警告告诉你的。

The solution here is to reverse your 2 conditions. Consider having

这里的解决方案是反转您的 2 个条件。考虑拥有

if (username == null || username.length() < 1)

instead. If you start over and go through each case, you'll notice that none of the expressions will always have the same result:

反而。如果您重新开始并检查每种情况,您会注意到没有一个表达式总是具有相同的结果:

  • The usernamegiven is not null. First condition clearly evaluates to falseand the second is evaluated, which may return trueor false.
  • The usernamegiven is null. The first condition clearly evaluated to trueand short-circuits.
  • username给出的不是null。第一个条件明确评估为false,第二个条件评估,可能返回truefalse
  • username给出的null。第一个条件明确评估为true短路。