From 1ecf87c2b5da7183491380ca680459bf98b3a794 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 4 Oct 2017 20:54:42 +0200 Subject: [PATCH] [java] Fix RedundantFieldInitializerRule not able to parse java7 literals with underscore --- .../RedundantFieldInitializerRule.java | 4 +- .../xml/RedundantFieldInitializer.xml | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/optimizations/RedundantFieldInitializerRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/optimizations/RedundantFieldInitializerRule.java index 3cee0a6bd4..75a728a1d6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/optimizations/RedundantFieldInitializerRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/optimizations/RedundantFieldInitializerRule.java @@ -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); } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/optimizations/xml/RedundantFieldInitializer.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/optimizations/xml/RedundantFieldInitializer.xml index 5a72358454..3722c2b586 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/optimizations/xml/RedundantFieldInitializer.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/optimizations/xml/RedundantFieldInitializer.xml @@ -1322,6 +1322,89 @@ public class SomeClass { + + + + java7 numeric literals + 4 + 8,21,31,47 +