src/share/classes/org/openjdk/jigsaw/cli/Commands.java

Print this page




  26 package org.openjdk.jigsaw.cli;
  27 
  28 import java.lang.module.*;
  29 import java.io.*;
  30 import java.util.*;
  31 import java.util.regex.*;
  32 
  33 import static java.lang.System.out;
  34 import static java.lang.System.err;
  35 
  36 import org.openjdk.jigsaw.*;
  37 import org.openjdk.internal.joptsimple.*;
  38 
  39 
  40 /**
  41  * Commands shared by multiple CLIs
  42  */
  43 
  44 class Commands {
  45 
  46     private static JigsawModuleSystem jms
  47         = JigsawModuleSystem.instance();
  48 
  49     private static void formatCommaList(PrintStream out,
  50                                         String prefix,
  51                                         Collection<?> list)
  52     {
  53         if (list.isEmpty())
  54             return;
  55         out.format("  %s", prefix);
  56         boolean first = true;
  57         for (Object ob : list) {
  58             if (first) {
  59                 out.format(" %s", ob);
  60                 first = false;
  61             } else {
  62                 out.format(", %s", ob);
  63             }
  64         }
  65         out.format("%n");
  66     }


  72                         view.aliases());
  73         formatCommaList(out, indent + "permits",
  74                         view.permits());
  75         Map<String,Set<String>> services = view.services();
  76         for (Map.Entry<String,Set<String>> entry: services.entrySet()) {
  77             String sn = entry.getKey();
  78             for (String impl: entry.getValue()) {
  79                 out.format("%s  provides service %s with %s%n", indent, sn, impl);
  80             }
  81         }
  82         
  83         if (!view.exports().isEmpty()) {
  84             out.format("  %sexports%n", indent);
  85             Set<String> exports = new TreeSet<>(view.exports());
  86             for (String pn : exports) {
  87                 out.format("  %s  %s%n", indent, pn);
  88             }
  89         }
  90     }
  91     
  92     private static void listCommand(Catalog cat, ModuleIdQuery midq,
  93                                     boolean parents, boolean verbose)
  94         throws Command.Exception
  95     {
  96         int n = 0;
  97         try {
  98             List<ModuleId> mids;
  99             if (midq == null) {
 100                 mids = parents ? cat.listDeclaringModuleIds() : cat.listLocalDeclaringModuleIds();
 101             } else { 
 102                 List<ModuleId> list = parents ? cat.listModuleIds()
 103                                               : cat.listLocalModuleIds();
 104                 mids = new ArrayList<>();
 105                 for (ModuleId mid : list) {
 106                     if (midq.matches(mid))
 107                         mids.add(mid);
 108                 }
 109             }
 110             for (ModuleId mid : mids) {
 111                 ModuleInfo mi = cat.readModuleInfo(mid);
 112                 if (verbose) {
 113                     out.format("%n");
 114                 }
 115 
 116                 // print module and its views
 117                 out.format("%s%n", mi.id());
 118                 n++;
 119                 if (verbose) {
 120                     for (ViewDependence d : mi.requiresModules()) {
 121                         out.format("  %s%n", d);
 122                     }
 123                     for (ServiceDependence sd: mi.requiresServices()) {
 124                         out.format("  %s%n", sd);
 125                     }
 126                     formatModuleView(out, mi.defaultView(), "");
 127 
 128                     for (ModuleView mv : mi.views()) {
 129                         if (mv == mi.defaultView())
 130                             continue;
 131 
 132                         out.format("  view %s%n", mv.id().name());
 133                         formatModuleView(out, mv, "  ");
 134                     }
 135                 }
 136             }


























 137         } catch (IOException x) {
 138             throw new Command.Exception(x);
 139         }
 140         if (verbose && n > 0)
 141             out.format("%n");
 142     }
 143 
 144     private static Catalog compose(ModuleIdQuery midq,
 145                                    List<? extends Catalog> cats)
 146         throws IOException
 147     {
 148 
 149         final Set<ModuleId> modules = new HashSet<>();
 150         final Map<ModuleId,ModuleInfo> mods = new HashMap<>();
 151         for (Catalog c : cats) {
 152             List<ModuleId> mids = c.listDeclaringModuleIds();
 153             modules.addAll(mids);
 154             for (ModuleId mid : mids) {
 155                 if (mods.containsKey(mid))
 156                     continue;




  26 package org.openjdk.jigsaw.cli;
  27 
  28 import java.lang.module.*;
  29 import java.io.*;
  30 import java.util.*;
  31 import java.util.regex.*;
  32 
  33 import static java.lang.System.out;
  34 import static java.lang.System.err;
  35 
  36 import org.openjdk.jigsaw.*;
  37 import org.openjdk.internal.joptsimple.*;
  38 
  39 
  40 /**
  41  * Commands shared by multiple CLIs
  42  */
  43 
  44 class Commands {
  45 
  46     private static final JigsawModuleSystem jms
  47         = JigsawModuleSystem.instance();
  48 
  49     private static void formatCommaList(PrintStream out,
  50                                         String prefix,
  51                                         Collection<?> list)
  52     {
  53         if (list.isEmpty())
  54             return;
  55         out.format("  %s", prefix);
  56         boolean first = true;
  57         for (Object ob : list) {
  58             if (first) {
  59                 out.format(" %s", ob);
  60                 first = false;
  61             } else {
  62                 out.format(", %s", ob);
  63             }
  64         }
  65         out.format("%n");
  66     }


  72                         view.aliases());
  73         formatCommaList(out, indent + "permits",
  74                         view.permits());
  75         Map<String,Set<String>> services = view.services();
  76         for (Map.Entry<String,Set<String>> entry: services.entrySet()) {
  77             String sn = entry.getKey();
  78             for (String impl: entry.getValue()) {
  79                 out.format("%s  provides service %s with %s%n", indent, sn, impl);
  80             }
  81         }
  82 
  83         if (!view.exports().isEmpty()) {
  84             out.format("  %sexports%n", indent);
  85             Set<String> exports = new TreeSet<>(view.exports());
  86             for (String pn : exports) {
  87                 out.format("  %s  %s%n", indent, pn);
  88             }
  89         }
  90     }
  91 
  92     protected static void formatModule(PrintStream out, ModuleInfo mi,
  93                                        boolean verbose)

  94     {




















  95         // print module and its views
  96         out.format("%s%n", mi.id());
  97 
  98         if (verbose) {
  99             for (ViewDependence d : mi.requiresModules()) {
 100                 out.format("  %s%n", d);
 101             }
 102             for (ServiceDependence sd: mi.requiresServices()) {
 103                 out.format("  %s%n", sd);
 104             }
 105             formatModuleView(out, mi.defaultView(), "");
 106 
 107             for (ModuleView mv : mi.views()) {
 108                 if (mv == mi.defaultView())
 109                     continue;
 110 
 111                 out.format("  view %s%n", mv.id().name());
 112                 formatModuleView(out, mv, "  ");
 113             }
 114         }
 115     }
 116 
 117     private static void listCommand(Catalog cat, ModuleIdQuery midq,
 118                                     boolean parents, boolean verbose)
 119         throws Command.Exception
 120     {
 121         int n = 0;
 122         try {
 123             List<ModuleId> mids;
 124             if (midq == null) {
 125                 mids = parents ? cat.listDeclaringModuleIds()
 126                                : cat.listLocalDeclaringModuleIds();
 127             } else {
 128                 List<ModuleId> list = parents ? cat.listModuleIds()
 129                                               : cat.listLocalModuleIds();
 130                 mids = new ArrayList<>();
 131                 for (ModuleId mid : list) {
 132                     if (midq.matches(mid))
 133                         mids.add(mid);
 134                 }
 135             }
 136             for (ModuleId mid : mids) {
 137                 if (verbose)
 138                     out.format("%n");
 139                 formatModule(out, cat.readModuleInfo(mid), verbose);
 140                 n++;
 141             }
 142         } catch (IOException x) {
 143             throw new Command.Exception(x);
 144         }
 145         if (verbose && n > 0)
 146             out.format("%n");
 147     }
 148 
 149     private static Catalog compose(ModuleIdQuery midq,
 150                                    List<? extends Catalog> cats)
 151         throws IOException
 152     {
 153 
 154         final Set<ModuleId> modules = new HashSet<>();
 155         final Map<ModuleId,ModuleInfo> mods = new HashMap<>();
 156         for (Catalog c : cats) {
 157             List<ModuleId> mids = c.listDeclaringModuleIds();
 158             modules.addAll(mids);
 159             for (ModuleId mid : mids) {
 160                 if (mods.containsKey(mid))
 161                     continue;