Skip to content

Commit

Permalink
issue #36: Some more "invert ternary if/else" patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed May 18, 2014
1 parent 42765a0 commit 351e69c
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 71 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.markiewb.netbeans.plugins</groupId>
<artifactId>AdditionalHints</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
<packaging>nbm</packaging>

<name>Additional Java hints</name>
Expand Down Expand Up @@ -239,6 +239,11 @@
&lt;img src="https://raw.github.com/markiewb/nb-additional-hints/1.2.0/doc/screenshot.png"/&gt;

&lt;h2&gt;Updates&lt;/h2&gt;
&lt;h3&gt;1.3.0:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/36"&gt;Updated Hint&lt;/a&gt;]: "Invert ternary if/else" now supports more patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;1.2.0:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/pull/22"&gt;New Hint&lt;/a&gt;]: Change the modifier of a class/method/field to public/package protected/protected/private (by &lt;a href="https://github.com/rasa-silva"&gt;rasa-silva&lt;/a&gt;)&lt;/li&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,62 +40,109 @@
* Portions Copyrighted 2014 Sun Microsystems, Inc.
* Portions Copyrighted 2014 benno.markiewicz@googlemail.com
*/
package de.markiewb.netbeans.plugins.hints.ternary;

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;

/**
*
* @author markiewb
*/
@NbBundle.Messages({
"DN_InvertTernary=Invert ternary if/else",
"DESC_InvertTernary=Negates the condition and switches the if/else blocks of a ternary statement. <p>For example: <tt>(a != null) ? \"foo\" : \"bar\"</tt> will be transformed to <tt>(a == null) ? \"bar\" : \"foo\"</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",})
public class InvertTernary {

@TriggerPatterns({
@TriggerPattern(value = "($var1 != null) ? $a : $b"),
@TriggerPattern(value = "($var2 == null) ? $a : $b"),
@TriggerPattern(value = "($var3 > $c) ? $a : $b"),
@TriggerPattern(value = "($var4 < $c) ? $a : $b"),
@TriggerPattern(value = "($var5 >= $c) ? $a : $b"),
@TriggerPattern(value = "($var6 <= $c) ? $a : $b"),
})
@Hint(displayName = "#DN_InvertTernary", description = "#DESC_InvertTernary", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT)
@NbBundle.Messages("ERR_InvertTernary=Invert ternary if/else")
public static ErrorDescription toTernary(HintContext ctx) {
String result = null;
if (ctx.getVariables().containsKey("$var1")) {
result = "($var1 == null) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var2")) {
result = "($var2 != null) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var3")) {
result = "($var3 <= $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var4")) {
result = "($var4 >= $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var5")) {
result = "($var5 < $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var6")) {
result = "($var6 > $c) ? $b : $a";
}
if (result != null) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_InvertTernary(), ctx.getPath(), result);
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_InvertTernary(), fix);
}
return null;
}

}
package de.markiewb.netbeans.plugins.hints.ternary;

import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
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;

