diff --git a/README.md b/README.md index dc4f181..5342ae1 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,126 @@ [![Build Status](https://travis-ci.org/markiewb/nb-additional-hints.svg?branch=master)](https://travis-ci.org/markiewb/nb-additional-hints) [![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=K4CMP92RZELE2) -Additional java hints for NetBeans IDE -=================== +Plugin page: http://plugins.netbeans.org/plugin/47589/ -Supported hints: -* "Replace '+' with 'MessageFormat.format()'" -* "Replace '+' with 'new StringBuilder().append()'" -* "Replace '+' with 'String.format()'" -* "Join literals" -* "Copy joined literals to clipboard" -* "Split at linebreaks" -* "Split at caret" (since 1.1) -* "Convert to assertTrue/assertFalse" (since 1.1) -* "Support transformation to BigDecimal constants" (since 1.1) -* "Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1) -* ... and more see http://plugins.netbeans.org/plugin/47589/?show=true +

Additional Java hints for NetBeans IDE

+

Supported Hints:

-Example: --------- + -![Screenshot](https://raw.github.com/markiewb/nb-additional-hints/v1.4.0/doc/screenshot.png) +

Example:

+ -Further information: --------------------- -This plugin is originally based on code from the _"I18N Checker"_ plugin from _Jan Lahoda_. -The original sourcecode can be found at http://hg.netbeans.org/main/contrib/file/tip/editor.hints.i18n +

Updates

+

1.6.1:

+ +

1.6.0:

+ +

1.5.0:

+ +

1.4.0:

+ +

1.3.0:

+ -License remains CDDL-GPL-2-CP - http://www.netbeans.org/cddl-gplv2.html +

1.2.0:

+ -Compatible to NetBeans 7.4 and higher +

1.1.0:

+ +

1.0.x:

+ +

+Further information:

+ +

This plugin is orginally based on code from the "I18N Checker" plugin from Jan Lahoda. +The original sourcecode can be found at http://hg.netbeans.org/main/contrib/file/tip/editor.hints.i18n

+ +

License remains CDDL-GPL-2-CP - http://www.netbeans.org/cddl-gplv2.html

+ +

+Provide defects, request for enhancements and feedback at https://github.com/markiewb/nb-additional-hints/issues +

+

Compatible to NetBeans 8.1+

+

+btn_donate_SM.gif + +

\ No newline at end of file diff --git a/doc/screenshot.png b/doc/screenshot.png index 80c57c3..754683f 100644 Binary files a/doc/screenshot.png and b/doc/screenshot.png differ diff --git a/pom.xml b/pom.xml index 1ef7000..4f55be5 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ de.markiewb.netbeans.plugins AdditionalHints - 1.6.0.10 + 1.6.1.0 nbm Additional Java hints @@ -220,9 +220,13 @@ </ul> <h2>Example:</h2> -<img src="https://raw.githubusercontent.com/markiewb/nb-additional-hints/v1.4.0/doc/screenshot.png"/> +<img src="https://raw.githubusercontent.com/markiewb/nb-additional-hints/v1.6.1/doc/screenshot.png"/> <h2>Updates</h2> +<h3>1.6.1:</h3> +<ul> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/78">Bugfix</a>]: Fixed: Hints for converting ternary to if and back are missing</li> +</ul> <h3>1.6.0:</h3> <ul> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/55">New Fix</a>]: Replace with null-assignment to Optional with Optional.empty()</li> 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..da6b4da --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernary.java @@ -0,0 +1,126 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2014 Sun Microsystems, Inc. + * Portions Copyrighted 2014 benno.markiewicz@googlemail.com + */ +package de.markiewb.netbeans.plugins.hints.ternary; + +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_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

", + "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", + "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", + "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;}") + @Hint(displayName = "#DN_ToTernaryReturn", description = "#DESC_ToTernaryReturn", category = "suggestions", hintKind = Hint.Kind.ACTION, severity = Severity.HINT) + @NbBundle.Messages("ERR_ToTernaryReturn=Convert to ternary return") + 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 = "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") + 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); + } + + @TriggerPatterns( + { +// @TriggerPattern(value = "$var1=($cond)?$a:$b;"), + @TriggerPattern(value = "$var2=$cond?$a:$b;"), +// @TriggerPattern(value = "$type $var3=($cond)?$a:$b;"), + @TriggerPattern(value = "$type $var4=$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") + public static ErrorDescription toIfElseAssign(HintContext ctx) { + + Fix fix = null; +// if (ctx.getVariables().containsKey("$var1")) { +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if (($cond)) {$var1 = $a;} else {$var1 = $b;}"); +// } + if (ctx.getVariables().containsKey("$var2")) { + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "if ($cond) {$var2 = $a;} else {$var2 = $b;}"); + } +// if (ctx.getVariables().containsKey("$var3")) { +// fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var3; if (($cond)) {$var3 = $a;} else {$var3 = $b;}"); +// } + if (ctx.getVariables().containsKey("$var4")) { + fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var4; if ($cond) {$var4 = $a;} else {$var4 = $b;}"); + } + if (null != fix) { + return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ToIfElseAssign(), fix); + } else { + return null; + } + } +} 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..01bcf51 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/ternary/ToTernaryTest.java @@ -0,0 +1,192 @@ +/* + * 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 org.junit.Ignore; +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; + +/** + * + * @author markiewb + */ +public class ToTernaryTest { + + @Test + public void testToTernaryReturn() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " if (tr|ue){return 1;}else{return 0;}\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:14-3:14:hint:" + Bundle.ERR_ToTernaryReturn()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static int main(String[] args) {\n" + + " return (true) ? 1 : 0;\n" + + " }\n" + + "}\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"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @Test + public void testToTernaryAssign() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " if (t|rue){s = 1;}else{s = 0;}\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("4:13-4:13:hint:" + Bundle.ERR_ToTernaryAssign()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s;\n" + + " s = (true) ? 1 : 0;\n" + + " }\n" + + "}\n"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @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"); + + } + + @Ignore(value = "Since NB8.1: java.lang.IllegalArgumentException: No document available for MIME type: text/x-java\n" + +" at org.netbeans.api.editor.document.LineDocumentUtils.createDocument(LineDocumentUtils.java:554)") + @Test + public void testToIfElseAssignWithoutBrackets() 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:13-4:13: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"); + + } + + @Test + public void testToIfElseAssignNewVariable() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s = (t|rue) ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:18-3:18: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"); + + } + + @Test + public void testToIfElseAssignNewVariableWithoutBrackets() throws Exception { + HintTest.create() + .setCaretMarker('|') + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " int s = t|rue ? 1 : 0;\n" + + " }\n" + + "}\n") + .run(ToTernary.class) + .findWarning("3:17-3:17: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"); + + } +}