From a73f707ebddde38f98021e868ba68d78a14d732f Mon Sep 17 00:00:00 2001 From: markiewb Date: Wed, 6 Nov 2013 23:35:14 +0100 Subject: [PATCH] issue #11: Support transformation to BigDecimal constants --- pom.xml | 3 +- .../UseBigDecimalConstantsFixONE.java | 49 +++++++++ .../UseBigDecimalConstantsFixTEN.java | 49 +++++++++ .../UseBigDecimalConstantsFixZERO.java | 48 +++++++++ .../UseBigDecimalConstantsFixONETest.java | 101 ++++++++++++++++++ .../UseBigDecimalConstantsFixTENTest.java | 101 ++++++++++++++++++ .../UseBigDecimalConstantsFixZEROTest.java | 101 ++++++++++++++++++ src/test/java/example/ExampleBigDecimal.java | 75 +++++++++++++ 8 files changed, 526 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONE.java create mode 100644 src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTEN.java create mode 100644 src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZERO.java create mode 100644 src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONETest.java create mode 100644 src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTENTest.java create mode 100644 src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZEROTest.java create mode 100644 src/test/java/example/ExampleBigDecimal.java diff --git a/pom.xml b/pom.xml index 37574cc..32d4177 100644 --- a/pom.xml +++ b/pom.xml @@ -224,8 +224,9 @@ <h2>Updates</h2> <h3>1.1.0:</h3> <ul> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/11">Feature 11</a>]: Support transformation to BigDecimal constants</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/5">Feature 5</a>]: Split a string at caret</li> -<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/5">Feature 13</a>]: Convert to assertTrue/assertFalse</li> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/13">Feature 13</a>]: Convert to assertTrue/assertFalse</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/8">Bug 8</a>]: Literals with quoted Strings won't be copied properly to clipboard</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/7">Refactoring 7</a>]: Convert to maven based module</li> </ul> diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONE.java b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONE.java new file mode 100644 index 0000000..0d4c89c --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONE.java @@ -0,0 +1,49 @@ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.TriggerPattern; +import org.netbeans.spi.java.hints.TriggerPatterns; +import org.openide.util.NbBundle.Messages; + +@Messages({ + "ERR_UseBigDecimalConstantsFixONE=Convert to BigDecimal.ONE", + "DN_UseBigDecimalConstantsONE=Convert to BigDecimal.ONE", + "DESC_UseBigDecimalConstantsONE=Converts expressions like new java.math.BigDecimal(1) to BigDecimal.ONE ",}) +public class UseBigDecimalConstantsFixONE { + + @Hint(displayName = "#DN_UseBigDecimalConstantsONE", description = "#DESC_UseBigDecimalConstantsONE", category = "Suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern("new java.math.BigDecimal(\"1\")"), + @TriggerPattern("new java.math.BigDecimal(\"1.0\")"), + @TriggerPattern("new java.math.BigDecimal(\"1.00\")"), + @TriggerPattern("new java.math.BigDecimal(1)"), + @TriggerPattern("new java.math.BigDecimal(1L)"), + @TriggerPattern("new java.math.BigDecimal(1f)"), + @TriggerPattern("new java.math.BigDecimal(1.0f)"), + @TriggerPattern("new java.math.BigDecimal(1.00f)"), + @TriggerPattern("new java.math.BigDecimal(1d)"), + @TriggerPattern("new java.math.BigDecimal(1.0d)"), + @TriggerPattern("new java.math.BigDecimal(1.00d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1L)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1.0f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1.00f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1.0d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(1.00d)"),} + ) + public static ErrorDescription convertToZero(HintContext ctx) { + + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_UseBigDecimalConstantsFixONE(), ctx.getPath(), "java.math.BigDecimal.ONE"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_UseBigDecimalConstantsFixONE(), fix); + } + + +} diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTEN.java b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTEN.java new file mode 100644 index 0000000..42b7dbe --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTEN.java @@ -0,0 +1,49 @@ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.TriggerPattern; +import org.netbeans.spi.java.hints.TriggerPatterns; +import org.openide.util.NbBundle.Messages; + +@Messages({ + "ERR_UseBigDecimalConstantsFixTEN=Convert to BigDecimal.TEN", + "DN_UseBigDecimalConstantsTEN=Convert to BigDecimal.TEN", + "DESC_UseBigDecimalConstantsTEN=Converts expressions like new java.math.BigDecimal(10) to BigDecimal.TEN ",}) +public class UseBigDecimalConstantsFixTEN { + + @Hint(displayName = "#DN_UseBigDecimalConstantsTEN", description = "#DESC_UseBigDecimalConstantsTEN", category = "Suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern("new java.math.BigDecimal(\"10\")"), + @TriggerPattern("new java.math.BigDecimal(\"10.0\")"), + @TriggerPattern("new java.math.BigDecimal(\"10.00\")"), + @TriggerPattern("new java.math.BigDecimal(10)"), + @TriggerPattern("new java.math.BigDecimal(10L)"), + @TriggerPattern("new java.math.BigDecimal(10f)"), + @TriggerPattern("new java.math.BigDecimal(10.0f)"), + @TriggerPattern("new java.math.BigDecimal(10.00f)"), + @TriggerPattern("new java.math.BigDecimal(10d)"), + @TriggerPattern("new java.math.BigDecimal(10.0d)"), + @TriggerPattern("new java.math.BigDecimal(10.00d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10L)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10.0f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10.00f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10.0d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(10.00d)"),} + ) + public static ErrorDescription convertToZero(HintContext ctx) { + + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_UseBigDecimalConstantsFixTEN(), ctx.getPath(), "java.math.BigDecimal.TEN"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_UseBigDecimalConstantsFixTEN(), fix); + } + + +} diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZERO.java b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZERO.java new file mode 100644 index 0000000..85f667b --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZERO.java @@ -0,0 +1,48 @@ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.TriggerPattern; +import org.netbeans.spi.java.hints.TriggerPatterns; +import org.openide.util.NbBundle.Messages; + +@Messages({ + "ERR_UseBigDecimalConstantsFixZERO=Convert to BigDecimal.ZERO", + "DN_UseBigDecimalConstantsZERO=Convert to BigDecimal.ZERO", + "DESC_UseBigDecimalConstantsZERO=Converts expressions like new java.math.BigDecimal(0) to use BigDecimal.ZERO ",}) +public class UseBigDecimalConstantsFixZERO { + + @Hint(displayName = "#DN_UseBigDecimalConstantsZERO", description = "#DESC_UseBigDecimalConstantsZERO", category = "Suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern("new java.math.BigDecimal(\"0\")"), + @TriggerPattern("new java.math.BigDecimal(\"0.0\")"), + @TriggerPattern("new java.math.BigDecimal(\"0.00\")"), + @TriggerPattern("new java.math.BigDecimal(0)"), + @TriggerPattern("new java.math.BigDecimal(0L)"), + @TriggerPattern("new java.math.BigDecimal(0f)"), + @TriggerPattern("new java.math.BigDecimal(0.0f)"), + @TriggerPattern("new java.math.BigDecimal(0.00f)"), + @TriggerPattern("new java.math.BigDecimal(0d)"), + @TriggerPattern("new java.math.BigDecimal(0.0d)"), + @TriggerPattern("new java.math.BigDecimal(0.00d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0L)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0.0f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0.00f)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0.0d)"), + @TriggerPattern("java.math.BigDecimal.valueOf(0.00d)"),} + ) + public static ErrorDescription convertToZero(HintContext ctx) { + + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_UseBigDecimalConstantsFixZERO(), ctx.getPath(), "java.math.BigDecimal.ZERO"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_UseBigDecimalConstantsFixZERO(), fix); + } + +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONETest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONETest.java new file mode 100644 index 0000000..38f7118 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixONETest.java @@ -0,0 +1,101 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; + +public class UseBigDecimalConstantsFixONETest { + + @Test + public void testConvert_A() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"1\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixONE.class) + .findWarning("3:31-3:60:hint:" + Bundle.ERR_UseBigDecimalConstantsFixONE()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ONE;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_B() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"1.0\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixONE.class) + .findWarning("3:31-3:62:hint:" + Bundle.ERR_UseBigDecimalConstantsFixONE()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ONE;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_C() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(1.0);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixONE.class) + .findWarning("3:31-3:60:hint:" + Bundle.ERR_UseBigDecimalConstantsFixONE()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ONE;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_D() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(1.00d);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixONE.class) + .findWarning("3:31-3:62:hint:" + Bundle.ERR_UseBigDecimalConstantsFixONE()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ONE;\n" + + " }\n" + + "}\n"); + + } +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTENTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTENTest.java new file mode 100644 index 0000000..5aa288c --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixTENTest.java @@ -0,0 +1,101 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; + +public class UseBigDecimalConstantsFixTENTest { + + @Test + public void testConvert_A() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"10\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixTEN.class) + .findWarning("3:31-3:61:hint:" + Bundle.ERR_UseBigDecimalConstantsFixTEN()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.TEN;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_B() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"10.0\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixTEN.class) + .findWarning("3:31-3:63:hint:" + Bundle.ERR_UseBigDecimalConstantsFixTEN()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.TEN;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_C() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(10.0);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixTEN.class) + .findWarning("3:31-3:61:hint:" + Bundle.ERR_UseBigDecimalConstantsFixTEN()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.TEN;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_D() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(10.00d);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixTEN.class) + .findWarning("3:31-3:63:hint:" + Bundle.ERR_UseBigDecimalConstantsFixTEN()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.TEN;\n" + + " }\n" + + "}\n"); + + } +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZEROTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZEROTest.java new file mode 100644 index 0000000..109d9e8 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/bigdecimal/UseBigDecimalConstantsFixZEROTest.java @@ -0,0 +1,101 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.bigdecimal; + +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; + +public class UseBigDecimalConstantsFixZEROTest { + + @Test + public void testConvert_A() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"0\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixZERO.class) + .findWarning("3:31-3:60:hint:" + Bundle.ERR_UseBigDecimalConstantsFixZERO()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ZERO;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_B() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(\"0.0\");\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixZERO.class) + .findWarning("3:31-3:62:hint:" + Bundle.ERR_UseBigDecimalConstantsFixZERO()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ZERO;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_C() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(0.0);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixZERO.class) + .findWarning("3:31-3:60:hint:" + Bundle.ERR_UseBigDecimalConstantsFixZERO()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ZERO;\n" + + " }\n" + + "}\n"); + + } + @Test + public void testConvert_D() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=new java.math.BigDecimal(0.00d);\n" + + " }\n" + + "}\n") + .run(UseBigDecimalConstantsFixZERO.class) + .findWarning("3:31-3:62:hint:" + Bundle.ERR_UseBigDecimalConstantsFixZERO()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import java.math.BigDecimal;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " java.math.BigDecimal a=BigDecimal.ZERO;\n" + + " }\n" + + "}\n"); + + } +} diff --git a/src/test/java/example/ExampleBigDecimal.java b/src/test/java/example/ExampleBigDecimal.java new file mode 100644 index 0000000..bf0fa92 --- /dev/null +++ b/src/test/java/example/ExampleBigDecimal.java @@ -0,0 +1,75 @@ +package example; + +import java.math.BigDecimal; +import java.util.Date; + +public class ExampleBigDecimal { + + public static void testZero() { + BigDecimal[] foo = new BigDecimal[]{ + new java.math.BigDecimal("0"), + new java.math.BigDecimal("0.0"), + new java.math.BigDecimal("0.00"), + new java.math.BigDecimal(0), + new java.math.BigDecimal(0L), + new java.math.BigDecimal(0f), + new java.math.BigDecimal(0.0f), + new java.math.BigDecimal(0.00f), + new java.math.BigDecimal(0d), + new java.math.BigDecimal(0.0d), + new java.math.BigDecimal(0.00d), + java.math.BigDecimal.valueOf(0), + java.math.BigDecimal.valueOf(0L), + java.math.BigDecimal.valueOf(0f), + java.math.BigDecimal.valueOf(0.0f), + java.math.BigDecimal.valueOf(0.00f), + java.math.BigDecimal.valueOf(0d), + java.math.BigDecimal.valueOf(0.0d), + java.math.BigDecimal.valueOf(0.00d)}; + } + + public static void testOne() { + BigDecimal[] foo = new BigDecimal[]{ + new java.math.BigDecimal("1"), + new java.math.BigDecimal("1.0"), + new java.math.BigDecimal("1.00"), + new java.math.BigDecimal(1), + new java.math.BigDecimal(1L), + new java.math.BigDecimal(1f), + new java.math.BigDecimal(1.0f), + new java.math.BigDecimal(1.00f), + new java.math.BigDecimal(1d), + new java.math.BigDecimal(1.0d), + new java.math.BigDecimal(1.00d), + java.math.BigDecimal.valueOf(1), + java.math.BigDecimal.valueOf(1L), + java.math.BigDecimal.valueOf(1f), + java.math.BigDecimal.valueOf(1.0f), + java.math.BigDecimal.valueOf(1.00f), + java.math.BigDecimal.valueOf(1d), + java.math.BigDecimal.valueOf(1.0d), + java.math.BigDecimal.valueOf(1.00d)}; + } + public static void testTen() { + BigDecimal[] foo = new BigDecimal[]{ + new java.math.BigDecimal("10"), + new java.math.BigDecimal("10.0"), + new java.math.BigDecimal("10.00"), + new java.math.BigDecimal(10), + new java.math.BigDecimal(10L), + new java.math.BigDecimal(10f), + new java.math.BigDecimal(10.0f), + new java.math.BigDecimal(10.00f), + new java.math.BigDecimal(10d), + new java.math.BigDecimal(10.0d), + new java.math.BigDecimal(10.00d), + java.math.BigDecimal.valueOf(10), + java.math.BigDecimal.valueOf(10L), + java.math.BigDecimal.valueOf(10f), + java.math.BigDecimal.valueOf(10.0f), + java.math.BigDecimal.valueOf(10.00f), + java.math.BigDecimal.valueOf(10d), + java.math.BigDecimal.valueOf(10.0d), + java.math.BigDecimal.valueOf(10.00d)}; + } +}