Skip to content

Commit

Permalink
Hints for converting ternary to if and back are missing #78 and bump …
Browse files Browse the repository at this point in the history
…to version 1.6.1
  • Loading branch information
markiewb committed Jan 14, 2017
1 parent b281c68 commit 0ecbe63
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 25 deletions.
139 changes: 116 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: <a href="http://plugins.netbeans.org/plugin/47589/">http://plugins.netbeans.org/plugin/47589/</a>

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
<h1>Additional Java hints for NetBeans IDE</h1>

<h2>Supported Hints:</h2>

Example:
--------
<ul>
<li>"Replace '+' with 'MessageFormat.format()'"</li>
<li>"Replace '+' with 'new StringBuilder().append()'"</li>
<li>"Replace '+' with 'String.format()'"</li>
<li>"Join literals"</li>
<li>"Copy joined literals to clipboard"</li>
<li>"Split at linebreaks"</li>
<li>"Split at caret" (since 1.1)</li>
<li>"Convert to assertTrue/assertFalse" (since 1.1)</li>
<li>"Support transformation to BigDecimal constants" (since 1.1)</li>
<li>"Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1)</li>
<li>"Remove public/abstract/final modifiers from field declarations within interfaces" (since 1.2)</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>
<li>"Invert ternary if/else" (since 1.2)</li>
<li>"Report methods that have class name" (since 1.2, no transformation)</li>
<li>"Add "this." to methods and variables" (since 1.3, disabled by default)</li>
<li>"Replace with Optional.isPresent()/Convert return null to return Optional.empty()" (since 1.5)</li>
<li>"Replace with null-assignment to Optional with Optional.empty()| (since 1.6)</li>
<li>"Convert return xxx to return Optional.ofNullable(xxx)/Optional.of(xxx)/Optional.empty()| (since 1.6)</li>
<li>"Convert to assertNull" (since 1.6)</li>
<li>"Replace with org.junit.Assert" (since 1.6)</li>
</ul>

![Screenshot](https://raw.github.com/markiewb/nb-additional-hints/v1.4.0/doc/screenshot.png)
<h2>Example:</h2>
<img src="https://raw.githubusercontent.com/markiewb/nb-additional-hints/v1.6.1/doc/screenshot.png"/>

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
<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>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/56">New Fix</a>]: Convert return xxx to return Optional.ofNullable(xxx)/Optional.of(xxx)/Optional.empty()</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/63">New Fix</a>]: Convert to assertNull</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/68">New Fix</a>]: Replace with org.junit.Assert</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/54">Updated Fix</a>]: "Replace +..." works for more expressions</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/57">Updated Fix</a>]: Fixed false positive result from "Convert to Optional.isEmpty()"</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/58">Updated Fix</a>]: Make "Change modifiers" hints non-intrusive</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/59">Updated Fix</a>]: Make "Change modifiers" hints work for constructors too</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/64">Updated Fix </a>]: java.lang.NullPointerException in "Report methods that have class name"</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/75">Updated Fix </a>]: java.lang.NullPointerException in ReturnForOptional</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/70">Removed Fix</a>]: "dead instanceof" hint has been removed</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/72">Task</a>]: Update dependencies to NB 8.1</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/76">Task</a>]: Reduce dependencies</li>
</ul>
<h3>1.5.0:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/42">Updated Fix</a>]: "Convert to if/else" now supports assignments to new variables</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/43">Updated Fix</a>]: "Invert ternary"/"Convert to ternary" now support conditions without brackets</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/3">Updated Fix</a>]: "Replace +..." is not proposed for erroneous conditions anymore</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/51">Updated Fix</a>]: "Replace +..." works for more expressions</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/50">Updated Fix</a>]: Remove false positive detected by "Detect dead instanceof"</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/56">New Fix</a>]: Replace with Optional.isPresent()/Convert return null to return Optional.empty()</li>
<li>[Task]: Update requirements JDK7 and NB7.4</li>
</ul>
<h3>1.4.0:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/44">New Hint</a>]: Detect dead instanceof-expressions</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/2">Updated Fix</a>]: "Replace +..." hints can now supports chars too</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/47">Meta</a>]: Add donation link (for those who want to support this project)</li>
</ul>
<h3>1.3.0:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/36">Updated Fix</a>]: "Invert ternary if/else" now supports more patterns</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/39">New Hint</a>]: Add "this." to methods and variables (disabled by default)</li>
</ul>

