Making the Convert to diamond hint configurable
authorJan Lahoda <jlahoda@netbeans.org>
Thu, 06 Jan 2011 19:27:11 +0100
changeset 5076cbc51eb46bb
parent 506 ec6bf12c6eee
child 508 5a4eaec07a00
Making the Convert to diamond hint configurable
borrowedtests/src/org/netbeans/modules/java/hints/jackpot/code/spi/TestBase.java
cmdline/tool/test/scripted/test-config-diamond
hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/ConvertToDiamondBulkHint.java
hintsimpl/test/unit/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/ConvertToDiamondBulkHintTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/borrowedtests/src/org/netbeans/modules/java/hints/jackpot/code/spi/TestBase.java	Thu Jan 06 19:27:11 2011 +0100
     1.3 @@ -0,0 +1,333 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2009-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * If you wish your version of this file to be governed by only the CDDL
    1.31 + * or only the GPL Version 2, indicate your decision by adding
    1.32 + * "[Contributor] elects to include this software in this distribution
    1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 + * single choice of license, a recipient has the option to distribute
    1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 + * to extend the choice of license to its licensees as provided above.
    1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 + * Version 2 license, then the option applies only if the new code is
    1.39 + * made subject to such option by the copyright holder.
    1.40 + *
    1.41 + * Contributor(s):
    1.42 + *
    1.43 + * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
    1.44 + */
    1.45 +
    1.46 +package org.netbeans.modules.java.hints.jackpot.code.spi;
    1.47 +
    1.48 +import java.io.File;
    1.49 +import java.lang.reflect.Method;
    1.50 +import java.util.Arrays;
    1.51 +import java.util.Collection;
    1.52 +import java.util.Collections;
    1.53 +import java.util.Comparator;
    1.54 +import java.util.HashMap;
    1.55 +import java.util.HashSet;
    1.56 +import java.util.LinkedList;
    1.57 +import java.util.List;
    1.58 +import java.util.Map;
    1.59 +import java.util.Set;
    1.60 +import java.util.concurrent.atomic.AtomicBoolean;
    1.61 +import java.util.logging.Level;
    1.62 +import java.util.logging.Logger;
    1.63 +import javax.swing.text.Document;
    1.64 +import org.netbeans.api.java.lexer.JavaTokenId;
    1.65 +import org.netbeans.api.java.source.CompilationInfo;
    1.66 +import org.netbeans.api.java.source.JavaSource;
    1.67 +import org.netbeans.api.java.source.JavaSource.Phase;
    1.68 +import org.netbeans.api.java.source.SourceUtilsTestUtil;
    1.69 +import org.netbeans.api.java.source.TestUtilities;
    1.70 +import org.netbeans.api.java.source.support.CaretAwareJavaSourceTaskFactory;
    1.71 +import org.netbeans.api.lexer.Language;
    1.72 +import org.netbeans.junit.NbTestCase;
    1.73 +import org.netbeans.modules.java.hints.jackpot.code.CodeHintProviderImpl;
    1.74 +import org.netbeans.modules.java.hints.jackpot.code.FSWrapper;
    1.75 +import org.netbeans.modules.java.hints.jackpot.code.FSWrapper.ClassWrapper;
    1.76 +import org.netbeans.modules.java.hints.jackpot.spi.HintDescription;
    1.77 +import org.netbeans.modules.java.hints.jackpot.spi.HintMetadata;
    1.78 +import org.netbeans.modules.java.hints.jackpot.spi.HintsRunner;
    1.79 +import org.netbeans.modules.java.source.TreeLoader;
    1.80 +import org.netbeans.spi.editor.hints.ErrorDescription;
    1.81 +import org.netbeans.spi.editor.hints.Fix;
    1.82 +import org.openide.LifecycleManager;
    1.83 +import org.openide.cookies.EditorCookie;
    1.84 +import org.openide.filesystems.FileObject;
    1.85 +import org.openide.filesystems.FileUtil;
    1.86 +import org.openide.loaders.DataObject;
    1.87 +import org.openide.util.NbBundle;
    1.88 +
    1.89 +/**
    1.90 + *
    1.91 + * @author lahvac
    1.92 + */
    1.93 +public abstract class TestBase extends NbTestCase {
    1.94 +    private final Class<?> hintClass;
    1.95 +    protected final Logger LOG;
    1.96 +
    1.97 +    public TestBase(String name, Class<?> hintClass) {
    1.98 +        super(name);
    1.99 +        this.hintClass = hintClass;
   1.100 +        LOG = Logger.getLogger("test." + name);
   1.101 +    }
   1.102 +
   1.103 +    @Override
   1.104 +    protected Level logLevel() {
   1.105 +        return Level.INFO;
   1.106 +    }
   1.107 +
   1.108 +    @Override
   1.109 +    protected void setUp() throws Exception {
   1.110 +        super.setUp();
   1.111 +        SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml", "META-INF/generated-layer.xml"}, new Object[0]);
   1.112 +        TreeLoader.DISABLE_CONFINEMENT_TEST = true;
   1.113 +    }
   1.114 +
   1.115 +    private String sourceLevel = "1.5";
   1.116 +
   1.117 +    protected void setSourceLevel(String sourceLevel) {
   1.118 +        this.sourceLevel = sourceLevel;
   1.119 +    }
   1.120 +    
   1.121 +    protected void prepareTest(String fileName, String code) throws Exception {
   1.122 +        clearWorkDir();
   1.123 +        File wdFile = getWorkDir();
   1.124 +        FileUtil.refreshFor(wdFile);
   1.125 +
   1.126 +        FileObject wd = FileUtil.toFileObject(wdFile);
   1.127 +        assertNotNull(wd);
   1.128 +        sourceRoot = FileUtil.createFolder(wd, "src");
   1.129 +        FileObject buildRoot = FileUtil.createFolder(wd, "build");
   1.130 +        FileObject cache = FileUtil.createFolder(wd, "cache");
   1.131 +
   1.132 +        FileObject data = FileUtil.createData(sourceRoot, fileName);
   1.133 +        File dataFile = FileUtil.toFile(data);
   1.134 +
   1.135 +        assertNotNull(dataFile);
   1.136 +
   1.137 +        TestUtilities.copyStringToFile(dataFile, code);
   1.138 +
   1.139 +        SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache, extraClassPath());
   1.140 +
   1.141 +        SourceUtilsTestUtil.setSourceLevel(data, sourceLevel);
   1.142 +
   1.143 +        DataObject od = DataObject.find(data);
   1.144 +        EditorCookie ec = od.getCookie(EditorCookie.class);
   1.145 +
   1.146 +        assertNotNull(ec);
   1.147 +
   1.148 +        doc = ec.openDocument();
   1.149 +        doc.putProperty(Language.class, JavaTokenId.language());
   1.150 +        doc.putProperty("mimeType", "text/x-java");
   1.151 +
   1.152 +        JavaSource js = JavaSource.forFileObject(data);
   1.153 +
   1.154 +        assertNotNull(js);
   1.155 +
   1.156 +        info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
   1.157 +
   1.158 +        assertNotNull(info);
   1.159 +    }
   1.160 +
   1.161 +    private FileObject sourceRoot;
   1.162 +    private CompilationInfo info;
   1.163 +    private Document doc;
   1.164 +
   1.165 +    protected final void setTestFileCaretLocation(int pos) throws Exception {
   1.166 +        Method m = CaretAwareJavaSourceTaskFactory.class.getDeclaredMethod("setLastPosition", FileObject.class, int.class);
   1.167 +
   1.168 +        m.setAccessible(true);
   1.169 +        m.invoke(null, info.getFileObject(), pos);
   1.170 +    }
   1.171 +
   1.172 +    private List<ErrorDescription> computeErrors(CompilationInfo info) {
   1.173 +        Map<HintMetadata, Collection<HintDescription>> hints = new HashMap<HintMetadata, Collection<HintDescription>>();
   1.174 +
   1.175 +        ClassWrapper found = null;
   1.176 +
   1.177 +        for (ClassWrapper w : FSWrapper.listClasses()) {
   1.178 +            if (w.getName().equals(hintClass.getName())) {
   1.179 +                found = w;
   1.180 +                break;
   1.181 +            }
   1.182 +        }
   1.183 +
   1.184 +        assertNotNull(found);
   1.185 +        
   1.186 +        CodeHintProviderImpl.processClass(found, hints);
   1.187 +
   1.188 +        List<HintDescription> total = new LinkedList<HintDescription>();
   1.189 +
   1.190 +        for (Collection<? extends HintDescription> l : hints.values()) {
   1.191 +            total.addAll(l);
   1.192 +        }
   1.193 +
   1.194 +        return HintsRunner.computeErrors(info, total, new AtomicBoolean());
   1.195 +    }
   1.196 +
   1.197 +    protected String toDebugString(CompilationInfo info, Fix f) {
   1.198 +        return "FixImpl";
   1.199 +    }
   1.200 +
   1.201 +    protected void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
   1.202 +        prepareTest(fileName, code);
   1.203 +
   1.204 +        List<ErrorDescription> errors = computeErrors(info);
   1.205 +        Collections.sort (errors, ERRORS_COMPARATOR);
   1.206 +        List<String> errorsNames = new LinkedList<String>();
   1.207 +
   1.208 +        errors = errors != null ? errors : Collections.<ErrorDescription>emptyList();
   1.209 +
   1.210 +        for (ErrorDescription e : errors) {
   1.211 +            errorsNames.add(e.toString());
   1.212 +        }
   1.213 +
   1.214 +        assertTrue("The warnings provided by the hint do not match expected warnings. Provided warnings: " + errorsNames.toString(), Arrays.equals(golden, errorsNames.toArray(new String[0])));
   1.215 +    }
   1.216 +
   1.217 +    protected void performAnalysisContainsTest(String fileName, String code, String... golden) throws Exception {
   1.218 +        prepareTest(fileName, code);
   1.219 +
   1.220 +        Set<String> goldenSet = new HashSet<String>();
   1.221 +        for (String s : golden) {
   1.222 +            goldenSet.add(s);
   1.223 +        }
   1.224 +
   1.225 +        List<ErrorDescription> errors = computeErrors(info);
   1.226 +        Collections.sort (errors, ERRORS_COMPARATOR);
   1.227 +        List<String> errorsNames = new LinkedList<String>();
   1.228 +
   1.229 +        errors = errors != null ? errors : Collections.<ErrorDescription>emptyList();
   1.230 +
   1.231 +        for (ErrorDescription d : errors) {
   1.232 +            goldenSet.remove(d.toString());
   1.233 +            errorsNames.add(d.toString());
   1.234 +        }
   1.235 +        assertTrue("The warnings provided by the hint do not contain expected warnings. Provided warnings: " + errorsNames.toString(), goldenSet.isEmpty());
   1.236 +    }
   1.237 +
   1.238 +    protected void performAnalysisExcludesTest(String fileName, String code, String... golden) throws Exception {
   1.239 +        prepareTest(fileName, code);
   1.240 +
   1.241 +        Set<String> goldenSet = new HashSet<String>();
   1.242 +        for (String s : golden) {
   1.243 +            goldenSet.add(s);
   1.244 +        }
   1.245 +
   1.246 +        List<ErrorDescription> errors = computeErrors(info);
   1.247 +        Collections.sort (errors, ERRORS_COMPARATOR);
   1.248 +        List<String> errorsNames = new LinkedList<String>();
   1.249 +
   1.250 +        errors = errors != null ? errors : Collections.<ErrorDescription>emptyList();
   1.251 +
   1.252 +        boolean fail = false;
   1.253 +        for (ErrorDescription d : errors) {
   1.254 +            if (goldenSet.remove(d.getDescription()))
   1.255 +                fail = true;
   1.256 +            errorsNames.add(d.toString());
   1.257 +        }
   1.258 +        assertFalse("The warnings provided by the hint do not exclude expected warnings. Provided warnings: " + errorsNames.toString(), fail);
   1.259 +    }
   1.260 +
   1.261 +    protected String performFixTest(String fileName, String code, String errorDescriptionToString, String fixDebugString, String golden) throws Exception {
   1.262 +        return performFixTest(fileName, code, errorDescriptionToString, fixDebugString, fileName, golden);
   1.263 +    }
   1.264 +
   1.265 +    protected String performFixTest(String fileName, String code, String errorDescriptionToString, String fixDebugString, String goldenFileName, String golden) throws Exception {
   1.266 +        prepareTest(fileName, code);
   1.267 +
   1.268 +        List<ErrorDescription> errors = computeErrors(info);
   1.269 +        Collections.sort (errors, ERRORS_COMPARATOR);
   1.270 +
   1.271 +        ErrorDescription toFix = null;
   1.272 +
   1.273 +        for (ErrorDescription d : errors) {
   1.274 +            if (errorDescriptionToString.equals(d.toString())) {
   1.275 +                toFix = d;
   1.276 +                break;
   1.277 +            }
   1.278 +        }
   1.279 +
   1.280 +        assertNotNull("Error: \"" + errorDescriptionToString + "\" not found. All ErrorDescriptions: " + errors.toString(), toFix);
   1.281 +
   1.282 +        assertTrue("Must be computed", toFix.getFixes().isComputed());
   1.283 +
   1.284 +        List<Fix> fixes = toFix.getFixes().getFixes();
   1.285 +        List<String> fixNames = new LinkedList<String>();
   1.286 +        Fix toApply = null;
   1.287 +
   1.288 +        for (Fix f : fixes) {
   1.289 +            if (fixDebugString.equals(toDebugString(info, f))) {
   1.290 +                toApply = f;
   1.291 +            }
   1.292 +
   1.293 +            fixNames.add(toDebugString(info, f));
   1.294 +        }
   1.295 +
   1.296 +        assertNotNull("Cannot find fix to invoke: " + fixNames.toString(), toApply);
   1.297 +
   1.298 +        toApply.implement();
   1.299 +
   1.300 +        FileObject toCheck = sourceRoot.getFileObject(goldenFileName);
   1.301 +
   1.302 +        assertNotNull(toCheck);
   1.303 +
   1.304 +        DataObject toCheckDO = DataObject.find(toCheck);
   1.305 +        EditorCookie ec = toCheckDO.getLookup().lookup(EditorCookie.class);
   1.306 +        Document toCheckDocument = ec.openDocument();
   1.307 +
   1.308 +        String realCode = toCheckDocument.getText(0, toCheckDocument.getLength());
   1.309 +
   1.310 +        //ignore whitespaces:
   1.311 +        realCode = realCode.replaceAll("[ \t\n]+", " ");
   1.312 +
   1.313 +        if (golden != null) {
   1.314 +            assertEquals("The output code does not match the expected code.", golden, realCode);
   1.315 +        }
   1.316 +
   1.317 +        LifecycleManager.getDefault().saveAll();
   1.318 +
   1.319 +        return realCode;
   1.320 +    }
   1.321 +
   1.322 +    protected FileObject[] extraClassPath() {
   1.323 +        return new FileObject[0];
   1.324 +    }
   1.325 +
   1.326 +    static {
   1.327 +        NbBundle.setBranding("test");
   1.328 +    }
   1.329 +
   1.330 +    private static final Comparator<ErrorDescription> ERRORS_COMPARATOR = new Comparator<ErrorDescription> () {
   1.331 +
   1.332 +        public int compare (ErrorDescription e1, ErrorDescription e2) {
   1.333 +            return e1.getRange ().getBegin ().getOffset () - e2.getRange ().getBegin ().getOffset ();
   1.334 +        }
   1.335 +    };
   1.336 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/cmdline/tool/test/scripted/test-config-diamond	Thu Jan 06 19:27:11 2011 +0100
     2.3 @@ -0,0 +1,79 @@
     2.4 +#!/bin/bash
     2.5 +#
     2.6 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.7 +#
     2.8 +# Copyright 2010 Sun Microsystems, Inc. All rights reserved.
     2.9 +#
    2.10 +# The contents of this file are subject to the terms of either the GNU
    2.11 +# General Public License Version 2 only ("GPL") or the Common
    2.12 +# Development and Distribution License("CDDL") (collectively, the
    2.13 +# "License"). You may not use this file except in compliance with the
    2.14 +# License. You can obtain a copy of the License at
    2.15 +# http://www.netbeans.org/cddl-gplv2.html
    2.16 +# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.17 +# specific language governing permissions and limitations under the
    2.18 +# License.  When distributing the software, include this License Header
    2.19 +# Notice in each file and include the License file at
    2.20 +# nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.21 +# particular file as subject to the "Classpath" exception as provided
    2.22 +# by Sun in the GPL Version 2 section of the License file that
    2.23 +# accompanied this code. If applicable, add the following below the
    2.24 +# License Header, with the fields enclosed by brackets [] replaced by
    2.25 +# your own identifying information:
    2.26 +# "Portions Copyrighted [year] [name of copyright owner]"
    2.27 +#
    2.28 +# If you wish your version of this file to be governed by only the CDDL
    2.29 +# or only the GPL Version 2, indicate your decision by adding
    2.30 +# "[Contributor] elects to include this software in this distribution
    2.31 +# under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.32 +# single choice of license, a recipient has the option to distribute
    2.33 +# your version of this file under either the CDDL, the GPL Version 2 or
    2.34 +# to extend the choice of license to its licensees as provided above.
    2.35 +# However, if you add GPL Version 2 code and therefore, elected the GPL
    2.36 +# Version 2 license, then the option applies only if the new code is
    2.37 +# made subject to such option by the copyright holder.
    2.38 +#
    2.39 +# Contributor(s):
    2.40 +#
    2.41 +# Portions Copyrighted 2010 Sun Microsystems, Inc.
    2.42 +
    2.43 +perform_test() {
    2.44 +    create_file src/test/Test.java <<EOF
    2.45 +package test;
    2.46 +import java.util.LinkedList;
    2.47 +import java.util.List;
    2.48 +public class Test {
    2.49 +    private void test() {
    2.50 +        List<String> l = new LinkedList<String>();
    2.51 +    }
    2.52 +}
    2.53 +EOF
    2.54 +
    2.55 +    run_tool --hint "Convert to diamond bulk hint" --config enabled="" src
    2.56 +
    2.57 +    assert_file_content  src/test/Test.java <<EOF
    2.58 +package test;
    2.59 +import java.util.LinkedList;
    2.60 +import java.util.List;
    2.61 +public class Test {
    2.62 +    private void test() {
    2.63 +        List<String> l = new LinkedList<String>();
    2.64 +    }
    2.65 +}
    2.66 +EOF
    2.67 +
    2.68 +    run_tool --hint "Convert to diamond bulk hint" --config enabled="initializer" src
    2.69 +
    2.70 +    assert_file_content  src/test/Test.java <<EOF
    2.71 +package test;
    2.72 +import java.util.LinkedList;
    2.73 +import java.util.List;
    2.74 +public class Test {
    2.75 +    private void test() {
    2.76 +        List<String> l = new LinkedList<>();
    2.77 +    }
    2.78 +}
    2.79 +EOF
    2.80 +}
    2.81 +
    2.82 +. `dirname $0`/harness
     3.1 --- a/hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/ConvertToDiamondBulkHint.java	Thu Jan 06 19:27:10 2011 +0100
     3.2 +++ b/hintsimpl/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/ConvertToDiamondBulkHint.java	Thu Jan 06 19:27:11 2011 +0100
     3.3 @@ -41,11 +41,18 @@
     3.4  
     3.5  import com.sun.source.util.TreePath;
     3.6  import java.util.Arrays;
     3.7 +import java.util.Collection;
     3.8 +import java.util.HashMap;
     3.9  import java.util.HashSet;
    3.10 +import java.util.LinkedHashMap;
    3.11  import java.util.LinkedList;
    3.12  import java.util.List;
    3.13 +import java.util.Map;
    3.14 +import java.util.Map.Entry;
    3.15  import java.util.Set;
    3.16 +import java.util.prefs.Preferences;
    3.17  import javax.tools.Diagnostic;
    3.18 +import org.netbeans.modules.jackpot30.spi.MatcherUtilities;
    3.19  import org.netbeans.modules.java.hints.errors.ConvertToDiamond;
    3.20  import org.netbeans.modules.java.hints.jackpot.code.spi.Hint;
    3.21  import org.netbeans.modules.java.hints.jackpot.code.spi.TriggerPattern;
    3.22 @@ -65,6 +72,16 @@
    3.23  @Hint(category="rules15",enabled=false, hintKind=Kind.HINT_NON_GUI)
    3.24  public class ConvertToDiamondBulkHint {
    3.25  
    3.26 +    private static final Map<String, Collection<String>> key2Pattern = new LinkedHashMap<String, Collection<String>>();
    3.27 +
    3.28 +    static {
    3.29 +        key2Pattern.put("initializer", Arrays.asList("$mods$ $type $name = $_;"));
    3.30 +        key2Pattern.put("assignment", Arrays.asList("$var = $_"));
    3.31 +        key2Pattern.put("return", Arrays.asList("return $_;"));
    3.32 +        key2Pattern.put("argument", Arrays.asList("$site.<$T$>$name($p$, $_, $s$)"));
    3.33 +        key2Pattern.put("other", Arrays.asList(new String[] {null}));
    3.34 +    }
    3.35 +    
    3.36      @TriggerPatterns({
    3.37          @TriggerPattern("new $clazz<$tparams$>($params$)")
    3.38      })
    3.39 @@ -74,11 +91,26 @@
    3.40          Set<String> codes = convert.getCodes();
    3.41          TreePath clazz = ctx.getVariables().get("$clazz");
    3.42          long start = ctx.getInfo().getTrees().getSourcePositions().getStartPosition(clazz.getCompilationUnit(), clazz.getLeaf());
    3.43 +        org.netbeans.modules.jackpot30.spi.HintContext newCTX = new org.netbeans.modules.jackpot30.spi.HintContext(ctx.getInfo(), null, ctx.getPath(), ctx.getVariables(), ctx.getMultiVariables(), ctx.getVariableNames());
    3.44  
    3.45 -        for (Diagnostic<?> d : ctx.getInfo().getDiagnostics()) {
    3.46 +        OUTER: for (Diagnostic<?> d : ctx.getInfo().getDiagnostics()) {
    3.47              if (start != d.getStartPosition()) continue;
    3.48              if (!codes.contains(d.getCode())) continue;
    3.49  
    3.50 +            FOUND: for (Entry<String, Collection<String>> e : key2Pattern.entrySet()) {
    3.51 +                for (String p : e.getValue()) {
    3.52 +                    if (p == null || MatcherUtilities.matches(newCTX, ctx.getPath().getParentPath(), p)) {
    3.53 +                        boolean enabled = isEnabled(ctx, e.getKey());
    3.54 +
    3.55 +                        if (!enabled) {
    3.56 +                            continue OUTER;
    3.57 +                        } else {
    3.58 +                            break FOUND;
    3.59 +                        }
    3.60 +                    }
    3.61 +                }
    3.62 +            }
    3.63 +
    3.64              List<Fix> fixes = convert.run(ctx.getInfo(), d.getCode(), (int) d.getPosition(), ctx.getInfo().getTreeUtilities().pathFor((int) d.getPosition() + 1), null);
    3.65              result.add(ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "", fixes, ctx.getInfo().getFileObject(), (int) d.getStartPosition(), (int) d.getEndPosition()));
    3.66          }
    3.67 @@ -86,4 +118,14 @@
    3.68          return result;
    3.69      }
    3.70  
    3.71 +    static final String ALL = "initializer,assignment,return,argument,other";
    3.72 +    
    3.73 +    static String getConfiguration(Preferences p) {
    3.74 +        return p.get("enabled", ALL);
    3.75 +    }
    3.76 +
    3.77 +    private static boolean isEnabled(HintContext ctx, String key) {
    3.78 +        return ("," + getConfiguration(ctx.getPreferences()) + ",").contains("," + key + ",");
    3.79 +    }
    3.80 +
    3.81  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/hintsimpl/test/unit/src/org/netbeans/modules/jackpot30/hintsimpl/jdk7/ConvertToDiamondBulkHintTest.java	Thu Jan 06 19:27:11 2011 +0100
     4.3 @@ -0,0 +1,203 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
     4.8 + *
     4.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.10 + * Other names may be trademarks of their respective owners.
    4.11 + *
    4.12 + * The contents of this file are subject to the terms of either the GNU
    4.13 + * General Public License Version 2 only ("GPL") or the Common
    4.14 + * Development and Distribution License("CDDL") (collectively, the
    4.15 + * "License"). You may not use this file except in compliance with the
    4.16 + * License. You can obtain a copy of the License at
    4.17 + * http://www.netbeans.org/cddl-gplv2.html
    4.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.19 + * specific language governing permissions and limitations under the
    4.20 + * License.  When distributing the software, include this License Header
    4.21 + * Notice in each file and include the License file at
    4.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.23 + * particular file as subject to the "Classpath" exception as provided
    4.24 + * by Oracle in the GPL Version 2 section of the License file that
    4.25 + * accompanied this code. If applicable, add the following below the
    4.26 + * License Header, with the fields enclosed by brackets [] replaced by
    4.27 + * your own identifying information:
    4.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.29 + *
    4.30 + * If you wish your version of this file to be governed by only the CDDL
    4.31 + * or only the GPL Version 2, indicate your decision by adding
    4.32 + * "[Contributor] elects to include this software in this distribution
    4.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.34 + * single choice of license, a recipient has the option to distribute
    4.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.36 + * to extend the choice of license to its licensees as provided above.
    4.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.38 + * Version 2 license, then the option applies only if the new code is
    4.39 + * made subject to such option by the copyright holder.
    4.40 + *
    4.41 + * Contributor(s):
    4.42 + *
    4.43 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
    4.44 + */
    4.45 +
    4.46 +package org.netbeans.modules.jackpot30.hintsimpl.jdk7;
    4.47 +
    4.48 +
    4.49 +import java.util.prefs.Preferences;
    4.50 +import org.netbeans.modules.java.hints.jackpot.code.spi.TestBase;
    4.51 +import org.netbeans.modules.java.hints.jackpot.impl.RulesManager;
    4.52 +import org.netbeans.modules.java.hints.options.HintsSettings;
    4.53 +
    4.54 +/**
    4.55 + *
    4.56 + * @author lahvac
    4.57 + */
    4.58 +public class ConvertToDiamondBulkHintTest extends TestBase {
    4.59 +
    4.60 +    public ConvertToDiamondBulkHintTest(String name) {
    4.61 +        super(name, ConvertToDiamondBulkHint.class);
    4.62 +    }
    4.63 +
    4.64 +    private String oldPrefs;
    4.65 +
    4.66 +    @Override
    4.67 +    protected void setUp() throws Exception {
    4.68 +        super.setUp();
    4.69 +
    4.70 +        Preferences prefs = RulesManager.getPreferences(ConvertToDiamondBulkHint.class.getName(), HintsSettings.getCurrentProfileId());
    4.71 +
    4.72 +        oldPrefs = ConvertToDiamondBulkHint.getConfiguration(prefs);
    4.73 +    }
    4.74 +
    4.75 +    @Override
    4.76 +    protected void tearDown() throws Exception {
    4.77 +        setPrefs(oldPrefs);
    4.78 +        super.tearDown();
    4.79 +    }
    4.80 +
    4.81 +    private void setPrefs(String settings) {
    4.82 +        Preferences prefs = RulesManager.getPreferences(ConvertToDiamondBulkHint.class.getName(), HintsSettings.getCurrentProfileId());
    4.83 +
    4.84 +        prefs.put("enabled", settings);
    4.85 +    }
    4.86 +
    4.87 +    private void allBut(String key) {
    4.88 +        setPrefs(("," + ConvertToDiamondBulkHint.ALL + ",").replace("," + key + ",", ","));
    4.89 +    }
    4.90 +
    4.91 +    public void testSimple() throws Exception {
    4.92 +        setSourceLevel("1.7");
    4.93 +        performFixTest("test/Test.java",
    4.94 +                       "package test;\n" +
    4.95 +                       "public class Test {\n" +
    4.96 +                       "    private java.util.LinkedList<String> l = new java.util.LinkedList<String>();\n" +
    4.97 +                       "}\n",
    4.98 +                       "2:49-2:77:error:",
    4.99 +                       "FixImpl",
   4.100 +                       ("package test;\n" +
   4.101 +                       "public class Test {\n" +
   4.102 +                       "    private java.util.LinkedList<String> l = new java.util.LinkedList<>();\n" +
   4.103 +                       "}\n").replaceAll("[ \t\n]+", " "));
   4.104 +    }
   4.105 +
   4.106 +    public void testConfiguration1() throws Exception {
   4.107 +        setSourceLevel("1.7");
   4.108 +        allBut("initializer");
   4.109 +        performAnalysisTest("test/Test.java",
   4.110 +                            "package test;\n" +
   4.111 +                            "public class Test {\n" +
   4.112 +                            "    private java.util.LinkedList<String> l = new java.util.LinkedList<String>();\n" +
   4.113 +                            "}\n");
   4.114 +    }
   4.115 +
   4.116 +    public void testConfiguration2() throws Exception {
   4.117 +        setSourceLevel("1.7");
   4.118 +        allBut("initializer");
   4.119 +        performAnalysisTest("test/Test.java",
   4.120 +                            "package test;\n" +
   4.121 +                            "public class Test {\n" +
   4.122 +                            "    { java.util.LinkedList<String> l = new java.util.LinkedList<String>(); }\n" +
   4.123 +                            "}\n");
   4.124 +    }
   4.125 +
   4.126 +    public void testConfiguration2a() throws Exception {
   4.127 +        setSourceLevel("1.7");
   4.128 +        performAnalysisTest("test/Test.java",
   4.129 +                            "package test;\n" +
   4.130 +                            "public class Test {\n" +
   4.131 +                            "    { java.util.LinkedList<String> l = new java.util.LinkedList<String>(); }\n" +
   4.132 +                            "}\n",
   4.133 +                            "2:43-2:71:error:");
   4.134 +    }
   4.135 +
   4.136 +    public void testConfiguration2b() throws Exception {
   4.137 +        setSourceLevel("1.7");
   4.138 +        setPrefs("initializer");
   4.139 +        performAnalysisTest("test/Test.java",
   4.140 +                            "package test;\n" +
   4.141 +                            "public class Test {\n" +
   4.142 +                            "    { java.util.LinkedList<String> l = new java.util.LinkedList<String>(); }\n" +
   4.143 +                            "}\n",
   4.144 +                            "2:43-2:71:error:");
   4.145 +    }
   4.146 +
   4.147 +    public void testConfiguration3() throws Exception {
   4.148 +        setSourceLevel("1.7");
   4.149 +        allBut("initializer");
   4.150 +        performAnalysisTest("test/Test.java",
   4.151 +                            "package test;\n" +
   4.152 +                            "public class Test {\n" +
   4.153 +                            "    { final java.util.LinkedList<String> l = new java.util.LinkedList<String>(); }\n" +
   4.154 +                            "}\n");
   4.155 +    }
   4.156 +
   4.157 +    public void testConfiguration4() throws Exception {
   4.158 +        setSourceLevel("1.7");
   4.159 +        allBut("initializer");
   4.160 +        performAnalysisTest("test/Test.java",
   4.161 +                            "package test;\n" +
   4.162 +                            "public class Test {\n" +
   4.163 +                            "    private java.util.LinkedList<String> l() { return new java.util.LinkedList<String>(); }\n" +
   4.164 +                            "}\n",
   4.165 +                            "2:58-2:86:error:");
   4.166 +    }
   4.167 +
   4.168 +    public void testConfiguration5() throws Exception {
   4.169 +        setSourceLevel("1.7");
   4.170 +        allBut("return");
   4.171 +        performAnalysisTest("test/Test.java",
   4.172 +                            "package test;\n" +
   4.173 +                            "public class Test {\n" +
   4.174 +                            "    private java.util.LinkedList<String> l() { return new java.util.LinkedList<String>(); }\n" +
   4.175 +                            "}\n");
   4.176 +    }
   4.177 +
   4.178 +    public void testConfiguration6() throws Exception {
   4.179 +        setSourceLevel("1.7");
   4.180 +        allBut("argument");
   4.181 +        performAnalysisTest("test/Test.java",
   4.182 +                            "package test;\n" +
   4.183 +                            "public class Test {\n" +
   4.184 +                            "    private l(java.util.LinkedList<String> a) { l(new java.util.LinkedList<String>()); }\n" +
   4.185 +                            "}\n");
   4.186 +    }
   4.187 +
   4.188 +    public void testConfiguration7() throws Exception {
   4.189 +        setSourceLevel("1.7");
   4.190 +        performAnalysisTest("test/Test.java",
   4.191 +                            "package test;\n" +
   4.192 +                            "public class Test {\n" +
   4.193 +                            "    private l(java.util.LinkedList<String> a) { l(new java.util.LinkedList<String>()); }\n" +
   4.194 +                            "}\n");
   4.195 +    }
   4.196 +
   4.197 +    public void testConfiguration8() throws Exception {
   4.198 +        setSourceLevel("1.7");
   4.199 +        allBut("assignment");
   4.200 +        performAnalysisTest("test/Test.java",
   4.201 +                            "package test;\n" +
   4.202 +                            "public class Test {\n" +
   4.203 +                            "    { java.util.LinkedList<String> l; l = new java.util.LinkedList<String>(); }\n" +
   4.204 +                            "}\n");
   4.205 +    }
   4.206 +}
   4.207 \ No newline at end of file