Skip to content

Commit

Permalink
issue #11: Support transformation to BigDecimal constants
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed Nov 6, 2013
1 parent c1f9391 commit a73f707
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <tt>BigDecimal.ONE</tt>",
"DN_UseBigDecimalConstantsONE=Convert to <tt>BigDecimal.ONE</tt>",
"DESC_UseBigDecimalConstantsONE=Converts expressions like <tt>new java.math.BigDecimal(1)</tt> to <tt>BigDecimal.ONE</tt> ",})
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);
}


}
Original file line number Diff line number Diff line change
@@ -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 <tt>BigDecimal.TEN</tt>",
"DN_UseBigDecimalConstantsTEN=Convert to <tt>BigDecimal.TEN</tt>",
"DESC_UseBigDecimalConstantsTEN=Converts expressions like <tt>new java.math.BigDecimal(10)</tt> to <tt>BigDecimal.TEN</tt> ",})
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);
}


}
Original file line number Diff line number Diff line change
@@ -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 <tt>BigDecimal.ZERO</tt>",
"DN_UseBigDecimalConstantsZERO=Convert to <tt>BigDecimal.ZERO</tt>",
"DESC_UseBigDecimalConstantsZERO=Converts expressions like <tt>new java.math.BigDecimal(0)</tt> to use <tt>BigDecimal.ZERO</tt> ",})
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);
}

}
Original file line number Diff line number Diff line change
@@ -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");

}
}
Original file line number Diff line number Diff line change
@@ -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");

}
}
Loading

0 comments on commit a73f707

Please sign in to comment.