From 773cb8de268eb94becce97f354cecc2803d87933 Mon Sep 17 00:00:00 2001 From: markiewb Date: Wed, 14 May 2014 13:57:31 +0200 Subject: [PATCH] issue #31: Convert from/to ternary - 2ifelse hints * updated description too --- pom.xml | 3 ++ .../plugins/hints/ternary/ToTernary.java | 24 +++++++++- .../plugins/hints/ternary/ToTernaryTest.java | 48 +++++++++++++++++++ src/test/java/example/ToTernary.java | 20 ++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/test/java/example/ToTernary.java diff --git a/pom.xml b/pom.xml index f3cd9c8..e6797f2 100644 --- a/pom.xml +++ b/pom.xml @@ -226,7 +226,9 @@ <li>"Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1)</li> <li>"Change modifiers" (since 1.2)</li> <li>"Convert char to string and back" (since 1.2)</li> +<li>"Convert number in literal to number and back" (since 1.2)</li> <li>"Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()" (since 1.2)</li> +<li>"Convert from if/else to ternary and back" (since 1.2)</li> </ul> <h2>Example:</h2> @@ -240,6 +242,7 @@ <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/9">New Hint</a>]: Convert from char and string and back</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/10">New Hint</a>]: Convert number in literal to number and back</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/12">New Hint</a>]: Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()</li> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/31">New Hint</a>]: Convert from if/else to ternary and back</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/29">Updated Hint</a>]: "Convert to assertTrue/assertFalse" now supports junit.framework.Assert too</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/20">Updated Hint</a>]: "Replace +..." hints can now be configured</li> </ul> diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java b/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java index d8cbd15..18e73ba 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java @@ -15,9 +15,13 @@ */ @NbBundle.Messages({ "DN_ToTernaryReturn=Convert to ternary return", - "DESC_ToTernaryReturn=Converts if statement to ternary return statement.
For example: if ($cond) {return $a;} else {return $b;} will be transformed to return ($cond)?$a:$b;

Provided by nb-additional-hints plugin

", + "DESC_ToTernaryReturn=Converts if statement to ternary return statement.

For example: if ($cond) {return $a;} else {return $b;} will be transformed to return ($cond) ? $a : $b;

Provided by nb-additional-hints plugin

", + "DN_ToIfElseReturn=Convert to if/else return", + "DESC_ToIfElseReturn=Converts ternary return statement to if/else statement.

For example: return ($cond) ? $a : $b; will be transformed to if ($cond) {return $a;} else {return $b;}

Provided by nb-additional-hints plugin

", "DN_ToTernaryAssign=Convert to ternary assignment", - "DESC_ToTernaryAssign=Converts if statement to ternary assignment statement.
For example: if ($cond) {$var = $a;}else {$var = $b;} will be transformed to $var=($cond)?$a:$b;

Provided by nb-additional-hints plugin

",}) + "DESC_ToTernaryAssign=Converts if statement to ternary assignment statement.

For example: if ($cond) {$var = $a;} else {$var = $b;} will be transformed to $var = ($cond) ? $a : $b;

Provided by nb-additional-hints plugin

", + "DN_ToIfElseAssign=Convert to if/else assignment", + "DESC_ToIfElseAssign=Converts ternary assignment statement to if/else statement.

For example: $var = ($cond) ? $a : $b; will be transformed to if ($cond) {$var = $a;} else {$var = $b;}

Provided by nb-additional-hints plugin

",}) public class ToTernary { @TriggerPattern(value = "if ($cond) {return $a;} else {return $b;}") @@ -28,6 +32,14 @@ public static ErrorDescription toTernaryReturn(HintContext ctx) { return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryReturn(), fix); } + @TriggerPattern(value = "return ($cond)?$a:$b;") + @Hint(displayName = "#DN_ToIfElseReturn", description = "#DESC_ToIfElseReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToIfElseReturn=Convert to if/else return") + public static ErrorDescription toIfElseReturn(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseReturn(), ctx.getPath(), "if ($cond) {return $a;} else {return $b;}"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseReturn(), fix); + } + @TriggerPattern(value = "if ($cond) {$var = $a;}else {$var = $b;}") @Hint(displayName = "#DN_ToTernaryAssign", description = "#DESC_ToTernaryAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) @NbBundle.Messages("ERR_ToTernaryAssign=Convert to ternary assignment") @@ -35,4 +47,12 @@ public static ErrorDescription toTernaryAssign(HintContext ctx) { Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryAssign(), ctx.getPath(), "$var=($cond)?$a:$b;"); return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryAssign(), fix); } + + @TriggerPattern(value = "$var=($cond)?$a:$b;") + @Hint(displayName = "#DN_ToIfElseAssign", description = "#DESC_ToIfElseAssign", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToIfElseAssign=Convert to if/else assignment") + public static ErrorDescription toIfElseAssign(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if ($cond) {$var = $a;}else {$var = $b;}"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseAssign(), fix); + } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java index 6e4bcff..97d4790 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java @@ -36,6 +36,30 @@ public void testToTernaryReturn() throws Exception { + "}\n"); } + + @Test + public void testToIfElseReturn() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " return (tr|ue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:18-3:18:hint:" + Bundle.ERR_ToIfElseReturn()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " if (true) { return 1; } else { return 0; }\n" + + " }\n" + + "}\n"); + + } + @Test public void testToTernaryAssign() throws Exception { HintTest.create() @@ -60,5 +84,29 @@ public void testToTernaryAssign() throws Exception { + "}\n"); } + @Test + public void testToIfElseAssign() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " s = (t|rue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:14-4:14:hint:" + Bundle.ERR_ToIfElseAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (true) { s = 1; } else { s = 0; }\n" + + " }\n" + + "}\n"); + + } } diff --git a/src/test/java/example/ToTernary.java b/src/test/java/example/ToTernary.java new file mode 100644 index 0000000..b378966 --- /dev/null +++ b/src/test/java/example/ToTernary.java @@ -0,0 +1,20 @@ +/* + * 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 example; + +/** + * + * @author markiewb + */ +public class ToTernary { + + static int s; + + public static int main(String[] args) { + s = (true) ? 1 : 0; + return (true) ? 1 : 0; + } +}