Adding a hint that allows to ignore == operator while performing the Convert to switch over Strings operation without the need to manually alter the preferences.
authorJan Lahoda <jlahoda@netbeans.org>
Tue, 14 Dec 2010 19:42:46 +0100
changeset 4966c672293292f
parent 495 e9ebd0e5eef4
child 497 b3e84fcc8602
Adding a hint that allows to ignore == operator while performing the Convert to switch over Strings operation without the need to manually alter the preferences.
hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/Bundle.properties
hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/SafeConvertToSwitchString.java
     1.1 --- a/hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/Bundle.properties	Mon Dec 13 22:51:12 2010 +0100
     1.2 +++ b/hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/Bundle.properties	Tue Dec 14 19:42:46 2010 +0100
     1.3 @@ -3,3 +3,5 @@
     1.4  DN_jdk7_upgrade=To JDK7
     1.5  DN_java.util.Objects=Convert to use java.util.Objects.{equals,hashCode}
     1.6  DESC_java.util.Objects=Convert to use java.util.Objects.{equals,hashCode}
     1.7 +DN_org.netbeans.modules.jackpot30.hintsimpl.jdk7.SafeConvertToSwitchString=Convert to switch over Strings, ignoring the == operator
     1.8 +DESC_org.netbeans.modules.jackpot30.hintsimpl.jdk7.SafeConvertToSwitchString=Convert to switch over Strings, ignoring the == operator
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/SafeConvertToSwitchString.java	Tue Dec 14 19:42:46 2010 +0100
     2.3 @@ -0,0 +1,80 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2010 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 2010 Sun Microsystems, Inc.
    2.44 + */
    2.45 +
    2.46 +package org.netbeans.modules.jackpot30.hintsimpl.jdk7;
    2.47 +
    2.48 +import java.util.List;
    2.49 +import java.util.prefs.Preferences;
    2.50 +import org.netbeans.modules.java.hints.jackpot.code.spi.Hint;
    2.51 +import org.netbeans.modules.java.hints.jackpot.code.spi.TriggerPattern;
    2.52 +import org.netbeans.modules.java.hints.jackpot.impl.RulesManager;
    2.53 +import org.netbeans.modules.java.hints.jackpot.spi.HintContext;
    2.54 +import org.netbeans.modules.java.hints.jackpot.spi.HintMetadata.Kind;
    2.55 +import org.netbeans.modules.java.hints.jdk.ConvertToStringSwitch;
    2.56 +import org.netbeans.modules.java.hints.options.HintsSettings;
    2.57 +import org.netbeans.spi.editor.hints.ErrorDescription;
    2.58 +
    2.59 +/**
    2.60 + *
    2.61 + * @author lahvac
    2.62 + */
    2.63 +@Hint(category="rules15",enabled=false, hintKind=Kind.HINT_NON_GUI)
    2.64 +public class SafeConvertToSwitchString {
    2.65 +
    2.66 +    static final String KEY_ALSO_EQ = "also-equals";
    2.67 +    static final boolean DEF_ALSO_EQ = true;
    2.68 +
    2.69 +    @TriggerPattern(value="if ($cond) $body; else $else;")
    2.70 +    public static List<ErrorDescription> hint(HintContext ctx) {
    2.71 +        Preferences prefs = RulesManager.getPreferences("org.netbeans.modules.java.hints.jdk.ConvertToStringSwitch", HintsSettings.getCurrentProfileId());
    2.72 +        boolean old = prefs.getBoolean(KEY_ALSO_EQ, DEF_ALSO_EQ);
    2.73 +
    2.74 +        prefs.putBoolean(KEY_ALSO_EQ, false);
    2.75 +
    2.76 +        try {
    2.77 +            return ConvertToStringSwitch.hint(ctx);
    2.78 +        } finally {
    2.79 +            prefs.putBoolean(KEY_ALSO_EQ, old);
    2.80 +        }
    2.81 +    }
    2.82 +
    2.83 +}