[java] Fix RedundantFieldInitializerRule not able to parse java7 literals with underscore

This commit is contained in:
Andreas Dangel
2017-10-04 20:54:42 +02:00
parent 6fc758c21a
commit 1ecf87c2b5
2 changed files with 85 additions and 2 deletions

View File

@ -95,9 +95,9 @@ public class RedundantFieldInitializerRule extends AbstractJavaRule {
// remove the ending "f" or "F" for float
// values
s = s.substring(0, s.length() - 1);
value = Float.valueOf(s);
value = Float.valueOf(s.replaceAll("_", ""));
} else if (literal.isDoubleLiteral()) {
value = Double.valueOf(literal.getImage());
value = Double.valueOf(literal.getImage().replaceAll("_", ""));
} else if (literal.isCharLiteral()) {
value = (int) literal.getImage().charAt(1);
}

View File

@ -1322,6 +1322,89 @@ public class SomeClass {
<code><![CDATA[
public @interface SomeAnnotation {
int CONSTANT = 0;
}
]]></code>
</test-code>
<test-code>
<description>java7 numeric literals</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>8,21,31,47</expected-linenumbers>
<code><![CDATA[
public class Test {
int i1 = 0b00011110;
int i2 = 0B00011110;
int i3 = 0xA;
int i4 = 0x1___A_F;
int i5 = 0b1;
int i6 = 0b1___1_0;
int i7 = 0;
int i8 = 02;
int i9 = 0_123;
int i10 = 1;
int i11 = 1___3;
int i12 = 1_43_43598_7;
long l1 = 0b00011110L;
long l2 = 0B00011110l;
long l3 = 0xAL;
long l4 = 0x1___A_FL;
long l5 = 0b1L;
long l6 = 0b1___1_0L;
long l7 = 0l;
long l8 = 02L;
long l9 = 0_123l;
long l10 = 1l;
long l11 = 1___3l;
long l12 = 1_43_43598_7L;
long l13 = 1_43_43598_7;
float f1 = .1f;
float f2 = 1.f;
float f3 = 0f;
float f4 = 1e0F;
float f5 = 1e0f;
float f6 = 12.345F;
float f7 = .5____2_1f;
float f8 = 1__42__3.f;
float f9 = 0__2_4__324f;
float f10 = 1_34e0F;
float f11 = 1__1_2e0f;
float f12 = 2_1___2.3__4_5F;
float f13 = 1_34e0__4__3f;
float f14 = 1__1_2e00__000_4f;
float f15 = 2_1___2.3__4_5e00______0_5F;
double d1 = .1d;
double d2 = 1.D;
double d3 = 0d;
double d4 = 1e0D;
double d5 = 1e0d;
double d6 = 12.345D;
double d7 = .5____2_1d;
double d8 = 1__42__3.D;
double d9 = 0__2_4__324d;
double d10 = 1_34e0d;
double d11 = 1__1_2e0d;
double d12 = 2_1___2.3__4_5D;
double d13 = 1_34e0__4__3d;
double d14 = 1__1_2e00__000_4d;
double d15 = 2_1___2.3__4_5e00______0_5D;
double d16 = 0.12___34;
float hf1 = 0x.1___AFp1;
float hf2 = 0x.1___AFp0__0__0f;
float hf3 = 0x2__3_34.4___AFP00_00f;
double hd1 = 0x.1___AFp1;
double hd2 = 0x.1___AFp0__0__0d;
double hd3 = 0x2__3_34.4___AFP00_00d;
int doc1 = 1234_5678;
long doc2 = 1_2_3_4__5_6_7_8L;
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>