Skip to content

Commit

Permalink
issue #31: Convert from/to ternary - 2ifelse hints
Browse files Browse the repository at this point in the history
* updated description too
  • Loading branch information
markiewb committed May 14, 2014
1 parent b92096b commit 773cb8d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
@NbBundle.Messages({
"DN_ToTernaryReturn=Convert to ternary return",
"DESC_ToTernaryReturn=Converts if statement to ternary return statement. <br>For example: <tt>if ($cond) {return $a;} else {return $b;}</tt> will be transformed to <tt>return ($cond)?$a:$b;</tt><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",
"DESC_ToTernaryReturn=Converts if statement to ternary return statement. <p>For example: <tt>if ($cond) {return $a;} else {return $b;}</tt> will be transformed to <tt>return ($cond) ? $a : $b;</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",
"DN_ToIfElseReturn=Convert to if/else return",
"DESC_ToIfElseReturn=Converts ternary return statement to if/else statement. <p>For example: <tt>return ($cond) ? $a : $b;</tt> will be transformed to <tt>if ($cond) {return $a;} else {return $b;}</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",
"DN_ToTernaryAssign=Convert to ternary assignment",
"DESC_ToTernaryAssign=Converts if statement to ternary assignment statement. <br>For example: <tt>if ($cond) {$var = $a;}else {$var = $b;}</tt> will be transformed to <tt>$var=($cond)?$a:$b;</tt><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",})
"DESC_ToTernaryAssign=Converts if statement to ternary assignment statement. <p>For example: <tt>if ($cond) {$var = $a;} else {$var = $b;}</tt> will be transformed to <tt>$var = ($cond) ? $a : $b;</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",
"DN_ToIfElseAssign=Convert to if/else assignment",
"DESC_ToIfElseAssign=Converts ternary assignment statement to if/else statement. <p>For example: <tt>$var = ($cond) ? $a : $b;</tt> will be transformed to <tt>if ($cond) {$var = $a;} else {$var = $b;}</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",})
public class ToTernary {

@TriggerPattern(value = "if ($cond) {return $a;} else {return $b;}")
Expand All @@ -28,11 +32,27 @@ 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")
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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");

}

}
20 changes: 20 additions & 0 deletions src/test/java/example/ToTernary.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 773cb8d

Please sign in to comment.