Suggestion to exchange the if's then and else section and invert its condition.
authorJan Lahoda <jlahoda@netbeans.org>
Wed, 25 Apr 2012 21:55:05 +0200
changeset 17822e4f9aa73b5b5
parent 17820 76be0d576837
child 17824 9a7b2044ac95
Suggestion to exchange the if's then and else section and invert its condition.
javahints/nbproject/project.properties
javahints/src/org/netbeans/modules/javahints/InvertIf.java
javahints/test/unit/src/org/netbeans/modules/javahints/InvertIfTest.java
     1.1 --- a/javahints/nbproject/project.properties	Thu Apr 19 22:21:36 2012 +0200
     1.2 +++ b/javahints/nbproject/project.properties	Wed Apr 25 21:55:05 2012 +0200
     1.3 @@ -50,7 +50,7 @@
     1.4  auxiliary.org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width=80
     1.5  javac.compilerargs=-Xlint:unchecked
     1.6  javac.source=1.6
     1.7 -spec.version.base=2.55.0
     1.8 +spec.version.base=2.56.0
     1.9  
    1.10  nbm.needs.restart=true
    1.11  requires.nb.javac=true
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/javahints/src/org/netbeans/modules/javahints/InvertIf.java	Wed Apr 25 21:55:05 2012 +0200
     2.3 @@ -0,0 +1,70 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2012 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.modules.javahints;
    2.46 +
    2.47 +import com.sun.source.util.TreePath;
    2.48 +import org.netbeans.spi.editor.hints.ErrorDescription;
    2.49 +import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
    2.50 +import org.netbeans.spi.java.hints.Hint;
    2.51 +import org.netbeans.spi.java.hints.HintContext;
    2.52 +import org.netbeans.spi.java.hints.JavaFixUtilities;
    2.53 +import org.netbeans.spi.java.hints.TriggerPattern;
    2.54 +import org.openide.util.NbBundle.Messages;
    2.55 +
    2.56 +@Hint(displayName = "#DN_InvertIf", description = "#DESC_InvertIf", category = "general", hintKind= Hint.Kind.ACTION)
    2.57 +@Messages({
    2.58 +    "DN_InvertIf=Invert If",
    2.59 +    "DESC_InvertIf=Will invert an if statement; negate the condition and switch the statements from the then and else sections."
    2.60 +})
    2.61 +public class InvertIf {
    2.62 +
    2.63 +    @TriggerPattern(value = "if ($cond) $then; else $else;")
    2.64 +    @Messages({"ERR_InvertIf=Invert If",
    2.65 +               "FIX_InvertIf=Invert If"})
    2.66 +    public static ErrorDescription computeWarning(HintContext ctx) {
    2.67 +        TreePath cond = ctx.getVariables().get("$cond");
    2.68 +        long conditionEnd = ctx.getInfo().getTrees().getSourcePositions().getEndPosition(cond.getCompilationUnit(), cond.getParentPath().getLeaf());
    2.69 +        if (ctx.getCaretLocation() > conditionEnd) return null;
    2.70 +        return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_InvertIf(), JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_InvertIf(), ctx.getPath(), "if (!$cond) $else; else $then;"));
    2.71 +    }
    2.72 +
    2.73 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/javahints/test/unit/src/org/netbeans/modules/javahints/InvertIfTest.java	Wed Apr 25 21:55:05 2012 +0200
     3.3 @@ -0,0 +1,120 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * If you wish your version of this file to be governed by only the CDDL
    3.31 + * or only the GPL Version 2, indicate your decision by adding
    3.32 + * "[Contributor] elects to include this software in this distribution
    3.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.34 + * single choice of license, a recipient has the option to distribute
    3.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.36 + * to extend the choice of license to its licensees as provided above.
    3.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.38 + * Version 2 license, then the option applies only if the new code is
    3.39 + * made subject to such option by the copyright holder.
    3.40 + *
    3.41 + * Contributor(s):
    3.42 + *
    3.43 + * Portions Copyrighted 2012 Sun Microsystems, Inc.
    3.44 + */
    3.45 +package org.netbeans.modules.javahints;
    3.46 +
    3.47 +import org.junit.Test;
    3.48 +import org.netbeans.modules.java.hints.test.api.HintTest;
    3.49 +
    3.50 +public class InvertIfTest {
    3.51 +
    3.52 +    @Test
    3.53 +    public void testComments() throws Exception {
    3.54 +        HintTest.create()
    3.55 +                .setCaretMarker('|')
    3.56 +                .input("package test;\n" +
    3.57 +                       "public class Test {\n" +
    3.58 +                       "    public static void main(String[] args) {\n" +
    3.59 +                       "        i|f\n" +
    3.60 +                       "        // A\n" +
    3.61 +                       "        (args[0].isEmpty())\n" +
    3.62 +                       "        // B\n" +
    3.63 +                       "        { //1\n" +
    3.64 +                       "            //2\n" +
    3.65 +                       "            System.err.println(\"1\");\n" +
    3.66 +                       "            //3\n" +
    3.67 +                       "        } \n" +
    3.68 +                       "        // C\n" +
    3.69 +                       "        else\n" +
    3.70 +                       "        // D\n" +
    3.71 +                       "        {//4\n" +
    3.72 +                       "            //5\n" +
    3.73 +                       "            System.err.println(\"2\");\n" +
    3.74 +                       "            //6\n" +
    3.75 +                       "        } //E\n" +
    3.76 +                       "    }\n" +
    3.77 +                       "}\n")
    3.78 +                .run(InvertIf.class)
    3.79 +                .findWarning("3:9-3:9:verifier:" + Bundle.ERR_InvertIf())
    3.80 +                .applyFix()
    3.81 +                .assertCompilable()
    3.82 +                .assertOutput("package test;\n" +
    3.83 +                              "public class Test {\n" +
    3.84 +                              "    public static void main(String[] args) {\n" +
    3.85 +                              "        if\n" +
    3.86 +                              "        // A\n" +
    3.87 +                              "        (!args[0].isEmpty())\n" +
    3.88 +                              "        // B\n" +
    3.89 +                              "        {//4\n" +
    3.90 +                              "            //5\n" +
    3.91 +                              "            System.err.println(\"2\");\n" +
    3.92 +                              "            //6\n" +
    3.93 +                              "        } \n" +
    3.94 +                              "        // C\n" +
    3.95 +                              "        else\n" +
    3.96 +                              "        // D\n" +
    3.97 +                              "        { //1\n" +
    3.98 +                              "            //2\n" +
    3.99 +                              "            System.err.println(\"1\");\n" +
   3.100 +                              "            //3\n" +
   3.101 +                              "        } //E\n" +
   3.102 +                              "    }\n" +
   3.103 +                              "}\n");
   3.104 +    }
   3.105 +    
   3.106 +    @Test
   3.107 +    public void testCaretPosition() throws Exception {
   3.108 +        HintTest.create()
   3.109 +                .setCaretMarker('|')
   3.110 +                .input("package test;\n" +
   3.111 +                       "public class Test {\n" +
   3.112 +                       "    public static void main(String[] args) {\n" +
   3.113 +                       "        if (args[0].isEmpty()) |{\n" +
   3.114 +                       "            System.err.println(\"1\");\n" +
   3.115 +                       "        } else {\n" +
   3.116 +                       "            System.err.println(\"2\");\n" +
   3.117 +                       "        }\n" +
   3.118 +                       "    }\n" +
   3.119 +                       "}\n")
   3.120 +                .run(InvertIf.class)
   3.121 +                .assertWarnings();
   3.122 +    }
   3.123 +}