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
new file mode 100644
index 0000000..640c34c
--- /dev/null
+++ b/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java
@@ -0,0 +1,38 @@
+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.openide.util.NbBundle;
+
+/**
+ *
+ * @author markiewb
+ */
+@NbBundle.Messages({
+ "DN_ToTernaryReturn=Convert to XXX",
+ "DESC_ToTernaryReturn=Converts xxxx.
For example: xxxx will be transformed to xxx
Provided by nb-additional-hints plugin
", + "DN_ToTernaryAssign=Convert to XXX", + "DESC_ToTernaryAssign=Converts xxxx.Provided by nb-additional-hints plugin
",}) +public class ToTernary { + + @TriggerPattern(value = "if ($cond$){return $a;}else{return $b;}") + @Hint(displayName = "#DN_ToTernaryReturn", description = "#DESC_ToTernaryReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToTernaryReturn=XXX") + public static ErrorDescription toTernaryReturn(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryReturn(), ctx.getPath(), "return ($cond$)?$a:$b;"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryReturn(), fix); + } + + @TriggerPattern(value = "$type $var;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=XXX") + public static ErrorDescription toTernaryAssign(HintContext ctx) { + Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToTernaryAssign(), ctx.getPath(), "$type $var = ($cond$)?$a:$b;"); + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToTernaryAssign(), 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 new file mode 100644 index 0000000..59df1b7 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java @@ -0,0 +1,50 @@ +/* + * 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.ternary; + +import de.markiewb.netbeans.plugins.hints.apache.commons.ToBlankFix; +import org.apache.commons.lang.StringUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.java.hints.HintContext; +import org.openide.filesystems.FileUtil; + +/** + * + * @author markiewb + */ +public class ToTernaryTest { + + @Test + public void testToTernaryAssign() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s; if (true){s = 1;}else{s = 0;};\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:18-4:53:hint:" + Bundle.ERR_ToTernaryAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " int s = (true)?1:0;\n" + + " }\n" + + "}\n"); + + } + +}