Java 新的BigInteger(字符串)发出声纳警告

Java 新的BigInteger(字符串)发出声纳警告,java,sonarqube,biginteger,Java,Sonarqube,Biginteger,我使用的是IntelliJ IDEA 2018.1.3终极版,需要与表示为字符串的大整数(大到不能放入长的中,例如20180531234240565494)进行比较: public int compareNumeric(String compareTo) { return new BigInteger(version).compareTo(new BigInteger(compareTo)); } 这就是提出的解决方案,我一直认为这是从字符串创建biginger的正确方法 然而,Int

我使用的是IntelliJ IDEA 2018.1.3终极版,需要与表示为字符串的大整数(大到不能放入长的
中,例如
20180531234240565494
)进行比较:

public int compareNumeric(String compareTo) {
    return new BigInteger(version).compareTo(new BigInteger(compareTo));
}
这就是提出的解决方案,我一直认为这是从
字符串创建
biginger
的正确方法

然而,IntelliJ通过声纳插件提供:

构造函数不应用于实例化“String”、“BigInteger”、“BigDecimal”和基本包装类 鱿鱼:S2129
永远不要使用字符串、BigInteger、BigDecimal和用于包装原语的对象的构造函数。与在字符串中使用所需的值以及在其他所有内容中使用valueOf相比,这样做更不清晰,占用更多内存。
此外,这些构造函数在Java 9中被弃用,这表明它们最终将从语言中完全删除。

不合规代码示例

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/>
首先,我不认为Java9中有人反对使用声纳,这是不是错了

我是否做了错误的比较并引发了误报,还是应该以其他方式进行比较


我能想到的唯一其他方法是直接比较字符串,但这也会迫使我首先检查字符串是否为数字。

您遇到的问题已在最新的SonarJava版本中修复,该版本将在几天内公开。

建议的
biginger.valueOf()解决方案
仅适用于可以放入
long
中的值,因此我将忽略该警告(假设您的
字符串
s表示无法放入
long
中的值)@Stultuske警告提到这些构造函数已被弃用,但我没有看到
新的BigInteger(字符串)
已弃用。@Eran或假设字符串不是常量,在这种情况下,您不知道它包含什么值。@Stultuske在Java 10中也不弃用它们。他们甚至不打算在未来几年被弃用。更有可能的是,
biginger
与已弃用的
Integer
Long
构造函数被错误地扔到了同一个容器中。顺便说一句,您的
20180531234240565494
看起来很像日期和时间字符串。您确定
biginger
是此处使用的最佳类型吗?
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);