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
SONAR complaining to change the condition so that it does not always evaluate to "false"
提问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.
总是评估为的条件false是username == null和password == 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 notnull. The conditionusername.length() < 1is evaluated- If the result is
true, we return directly and enter theifbranch - If the result is
false, we try to evaluateusername == null. But since theusernamegiven is notnull, this alwaysevaluate tofalse.
- If the result is
- The
usernamegiven isnull. The conditionusername.length() < 1is evaluated. This actually stops right there: it will throw aNullPointerExceptionand will not evaluate the right hand side.
- 在
username给出的不是null。条件username.length() < 1被评估- 如果结果是
true,我们直接返回并进入if分支 - 如果结果是
false,我们尝试评估username == null。但由于username给定的不是null,因此始终评估为false。
- 如果结果是
- 在
username给出的null。username.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 notnull. First condition clearly evaluates tofalseand the second is evaluated, which may returntrueorfalse. - The
usernamegiven isnull. The first condition clearly evaluated totrueand short-circuits.
- 在
username给出的不是null。第一个条件明确评估为false,第二个条件评估,可能返回true或false。 - 在
username给出的null。第一个条件明确评估为true短路。