License remains CDDL-GPL-2-CP - http://www.netbeans.org/cddl-gplv2.html
<h3>1.2.0:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/pull/22">New Fix</a>]: Change the modifier of a class/method/field to public/package protected/protected/private (by <a href="https://github.com/rasa-silva">rasa-silva</a>)</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/24">New Fix</a>]: Remove public/abstract/final modifiers from field declarations within interfaces</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/9">New Fix</a>]: Convert from char and string and back</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/10">New Fix</a>]: Convert number in literal to number and back</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/12">New Fix</a>]: Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/31">New Fix</a>]: Convert from if/else to ternary and back</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/32">New Fix</a>]: Invert ternary if/else</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/34">New Hint</a>]: Report methods that have class name</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/29">Updated Fix</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 Fix</a>]: "Replace +..." hints can now be configured</li>
</ul>

Compatible to NetBeans 7.4 and higher
<h3>1.1.0:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/11">New Fix</a>]: Support transformation to BigDecimal constants</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/5">New Fix</a>]: Split a string at caret</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/13">New Fix</a>]: Convert to assertTrue/assertFalse</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/14">New Fix</a>]: Remove "public abstract" modifiers from method declarations within interfaces</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/8">Bugfix</a>]: Literals with quoted Strings won't be copied properly to clipboard</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/7">Refactoring</a>]: Convert to maven based module</li>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/18">Refactoring</a>]: Hints should be proposals instead of errors/warnings</li>
</ul>
<h3>1.0.x:</h3>
<ul>
<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/1">Bugfix</a>]: Literals with quoted Strings create uncompileable code</li>
</ul>
<h2>
<a name="further-information" class="anchor" href="#further-information"><span class="mini-icon mini-icon-link"></span></a>Further information:</h2>

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

<p>License remains CDDL-GPL-2-CP - <a href="http://www.netbeans.org/cddl-gplv2.html">http://www.netbeans.org/cddl-gplv2.html</a></p>

<p>
Provide defects, request for enhancements and feedback at <a href=https://github.com/markiewb/nb-additional-hints/issues">https://github.com/markiewb/nb-additional-hints/issues</a>
</p>
<p>Compatible to NetBeans 8.1+</p>
<p>
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=K4CMP92RZELE2"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="btn_donate_SM.gif"></a>

</p>
Binary file modified doc/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions 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.6.0.10</version>
<version>1.6.1.0</version>
<packaging>nbm</packaging>

<name>Additional Java hints</name>
Expand Down Expand Up @@ -220,9 +220,13 @@
&lt;/ul&gt;

&lt;h2&gt;Example:&lt;/h2&gt;
&lt;img src="https://raw.githubusercontent.com/markiewb/nb-additional-hints/v1.4.0/doc/screenshot.png"/&gt;
&lt;img src="https://raw.githubusercontent.com/markiewb/nb-additional-hints/v1.6.1/doc/screenshot.png"/&gt;

&lt;h2&gt;Updates&lt;/h2&gt;
&lt;h3&gt;1.6.1:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/78"&gt;Bugfix&lt;/a&gt;]: Fixed: Hints for converting ternary to if and back are missing&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;1.6.0:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/55"&gt;New Fix&lt;/a&gt;]: Replace with null-assignment to Optional with Optional.empty()&lt;/li&gt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 [email protected]
*/
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. <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",
"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",
"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;}")
@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 = "$var=($cond)?$a:$b;"),
@TriggerPattern(value = "$var=$cond?$a:$b;"),
@TriggerPattern(value = "$type $var=($cond)?$a:$b;"),
@TriggerPattern(value = "$type $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")
public static ErrorDescription toIfElseAssign(HintContext ctx) {
TreePath type = ctx.getVariables().get("$type");

Fix fix;
if (type != null) {
fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_ToIfElseAssign(), ctx.getPath(), "$type $var; if ($cond) {$var = $a;} else {$var = $b;}");
} else {
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);
}
}
Loading

0 comments on commit 0ecbe63

Please sign in to comment.