new BigInteger(String) 发出声纳警告
new BigInteger(String) gives Sonar warning
我正在使用 IntelliJ IDEA 2018.1.3 Ultimate Edition,需要与表示为字符串的大整数(大到无法放入 long
,例如 20180531234240565494
)进行比较:
public int compareNumeric(String compareTo) {
return new BigInteger(version).compareTo(new BigInteger(compareTo));
}
这是 here 提出的解决方案,我一直认为这是从 String
.
创建 BigInteger
的正确方法
但是,IntelliJ 通过 Sonar 插件给出 the following warning:
Constructors should not be used to instantiate "String", "BigInteger", "BigDecimal" and primitive-wrapper classes
squid:S2129
Constructors for Strings, BigInteger, BigDecimal and the objects used to wrap primitives should never be used. Doing so is less clear and uses more memory than simply using the desired value in the case of strings, and using valueOf for everything else.
Further, these constructors are deprecated in Java 9, which is an indication that they will eventually be removed from the language altogether.
Noncompliant Code Example
String empty = new String(); // Noncompliant; yields essentially "", so just use that.
String nonempty = new String("Hello world"); // Noncompliant
Double myDouble = new Double(1.1); // Noncompliant; use valueOf
Integer integer = new Integer(1); // Noncompliant
Boolean bool = new Boolean(true); // Noncompliant
BigInteger bigInteger = new BigInteger("1"); // Noncompliant
BigDecimal bigDecimal = new BigDecimal(1.1); // Noncompliant<br/>
Compliant Solution
String empty = "";
String nonempty = "Hello world";
Double myDouble = Double.valueOf(1.1);
Integer integer = Integer.valueOf(1);
Boolean bool = Boolean.valueOf(true);
BigInteger bigInteger = BigInteger.valueOf(1);
BigDecimal bigDecimal = BigDecimal.valueOf(1.1);
首先,我没有看到 the constructor 在 Java 9 中被弃用,Sonar 在这里错了吗?
我是不是做错了比较并触发了误报,还是应该以其他方式进行比较?
我能想到的唯一其他方法是直接比较字符串,但这也迫使我首先检查字符串是否为数字。
您遇到了这个问题 SONARJAVA-2740,该问题已在最新的 SonarJava 版本中修复,应该会在几天内公开发布。
我正在使用 IntelliJ IDEA 2018.1.3 Ultimate Edition,需要与表示为字符串的大整数(大到无法放入 long
,例如 20180531234240565494
)进行比较:
public int compareNumeric(String compareTo) {
return new BigInteger(version).compareTo(new BigInteger(compareTo));
}
这是 here 提出的解决方案,我一直认为这是从 String
.
BigInteger
的正确方法
但是,IntelliJ 通过 Sonar 插件给出 the following warning:
Constructors should not be used to instantiate "String", "BigInteger", "BigDecimal" and primitive-wrapper classes
squid:S2129
Constructors for Strings, BigInteger, BigDecimal and the objects used to wrap primitives should never be used. Doing so is less clear and uses more memory than simply using the desired value in the case of strings, and using valueOf for everything else.
Further, these constructors are deprecated in Java 9, which is an indication that they will eventually be removed from the language altogether.
Noncompliant Code ExampleString empty = new String(); // Noncompliant; yields essentially "", so just use that. String nonempty = new String("Hello world"); // Noncompliant Double myDouble = new Double(1.1); // Noncompliant; use valueOf Integer integer = new Integer(1); // Noncompliant Boolean bool = new Boolean(true); // Noncompliant BigInteger bigInteger = new BigInteger("1"); // Noncompliant BigDecimal bigDecimal = new BigDecimal(1.1); // Noncompliant<br/>
Compliant Solution
String empty = ""; String nonempty = "Hello world"; Double myDouble = Double.valueOf(1.1); Integer integer = Integer.valueOf(1); Boolean bool = Boolean.valueOf(true); BigInteger bigInteger = BigInteger.valueOf(1); BigDecimal bigDecimal = BigDecimal.valueOf(1.1);
首先,我没有看到 the constructor 在 Java 9 中被弃用,Sonar 在这里错了吗?
我是不是做错了比较并触发了误报,还是应该以其他方式进行比较?
我能想到的唯一其他方法是直接比较字符串,但这也迫使我首先检查字符串是否为数字。
您遇到了这个问题 SONARJAVA-2740,该问题已在最新的 SonarJava 版本中修复,应该会在几天内公开发布。