diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b341ed6703..9d9642e4ee 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -43,6 +43,8 @@ This is a minor release. * [#1258](https://github.com/pmd/pmd/issues/1285): \[java] False positive "UselessParentheses" for parentheses that contain assignment * java-errorprone * [#1078](https://github.com/pmd/pmd/issues/1078): \[java] MissingSerialVersionUID rule does not seem to catch inherited classes +* java-performance + * [#1298](https://github.com/pmd/pmd/issues/1298): \[java] RedundantFieldInitializer - NumberFormatException with Long * jsp * [#1274](https://github.com/pmd/pmd/issues/1274): \[jsp] Support EL in tag attributes * [#1276](https://github.com/pmd/pmd/issues/1276): \[jsp] add support for jspf and tag extensions diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java index 16e0d4db5b..2a18732390 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.ast; +import java.math.BigInteger; import java.util.Locale; import java.util.regex.Pattern; @@ -146,11 +147,14 @@ public class ASTLiteral extends AbstractJavaTypeNode { } public int getValueAsInt() { - return (int) getValueAsLong(); // the downcast allows to parse 0x80000000+ numbers as negative instead of a NumberFormatException + // the downcast allows to parse 0x80000000+ numbers as negative instead of a NumberFormatException + return (int) getValueAsLong(); } public long getValueAsLong() { - return Long.parseLong(stripIntValue(), getIntBase()); + // Using BigInteger to allow parsing 0x8000000000000000+ numbers as negative instead of a NumberFormatException + BigInteger bigInt = new BigInteger(stripIntValue(), getIntBase()); + return bigInt.longValue(); } public float getValueAsFloat() { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml index 3722c2b586..5870880e9b 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/RedundantFieldInitializer.xml @@ -1405,6 +1405,17 @@ public class Test { int doc3 = 0b0001_0010_0100_1000; double doc4 = 3.141_592_653_589_793d; double doc5 = 0x1.ffff_ffff_ffff_fP1_023; +} + ]]> + + + + #1298 [java] RedundantFieldInitializer - NumberFormatException with Long + 0 +