/**
*
* @author markiewb
*/
@NbBundle.Messages({
"DN_InvertTernary=Invert ternary if/else",
"DESC_InvertTernary=Negates the condition and switches the if/else blocks of a ternary statement. <p>For example: <tt>(a != null) ? \"foo\" : \"bar\"</tt> will be transformed to <tt>(a == null) ? \"bar\" : \"foo\"</tt></p><p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",})
public class InvertTernary {

@TriggerPatterns({
@TriggerPattern(value = "($var1 != $c) ? $a : $b"),
@TriggerPattern(value = "($var2 == $c) ? $a : $b"),
@TriggerPattern(value = "($var3 > $c) ? $a : $b"),
@TriggerPattern(value = "($var4 < $c) ? $a : $b"),
@TriggerPattern(value = "($var5 >= $c) ? $a : $b"),
@TriggerPattern(value = "($var6 <= $c) ? $a : $b"),
@TriggerPattern(value = "(!$var7) ? $a : $b"),
@TriggerPattern(value = "($var8) ? $a : $b"),
})
@Hint(displayName = "#DN_InvertTernary", description = "#DESC_InvertTernary", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT)
@NbBundle.Messages("ERR_InvertTernary=Invert ternary if/else")
public static ErrorDescription toTernary(HintContext ctx) {
String result = null;
if (ctx.getVariables().containsKey("$var1")) {
result = "($var1 == $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var2")) {
result = "($var2 != $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var3")) {
result = "($var3 <= $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var4")) {
result = "($var4 >= $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var5")) {
result = "($var5 < $c) ? $b : $a";
}
if (ctx.getVariables().containsKey("$var6")) {
result = "($var6 > $c) ? $b : $a";
}
//negated !var7
if (ctx.getVariables().containsKey("$var7")) {
TreePath get = ctx.getVariables().get("$var7");
if (get.getLeaf().getKind() == Tree.Kind.IDENTIFIER)
{
//var is a boolean variable
result = "($var7) ? $b : $a";
}
if (get.getLeaf().getKind() == Tree.Kind.BOOLEAN_LITERAL)
{
//var is a boolean literal like 'true'/'false'
//!true?a:b -> true?b:a
LiteralTree tree=(LiteralTree) get.getLeaf();
if ("true".equals(tree.getValue().toString())){
result = "(true) ? $b : $a";
}
//!true?a:b -> false?b:a
if ("false".equals(tree.getValue().toString())){
result = "(false) ? $b : $a";
}
}
}
//non-negated var8
if (ctx.getVariables().containsKey("$var8")) {
TreePath get = ctx.getVariables().get("$var8");
if (get.getLeaf().getKind() == Tree.Kind.IDENTIFIER) {
//var is a boolean variable
result = "(!$var8) ? $b : $a";
}
if (get.getLeaf().getKind() == Tree.Kind.BOOLEAN_LITERAL)
{
//var is a boolean literal like 'true'/'false'
LiteralTree tree=(LiteralTree) get.getLeaf();
if ("true".equals(tree.getValue().toString())){
result = "(false) ? $b : $a";
}
if ("false".equals(tree.getValue().toString())){
result = "(true) ? $b : $a";
}
}

}
if (result != null) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_InvertTernary(), ctx.getPath(), result);
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_InvertTernary(), fix);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
* @author markiewb
*/
public class InvertTernaryTest {
// "($var1 != null) ? $a : $b"),
// "($var2 == null) ? $a : $b"),
// "($var3 > $c) ? $a : $b"),
// "($var4 < $c) ? $a : $b"),
// "($var5 >= $c) ? $a : $b"),
// "($var6 =< $c) ? $a : $b"),
// @TriggerPattern(value = "($var1 != $c) ? $a : $b"),
// @TriggerPattern(value = "($var2 == $c) ? $a : $b"),
// @TriggerPattern(value = "($var3 > $c) ? $a : $b"),
// @TriggerPattern(value = "($var4 < $c) ? $a : $b"),
// @TriggerPattern(value = "($var5 >= $c) ? $a : $b"),
// @TriggerPattern(value = "($var6 <= $c) ? $a : $b"),
// @TriggerPattern(value = "(!$var7) ? $a : $b"),
// @TriggerPattern(value = "($var8) ? $a : $b"),

@Test
public void testNotEquals() throws Exception {
Expand All @@ -36,7 +38,7 @@ public void testNotEquals() throws Exception {
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Double val = null;\n"
+ " int a = (v|al != null) ? 1 : 0; \n"
+ " int a = (v|al != 9.0) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
Expand All @@ -47,7 +49,7 @@ public void testNotEquals() throws Exception {
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Double val = null;\n"
+ " int a = (val == null) ? 0 : 1; \n"
+ " int a = (val == 9.0) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

Expand All @@ -61,7 +63,7 @@ public void testEquals() throws Exception {
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Double val = null;\n"
+ " int a = (v|al == null) ? 1 : 0; \n"
+ " int a = (v|al == 9.0) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
Expand All @@ -72,7 +74,7 @@ public void testEquals() throws Exception {
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Double val = null;\n"
+ " int a = (val != null) ? 0 : 1; \n"
+ " int a = (val != 9.0) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

Expand Down Expand Up @@ -177,5 +179,145 @@ public void testLowerEquals() throws Exception {
+ "}\n");

}

@Test
public void testTrueVariable() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " boolean b = true;\n"
+ " int a = (b|) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("4:18-4:18:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " boolean b = true;\n"
+ " int a = (!b) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}

@Test
public void testTrueLiteral() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (tru|e) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("3:20-3:20:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (false) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}

@Test
public void testTrueLiteralNot() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (!tru|e) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("3:21-3:21:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (true) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}

@Test
public void testFalseVariable() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " boolean b = true;\n"
+ " int a = (!|b) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("4:18-4:18:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " boolean b = true;\n"
+ " int a = (b) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}
@Test
public void testFalseLiteral() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (fa|lse) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("3:19-3:19:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (true) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}
@Test
public void testFalseLiteralNot() throws Exception {
HintTest.create()
.setCaretMarker('|')
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (!fa|lse) ? 1 : 0; \n"
+ " }\n"
+ "}\n")
.run(InvertTernary.class)
.findWarning("3:20-3:20:hint:" + Bundle.ERR_InvertTernary())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " int a = (false) ? 0 : 1; \n"
+ " }\n"
+ "}\n");

}

}
Loading

0 comments on commit 351e69c

Please sign in to comment.