changeset 14182:6533c9f9440d

Default interface to get rid off code duplication Reviewed-by: jlaskey
author jfdenise
date Thu, 08 Oct 2015 14:32:47 +0200
parents 9a3cd31c64df
children ec7800a48a29
files src/jdk.jlink/share/classes/jdk/tools/jlink/TaskHelper.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdImageFilePluginProvider.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdPluginProvider.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdResourcePluginProvider.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffImageFilePluginProvider.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffPluginProvider.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffPluginProviderSupport.java src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffResourcePluginProvider.java
diffstat 9 files changed, 129 insertions(+), 184 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/TaskHelper.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/TaskHelper.java	Thu Oct 08 14:32:47 2015 +0200
@@ -177,7 +177,7 @@
             Map<String, List<String>> seen = new HashMap<>();
             for (PluginProvider prov : ImagePluginProviderRepository.getPluginProviders(pluginsLayer)) {
                 if (prov instanceof CmdPluginProvider) {
-                    CmdPluginProvider provider = (CmdPluginProvider) prov;
+                    CmdPluginProvider<?> provider = (CmdPluginProvider<?>) prov;
                     if (provider.getToolOption() != null) {
                         for (Entry<String, List<String>> entry : seen.entrySet()) {
                             if (entry.getKey().equals(provider.getToolOption())
@@ -194,7 +194,7 @@
                                             Map<String, String> m = plugins.get(provider);
                                             if (m == null) {
                                                 m = new HashMap<>();
-                                                plugins.put(provider, m);
+                                                plugins.put(prov, m);
                                             }
                                             m.put(CmdPluginProvider.TOOL_ARGUMENT_PROPERTY, arg);
                                         },
@@ -208,7 +208,7 @@
                                             Map<String, String> m = plugins.get(provider);
                                             if (m == null) {
                                                 m = new HashMap<>();
-                                                plugins.put(provider, m);
+                                                plugins.put(prov, m);
                                             }
                                             m.put(other, arg);
                                         },
@@ -230,7 +230,7 @@
                             Map<String, String> m = new HashMap<>();
                             m.put(CmdPluginProvider.TOOL_ARGUMENT_PROPERTY,
                                     ImagePluginConfiguration.ON_ARGUMENT);
-                            plugins.put(provider, m);
+                            plugins.put(prov, m);
                         }
                     }
                 }
