[java] RedundantFieldInitializer - NumberFormatException with Long

Fixes #1298
This commit is contained in:
Andreas Dangel
2018-08-13 09:55:02 +02:00
parent 7e53729baa
commit f8bb0488d8
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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;
}
]]></code>
</test-code>
<test-code>
<description>#1298 [java] RedundantFieldInitializer - NumberFormatException with Long</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class RedundantFieldInitializerTest {
private long hexLongMinusOne = 0xffffffffffffffffL;
private int hexIntMinusOne = 0xffffffff;
}
]]></code>
</test-code>