Show the declarative hints in the customizer dialog.
authorJan Lahoda <jlahoda@netbeans.org>
Sat, 12 Jan 2013 00:01:25 +0100
changeset 922f8f62f61694b
parent 921 6a16dc0f72ee
child 923 b8b9154040e4
Show the declarative hints in the customizer dialog.
cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateTool.java
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java
     1.1 --- a/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sat Jan 05 09:22:06 2013 +0100
     1.2 +++ b/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sat Jan 12 00:01:25 2013 +0100
     1.3 @@ -79,6 +79,7 @@
     1.4  import joptsimple.OptionParser;
     1.5  import joptsimple.OptionSet;
     1.6  import org.netbeans.api.java.classpath.ClassPath;
     1.7 +import org.netbeans.api.java.classpath.GlobalPathRegistry;
     1.8  import org.netbeans.api.java.source.CompilationController;
     1.9  import org.netbeans.api.java.source.ModificationResult;
    1.10  import org.netbeans.core.startup.MainLookup;
    1.11 @@ -136,7 +137,7 @@
    1.12  
    1.13      public static int compile(String... args) throws IOException, ClassNotFoundException {
    1.14          System.setProperty("netbeans.user", "/tmp/tmp-foo");
    1.15 -        
    1.16 +
    1.17          OptionParser parser = new OptionParser();
    1.18  //        ArgumentAcceptingOptionSpec<File> projects = parser.accepts("project", "project(s) to refactor").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class);
    1.19          ArgumentAcceptingOptionSpec<File> classpath = parser.accepts("classpath", "classpath").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class);
    1.20 @@ -173,6 +174,24 @@
    1.21              return 0;
    1.22          }
    1.23  
    1.24 +        List<FileObject> roots = new ArrayList<FileObject>();
    1.25 +        List<Folder> rootFolders = new ArrayList<Folder>();
    1.26 +
    1.27 +        for (String sr : parsed.nonOptionArguments()) {
    1.28 +            File r = new File(sr);
    1.29 +            FileObject root = FileUtil.toFileObject(r);
    1.30 +
    1.31 +            if (root != null) {
    1.32 +                roots.add(root);
    1.33 +                rootFolders.add(new Folder(root));
    1.34 +            }
    1.35 +        }
    1.36 +
    1.37 +        ClassPath bootCP = createClassPath(parsed.has(bootclasspath) ? parsed.valuesOf(bootclasspath) : null, createDefaultBootClassPath());
    1.38 +        ClassPath compileCP = createClassPath(parsed.has(classpath) ? parsed.valuesOf(classpath) : null, ClassPath.EMPTY);
    1.39 +        final ClassPath sourceCP = createClassPath(parsed.has(sourcepath) ? parsed.valuesOf(sourcepath) : null, ClassPathSupport.createClassPath(roots.toArray(new FileObject[0])));
    1.40 +        final ClassPath binaryCP = ClassPathSupport.createProxyClassPath(bootCP, compileCP);
    1.41 +
    1.42          if (parsed.has("show-gui")) {
    1.43              if (parsed.has(configFile)) {
    1.44                  final File settingsFile = parsed.valueOf(configFile);
    1.45 @@ -180,7 +199,7 @@
    1.46                      SwingUtilities.invokeAndWait(new Runnable() {
    1.47                          @Override public void run() {
    1.48                              try {
    1.49 -                                showGUICustomizer(settingsFile);
    1.50 +                                showGUICustomizer(settingsFile, binaryCP, sourceCP);
    1.51                              } catch (IOException ex) {
    1.52                                  Exceptions.printStackTrace(ex);
    1.53                              } catch (BackingStoreException ex) {
    1.54 @@ -237,19 +256,6 @@
    1.55              org.netbeans.api.project.ui.OpenProjects.getDefault().getOpenProjects();
    1.56              RepositoryUpdater.getDefault().start(false);
    1.57  
    1.58 -            List<FileObject> roots = new ArrayList<FileObject>();
    1.59 -            List<Folder> rootFolders = new ArrayList<Folder>();
    1.60 -
    1.61 -            for (String sr : parsed.nonOptionArguments()) {
    1.62 -                File r = new File(sr);
    1.63 -                FileObject root = FileUtil.toFileObject(r);
    1.64 -
    1.65 -                if (root != null) {
    1.66 -                    roots.add(root);
    1.67 -                    rootFolders.add(new Folder(root));
    1.68 -                }
    1.69 -            }
    1.70 -
    1.71              if (roots.isEmpty() && !parsed.has("list")) {
    1.72                  System.err.println("no source roots to work on");
    1.73                  return 1;
    1.74 @@ -257,11 +263,6 @@
    1.75  
    1.76              Iterable<? extends HintDescription> hints;
    1.77              
    1.78 -            ClassPath bootCP = createClassPath(parsed.has(bootclasspath) ? parsed.valuesOf(bootclasspath) : null, createDefaultBootClassPath());
    1.79 -            ClassPath compileCP = createClassPath(parsed.has(classpath) ? parsed.valuesOf(classpath) : null, ClassPath.EMPTY);
    1.80 -            ClassPath sourceCP = createClassPath(parsed.has(sourcepath) ? parsed.valuesOf(sourcepath) : null, ClassPathSupport.createClassPath(roots.toArray(new FileObject[0])));
    1.81 -            ClassPath binaryCP = ClassPathSupport.createProxyClassPath(bootCP, compileCP);
    1.82 -
    1.83              if (parsed.has("list")) {
    1.84                  printHints(sourceCP, binaryCP);
    1.85                  return 0;
    1.86 @@ -480,19 +481,20 @@
    1.87          return descs;
    1.88      }
    1.89  
    1.90 -    private static Iterable<? extends HintDescription> readHints(ClassPath sourceFrom, ClassPath binaryFrom, Preferences toEnableIn, boolean declarative) {
    1.91 +    private static Iterable<? extends HintDescription> readHints(ClassPath sourceFrom, ClassPath binaryFrom, Preferences toEnableIn, boolean declarativeEnabledByDefault) {
    1.92          Map<HintMetadata, ? extends Collection<? extends HintDescription>> hardcoded = RulesManager.getInstance().readHints(null, Arrays.<ClassPath>asList(), null);
    1.93 -        Map<HintMetadata, ? extends Collection<? extends HintDescription>> all = declarative ? RulesManager.getInstance().readHints(null, Arrays.asList(sourceFrom, binaryFrom), null) : hardcoded;
    1.94 +        Map<HintMetadata, ? extends Collection<? extends HintDescription>> all = RulesManager.getInstance().readHints(null, Arrays.asList(sourceFrom, binaryFrom), null);
    1.95          List<HintDescription> descs = new LinkedList<HintDescription>();
    1.96  
    1.97          for (Entry<HintMetadata, ? extends Collection<? extends HintDescription>> entry: all.entrySet()) {
    1.98              if (hardcoded.containsKey(entry.getKey())) {
    1.99 -                if (HintsSettings.isEnabled(toEnableIn.node(entry.getKey().id), entry.getKey().enabled)) {
   1.100 +                if (HintsSettings.isEnabledWithDefault(toEnableIn.node(entry.getKey().id), false)) {
   1.101                      descs.addAll(entry.getValue());
   1.102                  }
   1.103              } else {
   1.104 -                assert declarative;
   1.105 -                descs.addAll(entry.getValue());
   1.106 +                if (HintsSettings.isEnabledWithDefault(toEnableIn.node(entry.getKey().id), declarativeEnabledByDefault)) {
   1.107 +                    descs.addAll(entry.getValue());
   1.108 +                }
   1.109              }
   1.110          }
   1.111  
   1.112 @@ -663,12 +665,15 @@
   1.113          return ClassPathSupport.createClassPath(rootURLs.toArray(new URL[0]));
   1.114      }
   1.115  
   1.116 -    private static void showGUICustomizer(File settings) throws IOException, BackingStoreException {
   1.117 -        final Preferences p = XMLHintPreferences.from(settings);
   1.118 -        JPanel hintPanel = new HintsPanel(p.node("settings"), new ClassPathBasedHintWrapper());
   1.119 -        final JCheckBox runDeclarativeHints = new JCheckBox("Run Declarative Rules");
   1.120 +    private static void showGUICustomizer(File settingsFile, ClassPath binaryCP, ClassPath sourceCP) throws IOException, BackingStoreException {
   1.121 +        GlobalPathRegistry.getDefault().register(ClassPath.COMPILE, new ClassPath[] {binaryCP});
   1.122 +        GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, new ClassPath[] {sourceCP});
   1.123 +        ClassPathBasedHintWrapper hints = new ClassPathBasedHintWrapper();
   1.124 +        final Preferences p = XMLHintPreferences.from(settingsFile);
   1.125 +        JPanel hintPanel = new HintsPanel(p.node("settings"), hints);
   1.126 +        final JCheckBox runDeclarativeHints = new JCheckBox("Always Run Declarative Rules");
   1.127  
   1.128 -        runDeclarativeHints.setToolTipText("Should the declarative rules found on classpath be run?");
   1.129 +        runDeclarativeHints.setToolTipText("Always run the declarative rules found on classpath? (Only those selected above will be run when unchecked.)");
   1.130          runDeclarativeHints.setSelected(p.getBoolean("runDeclarative", true));
   1.131          runDeclarativeHints.addActionListener(new ActionListener() {
   1.132              @Override public void actionPerformed(ActionEvent e) {
   1.133 @@ -831,122 +836,3 @@
   1.134      }
   1.135  
   1.136  }
   1.137 -
   1.138 -//    public static void main(String... args) throws IOException, ClassNotFoundException {
   1.139 -//        System.setProperty("netbeans.user", "/tmp/tmp-foo");
   1.140 -//
   1.141 -//        OptionParser parser = new OptionParser();
   1.142 -////        ArgumentAcceptingOptionSpec<File> projects = parser.accepts("project", "project(s) to refactor").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class);
   1.143 -//        ArgumentAcceptingOptionSpec<File> classpath = parser.accepts("classpath", "classpath").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class);
   1.144 -//        ArgumentAcceptingOptionSpec<File> cache = parser.accepts("cache", "source directory").withRequiredArg().ofType(File.class);
   1.145 -//        ArgumentAcceptingOptionSpec<String> hint = parser.accepts("hint", "hint name").withRequiredArg().ofType(String.class);
   1.146 -//
   1.147 -//        parser.accepts("list", "list all known hints");
   1.148 -//
   1.149 -//        OptionSet parsed;
   1.150 -//
   1.151 -//        try {
   1.152 -//            parsed = parser.parse(args);
   1.153 -//        } catch (OptionException ex) {
   1.154 -//            System.err.println(ex.getLocalizedMessage());
   1.155 -//            parser.printHelpOn(System.err);
   1.156 -//            return;
   1.157 -//        }
   1.158 -//
   1.159 -//        if (parsed.has("list")) {
   1.160 -//            listHints();
   1.161 -//            System.exit(0);
   1.162 -//        }
   1.163 -//
   1.164 -//        File cacheDir = parsed.valueOf(cache);
   1.165 -//
   1.166 -//        if (cacheDir.isFile()) {
   1.167 -//            System.err.println("cache directory exists and is a file");
   1.168 -//            System.exit(1);
   1.169 -//        }
   1.170 -//
   1.171 -//        String[] cacheDirContent = cacheDir.list();
   1.172 -//
   1.173 -//        if (cacheDirContent != null && cacheDirContent.length > 0 && !new File(cacheDir, "segments").exists()) {
   1.174 -//            System.err.println("cache directory is not empty, but was not created by this tool");
   1.175 -//            System.exit(1);
   1.176 -//        }
   1.177 -//
   1.178 -//        cacheDir.mkdirs();
   1.179 -//
   1.180 -//        CacheFolder.setCacheFolder(FileUtil.toFileObject(FileUtil.normalizeFile(cacheDir)));
   1.181 -//
   1.182 -//
   1.183 -//        Map<String, Object> attrs = new HashMap<String, Object>();
   1.184 -//
   1.185 -//        attrs.put("type", "org.netbeans.modules.java.j2seproject");
   1.186 -//        attrs.put("iconResource", "org/netbeans/modules/java/j2seproject/ui/resources/j2seProject.png");
   1.187 -//        attrs.put("sharedName", "data");
   1.188 -//        attrs.put("sharedNamespace", "http://www.netbeans.org/ns/j2se-project/3");
   1.189 -//        attrs.put("privateName", "data");
   1.190 -//        attrs.put("privateNamespace", "http://www.netbeans.org/ns/j2se-project-private/1");
   1.191 -//        attrs.put("className", "org.netbeans.modules.java.j2seproject.J2SEProject");
   1.192 -//        attrs.put("instanceClass", "org.netbeans.spi.project.support.ant.AntBasedProjectType");
   1.193 -//
   1.194 -//        MainLookup.register(AntBasedProjectFactorySingleton.create(attrs));
   1.195 -//
   1.196 -//        //XXX:
   1.197 -//        MainLookup.register(new AntBasedProjectFactorySingleton());
   1.198 -//
   1.199 -//        System.err.println(Lookup.getDefault().lookupAll(ClassPathProvider.class));
   1.200 -//
   1.201 -////        for (Object o : Lookups.forPath("Services/AntBasedProjectTypes").lookupAll(Object.class)) {
   1.202 -////            MainLookup.register(o);
   1.203 -////        }
   1.204 -//
   1.205 -//        org.netbeans.api.project.ui.OpenProjects.getDefault().getOpenProjects();
   1.206 -//        RepositoryUpdater.getDefault().start(false);
   1.207 -//
   1.208 -//        List<FileObject> roots = new ArrayList<FileObject>();
   1.209 -//        List<File> projectRoots = parsed.valuesOf(projects);
   1.210 -//
   1.211 -//        if (projectRoots.isEmpty()) {
   1.212 -//            System.err.println("no projects to work on specified");
   1.213 -//            System.exit(1);
   1.214 -//        }
   1.215 -//
   1.216 -//        for (File projectRoot : projectRoots) {
   1.217 -//            if (!projectRoot.isDirectory()) {
   1.218 -//                System.err.println("project: " + projectRoot + " does not exist");
   1.219 -//                continue;
   1.220 -//            }
   1.221 -//
   1.222 -//            Project prj = ProjectManager.getDefault().findProject(FileUtil.toFileObject(FileUtil.normalizeFile(projectRoot)));
   1.223 -//
   1.224 -//            if (prj == null) {
   1.225 -//                System.err.println("project: " + projectRoot + " cannot be resolved to NetBeans project");
   1.226 -//                continue;
   1.227 -//            }
   1.228 -//
   1.229 -//            SourceGroup[] sourceGroups = ProjectUtils.getSources(prj).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
   1.230 -//
   1.231 -//            for (SourceGroup sg : sourceGroups) {
   1.232 -//                roots.add(sg.getRootFolder());
   1.233 -//            }
   1.234 -//        }
   1.235 -//
   1.236 -//        if (roots.isEmpty()) {
   1.237 -//            System.err.println("no source roots to work on");
   1.238 -//            System.exit(1);
   1.239 -//        }
   1.240 -//
   1.241 -//        Iterable<? extends HintDescription> hints = findHints(parsed.valueOf(hint));
   1.242 -//
   1.243 -//        if (!hints.iterator().hasNext()) {
   1.244 -//            System.err.println("no hints specified");
   1.245 -//            System.exit(1);
   1.246 -//        }
   1.247 -//
   1.248 -//        try {
   1.249 -//            perform(hints, roots.toArray(new FileObject[0]));
   1.250 -//        } catch (Throwable e) {
   1.251 -//            e.printStackTrace();
   1.252 -//        }
   1.253 -//
   1.254 -//        System.exit(0);
   1.255 -//    }
     2.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateTool.java	Sat Jan 05 09:22:06 2013 +0100
     2.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateTool.java	Sat Jan 12 00:01:25 2013 +0100
     2.3 @@ -61,8 +61,8 @@
     2.4      @Override
     2.5      protected Info computeInfo() {
     2.6          return new Info().addAdditionalRoots(Main.class.getName(), DeclarativeHintsTestBase.class.getName(), OpenProjectsTrampolineImpl.class.getName(), J2SEProject.class.getName(), DefaultJavaPlatformProvider.class.getName(), PatternConvertorImpl.class.getName())
     2.7 -                         .addAdditionalResources("org/netbeans/modules/java/hints/resources/Bundle.properties")
     2.8 -                         .addAdditionalLayers("org/netbeans/modules/java/hints/resources/layer.xml")
     2.9 +                         .addAdditionalResources("org/netbeans/modules/java/hints/resources/Bundle.properties", "org/netbeans/modules/java/hints/declarative/resources/Bundle.properties")
    2.10 +                         .addAdditionalLayers("org/netbeans/modules/java/hints/resources/layer.xml", "org/netbeans/modules/java/hints/declarative/resources/layer.xml")
    2.11                           .addMetaInfRegistrations(new MetaInfRegistration(org.netbeans.modules.project.uiapi.OpenProjectsTrampoline.class, OpenProjectsTrampolineImpl.class))
    2.12                           .addMetaInfRegistrationToCopy(PatternConvertor.class.getName())
    2.13                           .addExcludePattern(Pattern.compile("junit\\.framework\\..*"))
     3.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sat Jan 05 09:22:06 2013 +0100
     3.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sat Jan 12 00:01:25 2013 +0100
     3.3 @@ -287,6 +287,84 @@
     3.4                        "--apply");
     3.5      }
     3.6  
     3.7 +    public void testConfigurationFileDeclarative1() throws Exception {
     3.8 +        String golden =
     3.9 +            "package test;\n" +
    3.10 +            "public class Test {\n" +
    3.11 +            "    private void test(java.util.Collection c) {\n" +
    3.12 +            "        boolean b1 = c.isEmpty();\n" +
    3.13 +            "        boolean b2 = c.size() <= 0;\n" +
    3.14 +            "    }\n" +
    3.15 +            "}\n";
    3.16 +
    3.17 +        doRunCompiler(golden,
    3.18 +                      null,
    3.19 +                      null,
    3.20 +                      "src/test/Test.java",
    3.21 +                      "package test;\n" +
    3.22 +                      "public class Test {\n" +
    3.23 +                      "    private void test(java.util.Collection c) {\n" +
    3.24 +                      "        boolean b1 = c.size() == 0;\n" +
    3.25 +                      "        boolean b2 = c.size() <= 0;\n" +
    3.26 +                      "    }\n" +
    3.27 +                      "}\n",
    3.28 +                      "META-INF/upgrade/test1.hint",
    3.29 +                      "$c.size() == 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
    3.30 +                      "META-INF/upgrade/test2.hint",
    3.31 +                      "$c.size() <= 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
    3.32 +                      "settings.xml",
    3.33 +                      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    3.34 +                      "<hints apply=\"true\" runDeclarative=\"false\">\n" +
    3.35 +                      "    <settings>\n" +
    3.36 +                      "        <test1.hint enabled=\"true\"/>\n" +
    3.37 +                      "    </settings>\n" +
    3.38 +                      "</hints>\n",
    3.39 +                      null,
    3.40 +                      "--config-file",
    3.41 +                      "${workdir}/settings.xml",
    3.42 +                      "--source",
    3.43 +                      "1.6");
    3.44 +    }
    3.45 +
    3.46 +    public void testConfigurationFileDeclarative2() throws Exception {
    3.47 +        String golden =
    3.48 +            "package test;\n" +
    3.49 +            "public class Test {\n" +
    3.50 +            "    private void test(java.util.Collection c) {\n" +
    3.51 +            "        boolean b1 = c.isEmpty();\n" +
    3.52 +            "        boolean b2 = c.isEmpty();\n" +
    3.53 +            "    }\n" +
    3.54 +            "}\n";
    3.55 +
    3.56 +        doRunCompiler(golden,
    3.57 +                      null,
    3.58 +                      null,
    3.59 +                      "src/test/Test.java",
    3.60 +                      "package test;\n" +
    3.61 +                      "public class Test {\n" +
    3.62 +                      "    private void test(java.util.Collection c) {\n" +
    3.63 +                      "        boolean b1 = c.size() == 0;\n" +
    3.64 +                      "        boolean b2 = c.size() <= 0;\n" +
    3.65 +                      "    }\n" +
    3.66 +                      "}\n",
    3.67 +                      "META-INF/upgrade/test1.hint",
    3.68 +                      "$c.size() == 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
    3.69 +                      "META-INF/upgrade/test2.hint",
    3.70 +                      "$c.size() <= 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
    3.71 +                      "settings.xml",
    3.72 +                      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    3.73 +                      "<hints apply=\"true\" runDeclarative=\"true\">\n" +
    3.74 +                      "    <settings>\n" +
    3.75 +                      "        <test1.hint enabled=\"true\"/>\n" +
    3.76 +                      "    </settings>\n" +
    3.77 +                      "</hints>\n",
    3.78 +                      null,
    3.79 +                      "--config-file",
    3.80 +                      "${workdir}/settings.xml",
    3.81 +                      "--source",
    3.82 +                      "1.6");
    3.83 +    }
    3.84 +
    3.85      private void doRunCompiler(String golden, String stdOut, String stdErr, String... fileContentAndExtraOptions) throws Exception {
    3.86          List<String> fileAndContent = new LinkedList<String>();
    3.87          List<String> extraOptions = new LinkedList<String>();