@@ -560,7 +560,7 @@
             for (PluginProvider prov : ImagePluginProviderRepository.
                     getPluginProviders(pluginOptions.pluginsLayer)) {
                 if (showsPlugin(prov, showsImageBuilder)) {
-                    CmdPluginProvider provider = (CmdPluginProvider) prov;
+                    CmdPluginProvider<?> provider = (CmdPluginProvider<?>) prov;
 
                     if (provider.getToolOption() != null) {
                         StringBuilder line = new StringBuilder();
@@ -568,7 +568,7 @@
                         if (provider.getToolArgument() != null) {
                             line.append(" ").append(provider.getToolArgument());
                         }
-                        line.append("\n ").append(provider.getDescription());
+                        line.append("\n ").append(prov.getDescription());
                         if (provider.getAdditionalOptions() != null) {
                             line.append("\n").append(bundleHelper.
                                     getMessage("main.plugin.additional.options")).
@@ -596,11 +596,11 @@
         public void showPlugins(PrintWriter log, boolean showsImageBuilder) {
             for (PluginProvider prov : ImagePluginProviderRepository.getPluginProviders(getPluginsLayer())) {
                 if (showsPlugin(prov, showsImageBuilder)) {
-                    CmdPluginProvider fact = (CmdPluginProvider) prov;
+                    CmdPluginProvider<?> fact = (CmdPluginProvider<?>) prov;
                     log.println("\n" + bundleHelper.getMessage("main.plugin.name")
-                            + ": " + fact.getName());
-                    Integer[] range = ImagePluginConfiguration.getRange(fact);
-                    String cat = range == null ? fact.getCategory() : fact.getCategory()
+                            + ": " + prov.getName());
+                    Integer[] range = ImagePluginConfiguration.getRange(prov);
+                    String cat = range == null ? prov.getCategory() : prov.getCategory()
                             + ". " + bundleHelper.getMessage("main.plugin.range.from")
                             + " " + range[0] + " " + bundleHelper.
                             getMessage("main.plugin.range.to") + " "
@@ -608,7 +608,7 @@
                     log.println(bundleHelper.getMessage("main.plugin.category")
                             + ": " + cat);
                     log.println(bundleHelper.getMessage("main.plugin.description")
-                            + ": " + fact.getDescription());
+                            + ": " + prov.getDescription());
                     log.println(bundleHelper.getMessage("main.plugin.argument")
                             + ": " + (fact.getToolArgument() == null
                                     ? bundleHelper.getMessage("main.plugin.no.value")
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ImagePluginConfiguration.java	Thu Oct 08 14:32:47 2015 +0200
@@ -39,8 +39,6 @@
 import java.util.Objects;
 import java.util.Properties;
 
-import jdk.tools.jlink.plugins.CmdImageFilePluginProvider;
-import jdk.tools.jlink.plugins.CmdResourcePluginProvider;
 import jdk.tools.jlink.plugins.ImageBuilder;
 import jdk.tools.jlink.plugins.ImageFilePlugin;
 import jdk.tools.jlink.plugins.ImageFilePluginProvider;
@@ -296,13 +294,11 @@
     }
 
     private static boolean isResourceProvider(PluginProvider prov) {
-        return prov instanceof ResourcePluginProvider
-                || prov instanceof CmdResourcePluginProvider;
+        return prov instanceof ResourcePluginProvider;
     }
 
     private static boolean isImageFileProvider(PluginProvider prov) {
-        return prov instanceof ImageFilePluginProvider
-                || prov instanceof CmdImageFilePluginProvider;
+        return prov instanceof ImageFilePluginProvider;
     }
 
     private static int getIndex(int index, String radical, String category,
@@ -442,14 +438,14 @@
 
         String radical = null;
         Map<String, Integer> ranges = null;
-        if (provider instanceof CmdResourcePluginProvider) {
+        if (isResourceProvider(provider)) {
             ranges = RESOURCES_RANGES;
             radical = RESOURCES_RADICAL_PROPERTY;
-        } else if (provider instanceof CmdImageFilePluginProvider) {
+        } else if (isImageFileProvider(provider)) {
             ranges = FILES_RANGES;
             radical = FILES_RADICAL_PROPERTY;
         } else {
-            throw new IllegalArgumentException("Unknown provider type");
+            throw new IllegalArgumentException("Unknown provider type" + provider);
         }
         int index = getNextIndex(properties, provider.getCategory(), radical, ranges);
         properties.setProperty(radical
@@ -521,9 +517,11 @@
         Plugin[] plugins = ImagePluginProviderRepository.newPlugins(config,
                 name, pluginsLayer);
         List<OrderedPlugin> ordered = new ArrayList<>();
-        for (Plugin plugin : plugins) {
-            ordered.add(new OrderedPlugin(index, plugin));
-            index = index+1;
+        if (plugins != null) {
+            for (Plugin plugin : plugins) {
+                ordered.add(new OrderedPlugin(index, plugin));
+                index = index + 1;
+            }
         }
         return ordered;
     }
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdImageFilePluginProvider.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdImageFilePluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -30,7 +30,8 @@
 /**
  * An ImageFile Plugin provider that creates command line oriented plugins.
  */
-public abstract class CmdImageFilePluginProvider extends CmdPluginProvider {
+public abstract class CmdImageFilePluginProvider extends ImageFilePluginProvider
+        implements CmdPluginProvider<ImageFilePlugin> {
 
     protected CmdImageFilePluginProvider(String name, String description) {
         super(name, description);
@@ -39,4 +40,10 @@
     @Override
     public abstract ImageFilePlugin[] newPlugins(String[] arguments,
             Map<String, String> otherOptions) throws IOException;
+
+    // Must be implemented, an abstract method can't be implemented with a default method
+    @Override
+    public ImageFilePlugin[] newPlugins(Map<String, Object> conf) throws IOException {
+        return CmdPluginProvider.super.newPlugins(conf);
+    }
 }
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdPluginProvider.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdPluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -28,6 +28,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -39,18 +40,15 @@
  * <li><code>CmdResourcePluginProvider</code></li>
  * <li><code>CmdImageFilePluginProvider</code></li>
  * </ul>
+ * @param <T>
  */
-public abstract class CmdPluginProvider extends PluginProvider {
+public interface CmdPluginProvider<T> {
 
     /**
      * This property is the main argument (if any) passed to the plugin.
      */
     public static final String TOOL_ARGUMENT_PROPERTY = "argument";
 
-    CmdPluginProvider(String name, String description) {
-        super(name, description);
-    }
-
     /**
      * Returns the description
      *
@@ -73,8 +71,7 @@
      */
     public abstract Map<String, String> getAdditionalOptions();
 
-    @Override
-    public final Plugin[] newPlugins(Map<String, Object> conf) throws IOException {
+    public default T[] newPlugins(Map<String, Object> conf) throws IOException {
         Map<String, String> config = toString(conf);
         String[] arguments = null;
         Collection<String> options = Collections.emptyList();
@@ -109,7 +106,7 @@
      * @return An array of plugins.
      * @throws IOException
      */
-    public abstract Plugin[] newPlugins(String[] arguments,
+    public abstract T[] newPlugins(String[] arguments,
             Map<String, String> otherOptions) throws IOException;
 
     static Map<String, String> toString(Map<String, Object> input) {
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdResourcePluginProvider.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/CmdResourcePluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -30,7 +30,8 @@
 /**
  * An Resource Plugin provider that creates command line oriented plugins.
  */
-public abstract class CmdResourcePluginProvider extends CmdPluginProvider {
+public abstract class CmdResourcePluginProvider extends ResourcePluginProvider
+        implements CmdPluginProvider<ResourcePlugin> {
 
     protected CmdResourcePluginProvider(String name, String description) {
         super(name, description);
@@ -39,4 +40,10 @@
     @Override
     public abstract ResourcePlugin[] newPlugins(String[] arguments,
             Map<String, String> otherOptions) throws IOException;
+
+    // Must be implemented, an abstract method can't be implemented with a default method
+    @Override
+    public ResourcePlugin[] newPlugins(Map<String, Object> conf) throws IOException {
+        return CmdPluginProvider.super.newPlugins(conf);
+    }
 }
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffImageFilePluginProvider.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffImageFilePluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -25,10 +25,7 @@
 package jdk.tools.jlink.plugins;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
-import jdk.tools.jlink.plugins.OnOffPluginProviderSupport.PluginBuilder;
 
 /**
  *
@@ -36,47 +33,18 @@
  * support. Plugin created by this provider can be enabled by default (enabled
  * although no option is provided to the command line).
  */
-public abstract class OnOffImageFilePluginProvider extends CmdImageFilePluginProvider {
+public abstract class OnOffImageFilePluginProvider extends ImageFilePluginProvider
+        implements OnOffPluginProvider<ImageFilePlugin> {
 
     public OnOffImageFilePluginProvider(String name, String description) {
         super(name, description);
     }
 
+    // Must be implemented, an abstract method can't be implemented with a default method
     @Override
-    public ImageFilePlugin[] newPlugins(String[] arguments,
-            Map<String, String> otherOptions)
-            throws IOException {
-        PluginBuilder<ImageFilePlugin> builder = (Map<String, String> otherOptions1) -> {
-            ImageFilePlugin[] ret = createPlugins(otherOptions1);
-            List<ImageFilePlugin> lst = new ArrayList<>();
-            if (ret != null) {
-                for (ImageFilePlugin p : ret) {
-                    lst.add(p);
-                }
-            }
-            return lst;
-        };
-        List<ImageFilePlugin> ret = OnOffPluginProviderSupport.newPlugins(arguments,
-                otherOptions, builder);
-        ImageFilePlugin[] arr = new ImageFilePlugin[ret.size()];
-        return ret.toArray(arr);
-    }
-
-    public abstract ImageFilePlugin[] createPlugins(Map<String, String> otherOptions)
-            throws IOException;
-
-    @Override
-    public String getToolArgument() {
-        return OnOffPluginProviderSupport.getToolArgument();
-    }
-
-    /**
-     * Plugin wishing to be enabled by default (no need for command line option)
-     * can override this method and return true.
-     *
-     * @return true, the plugin is enabled by default, otherwise is is not.
-     */
-    public boolean isEnabledByDefault() {
-        return false;
+    public ImageFilePlugin[] newPlugins(Map<String, Object> conf) throws IOException {
+        ImageFilePlugin[] arr = OnOffPluginProvider.super.newPlugins(conf);
+        arr = arr == null ? new ImageFilePlugin[0] : arr;
+        return arr;
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffPluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.tools.jlink.plugins;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Objects;
+import jdk.tools.jlink.internal.ImagePluginConfiguration;
+
+/**
+ *
+ * On/Off plugin provider support class.
+ * @param <T>
+ */
+public interface OnOffPluginProvider<T> extends CmdPluginProvider<T> {
+
+    @Override
+    public default T[] newPlugins(String[] arguments,
+            Map<String, String> otherOptions)
+            throws IOException {
+        Objects.requireNonNull(arguments);
+        if (arguments.length != 1) {
+            throw new IOException("Invalid number of arguments expecting "
+                    + getToolArgument());
+        }
+        if (!ImagePluginConfiguration.OFF_ARGUMENT.equals(arguments[0])
+                && !ImagePluginConfiguration.ON_ARGUMENT.equals(arguments[0])) {
+            throw new IOException("Invalid argument " + arguments[0]
+                    + ", expecting " + ImagePluginConfiguration.ON_ARGUMENT + " or "
+                    + ImagePluginConfiguration.OFF_ARGUMENT);
+        }
+        if (ImagePluginConfiguration.OFF_ARGUMENT.equals(arguments[0])) {
+            return null;
+        }
+        return createPlugins(otherOptions);
+    }
+
+    public T[] createPlugins(Map<String, String> otherOptions)
+            throws IOException;
+
+    @Override
+    public default String getToolArgument() {
+        return ImagePluginConfiguration.ON_ARGUMENT + "|"
+                + ImagePluginConfiguration.OFF_ARGUMENT;
+    }
+
+    public default boolean isEnabledByDefault() {
+        return false;
+    }
+
+}
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffPluginProviderSupport.java	Mon Oct 05 18:03:01 2015 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package jdk.tools.jlink.plugins;
-
-import java.io.IOException;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import jdk.tools.jlink.internal.ImagePluginConfiguration;
-
-/**
- *
- * On/Off plugin provider support class.
- */
-abstract class OnOffPluginProviderSupport {
-
-    interface PluginBuilder<T> {
-
-        List<T> buildPlugins(Map<String, String> otherOptions) throws IOException;
-    }
-
-    private OnOffPluginProviderSupport() {
-    }
-
-    static <T> List<T> newPlugins(String[] arguments,
-            Map<String, String> otherOptions, PluginBuilder<T> builder)
-            throws IOException {
-        Objects.requireNonNull(arguments);
-        if (arguments.length != 1) {
-            throw new IOException("Invalid number of arguments expecting "
-                    + getToolArgument());
-        }
-        if (!ImagePluginConfiguration.OFF_ARGUMENT.equals(arguments[0])
-                && !ImagePluginConfiguration.ON_ARGUMENT.equals(arguments[0])) {
-            throw new IOException("Invalid argument " + arguments[0]
-                    + ", expecting " + ImagePluginConfiguration.ON_ARGUMENT + " or "
-                    + ImagePluginConfiguration.OFF_ARGUMENT);
-        }
-        if (ImagePluginConfiguration.OFF_ARGUMENT.equals(arguments[0])) {
-            return Collections.emptyList();
-        }
-        return builder.buildPlugins(otherOptions);
-    }
-
-    static String getToolArgument() {
-        return ImagePluginConfiguration.ON_ARGUMENT + "|"
-                + ImagePluginConfiguration.OFF_ARGUMENT;
-    }
-
-}
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffResourcePluginProvider.java	Mon Oct 05 18:03:01 2015 -0700
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/plugins/OnOffResourcePluginProvider.java	Thu Oct 08 14:32:47 2015 +0200
@@ -25,10 +25,7 @@
 package jdk.tools.jlink.plugins;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
-import jdk.tools.jlink.plugins.OnOffPluginProviderSupport.PluginBuilder;
 
 /**
  *
@@ -36,48 +33,19 @@
  * support. Plugin created by this provider can be enabled by default (enabled
  * although no option is provided to the command line).
  */
-public abstract class OnOffResourcePluginProvider extends CmdResourcePluginProvider {
+public abstract class OnOffResourcePluginProvider extends ResourcePluginProvider
+        implements OnOffPluginProvider<ResourcePlugin> {
 
     public OnOffResourcePluginProvider(String name, String description) {
         super(name, description);
     }
 
+    // Must be implemented, an abstract method can't be implemented with a default method
     @Override
-    public ResourcePlugin[] newPlugins(String[] arguments,
-            Map<String, String> otherOptions)
-            throws IOException {
-        PluginBuilder<ResourcePlugin> builder = (Map<String, String> otherOptions1) -> {
-            ResourcePlugin[] ret = createPlugins(otherOptions1);
-            List<ResourcePlugin> lst = new ArrayList<>();
-            if (ret != null) {
-                for (ResourcePlugin p : ret) {
-                    lst.add(p);
-                }
-            }
-            return lst;
-        };
-        List<ResourcePlugin> ret = OnOffPluginProviderSupport.newPlugins(arguments,
-                otherOptions, builder);
-        ResourcePlugin[] arr = new ResourcePlugin[ret.size()];
-        return ret.toArray(arr);
-    }
-
-    public abstract ResourcePlugin[] createPlugins(Map<String, String> otherOptions)
-            throws IOException;
-
-    @Override
-    public String getToolArgument() {
-        return OnOffPluginProviderSupport.getToolArgument();
-    }
-
-    /**
-     * Plugin wishing to be enabled by default (no need for command line option)
-     * can override this method and return true.
-     *
-     * @return true, the plugin is enabled by default, otherwise is is not.
-     */
-    public boolean isEnabledByDefault() {
-        return false;
+    public ResourcePlugin[] newPlugins(Map<String, Object> conf) throws IOException {
+        ResourcePlugin[] arr = OnOffPluginProvider.super.newPlugins(conf);
+        arr = arr == null ? new ResourcePlugin[0] : arr;
+        return arr;
     }
 
 }