SONAR complaining to change the condition so that it does not always evaluate to quot;falsequot;(SONAR 抱怨改变条件,使其并不总是评估为“假.)
问题描述
public String generateURLSafeToken(String username, char[] password) throws CredentialTokenException {this.tokenValid = false;字符串令牌 = null;if ((username.length() < 1) || (username == null)) {throw new CredentialTokenException("用户名不能为空字符串或 null.");}if ((password.length < 1) || (password == null)) {throw new CredentialTokenException("密码不能为空或 null.");}<块引用>
我在第 4 行和第 7 行遇到此错误(用户名 == null 和密码 == null)
我的代码中需要这部分.我正在尝试 isEmpty() 而不是 null 但也面临着问题.解决此 SONAR 错误的替代方法或解决方案是什么
总是计算结果为 false 的条件是 username == null 和 password ==空.
我们以username为例.运算符 || 是 短路 意味着它赢了'如果左侧为 true,则不计算右侧.基本上有两种情况:
- 给定的
username不是null.条件username.length() <1被评估- 如果结果为
true,我们直接返回,进入if分支 - 如果结果是
false,我们会尝试评估username == null.但是由于给出的username不是null,因此 always 的计算结果为false.
- 如果结果为
- 给定的
username是null.条件username.length() <1被评估.这实际上停在那里:它会抛出一个NullPointerException并且不会评估右侧.
因此,您可以看到,无论何时实际评估 username == null 条件,结果始终为 false.这就是 SonarQube 警告告诉您的内容.
这里的解决方案是颠倒您的 2 个条件.考虑拥有
if (username == null || username.length() < 1)相反.如果您重新开始并检查每个案例,您会注意到没有一个表达式将始终具有相同的结果:
- 给定的
username不是null.第一个条件明确评估为false,第二个条件被评估,可能返回true或false. - 给定的
username是null.第一个条件明确评估为true和短路.
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)
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
The conditions which always evaluates to false are username == null and password == null.
Let's take the example of username. The operator || is short-circuiting meaning it won't evaluate the right hand side if the left hand side is true. Basically, there are 2 cases:
- 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 always evaluate 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.
Therefore, you can see that whenever the username == null condition was actually evaluated, the result was always false. This is what the SonarQube warning is telling you.
The solution here is to reverse your 2 conditions. Consider having
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.
这篇关于SONAR 抱怨改变条件,使其并不总是评估为“假".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SONAR 抱怨改变条件,使其并不总是评估为“假".
基础教程推荐
- 从 python 访问 JVM 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
