< prev index next >

src/jdk.jartool/share/classes/sun/tools/jar/Main.java

Print this page

        

@@ -24,23 +24,16 @@
  */
 
 package sun.tools.jar;
 
 import java.io.*;
-import java.lang.module.Configuration;
-import java.lang.module.InvalidModuleDescriptorException;
-import java.lang.module.ModuleDescriptor;
+import java.lang.module.*;
 import java.lang.module.ModuleDescriptor.Exports;
 import java.lang.module.ModuleDescriptor.Provides;
 import java.lang.module.ModuleDescriptor.Opens;
 import java.lang.module.ModuleDescriptor.Requires;
 import java.lang.module.ModuleDescriptor.Version;
-import java.lang.module.ModuleFinder;
-import java.lang.module.ModuleReader;
-import java.lang.module.ModuleReference;
-import java.lang.module.ResolutionException;
-import java.lang.module.ResolvedModule;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.nio.file.Path;
 import java.nio.file.Files;
 import java.nio.file.Paths;

@@ -406,11 +399,11 @@
                 genIndex(rootjar, files);
             } else if (dflag) {
                 boolean found;
                 if (fname != null) {
                     try (ZipFile zf = new ZipFile(fname)) {
-                        found = printModuleDescriptor(zf);
+                        found = describeModule(zf);
                     }
                 } else {
                     try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) {
                         found = printModuleDescriptor(fin);
                     }

@@ -602,11 +595,11 @@
 
         /* parse file arguments */
         int n = args.length - count;
         if (n > 0) {
             if (dflag) {
-                // "--print-module-descriptor/-d" does not require file argument(s)
+                // "--describe-module/-d" does not require file argument(s)
                 usageError(formatMsg("error.bad.dflag", args[count]));
                 return false;
             }
             int version = BASE_VERSION;
             int k = 0;

@@ -1727,24 +1720,40 @@
             return "";
         return c.stream().map(e -> e.toString())
                            .collect(joining(", ", prefix, suffix));
     }
 
-    private boolean printModuleDescriptor(ZipFile zipFile)
-        throws IOException
-    {
+    private boolean describeModule(ZipFile zipFile) throws IOException {
         ZipEntry[] zes = zipFile.stream()
             .filter(e -> isModuleInfoEntry(e.getName()))
             .sorted(Validator.ENTRY_COMPARATOR)
             .toArray(ZipEntry[]::new);
-        if (zes.length == 0)
-            return false;
+
+        if (zes.length == 0) {
+            // No module descriptor found, derive the automatic module name
+            String fn = zipFile.getName();
+            ModuleFinder mf = ModuleFinder.of(Paths.get(fn));
+            try {
+                Set<ModuleReference> mref = mf.findAll();
+                if (mref.size() != 1)
+                    fatalError(formatMsg("error.expected.find.module", fn));
+                String nv = mref.iterator().next().descriptor().toNameAndVersion();
+                output(formatMsg("out.automodule.name", nv));
+            } catch (FindException e) {
+                String msg = formatMsg("error.unable.derive.automodule", fn);
+                Throwable t = e.getCause();
+                if (t != null)
+                    msg = msg + "\n" + t.getMessage();
+                output(msg);
+            }
+        } else {
         for (ZipEntry ze : zes) {
             try (InputStream is = zipFile.getInputStream(ze)) {
                 printModuleDescriptor(is, ze.getName());
             }
         }
+        }
         return true;
     }
 
     private boolean printModuleDescriptor(FileInputStream fis)
         throws IOException
< prev index next >