8210502: jdeps does not handle properly on analyzing a mixture of MR JARs and non-MR JARs
authormchung
Mon, 10 Sep 2018 12:48:57 -0700
changeset 51685 1f70116be2df
parent 51684 4e99f412148f
child 51686 b8b0da4a5f49
8210502: jdeps does not handle properly on analyzing a mixture of MR JARs and non-MR JARs Reviewed-by: alanb
src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ClassFileReader.java
test/langtools/tools/jdeps/MultiReleaseJar.java
test/langtools/tools/jdeps/foo/module-info.java
--- a/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ClassFileReader.java	Mon Sep 10 16:40:59 2018 +0200
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ClassFileReader.java	Mon Sep 10 12:48:57 2018 -0700
@@ -325,9 +325,6 @@
                 }
             } else {
                 jf = new JarFile(f, false, ZipFile.OPEN_READ, version);
-                if (!jf.isMultiRelease()) {
-                    throw new MultiReleaseException("err.multirelease.option.exists", f.getName());
-                }
             }
             return jf;
         }
--- a/test/langtools/tools/jdeps/MultiReleaseJar.java	Mon Sep 10 16:40:59 2018 +0200
+++ b/test/langtools/tools/jdeps/MultiReleaseJar.java	Mon Sep 10 12:48:57 2018 -0700
@@ -27,7 +27,7 @@
  * @summary Tests for jdeps tool with multi-release jar files
  * @modules jdk.jdeps/com.sun.tools.jdeps
  * @library mrjar mrjar/base mrjar/9 mrjar/10 mrjar/v9 mrjar/v10
- * @build test.* p.* q.*
+ * @build test.* p.* q.* foo/*
  * @run testng MultiReleaseJar
  */
 
@@ -37,11 +37,9 @@
 import org.testng.annotations.Test;
 
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.concurrent.TimeUnit;
 import java.util.stream.Stream;
 
 public class MultiReleaseJar {
@@ -54,10 +52,10 @@
     public void initialize() throws Exception {
         String testClassPath = System.getProperty("test.class.path", "");
         mrjar = Stream.of(testClassPath.split(File.pathSeparator))
-                .map(Paths::get)
-                .filter(e -> e.endsWith("mrjar"))
-                .findAny()
-                .orElseThrow(() -> new InternalError("mrjar not found"));
+                      .map(Paths::get)
+                      .filter(e -> e.endsWith("mrjar"))
+                      .findAny()
+                      .orElseThrow(() -> new InternalError("mrjar not found"));
         testJdk = System.getProperty("test.jdk");
         fileSep = System.getProperty("file.separator");
         cmdPath = Paths.get(testJdk, "bin");
@@ -65,10 +63,16 @@
 
     @Test
     public void basic() throws Exception {
-        // build the jar file
+        // build Version.jar, Version_9.jar and main.jar
         Result r = run("jar -cf Version.jar -C base test --release 9 -C 9 test --release 10 -C 10 test");
         checkResult(r);
 
+        r = run("jar -cf Version_9.jar -C base test --release 9 -C 9 test");
+        checkResult(r);
+
+        r = run("jar -cf Main.jar test/Main.class");
+        checkResult(r);
+
         // try out a bunch of things
         r = run("jdeps --multi-release 9  -v missing.jar");
         checkResult(r, false, "Warning: Path does not exist: missing.jar");
@@ -78,31 +82,31 @@
 
         r = run("jdeps --multi-release base  -v Version.jar");
         checkResult(r, true,
-                "Version.jar ->",
-                "test.Version",
-                "test.Version"
+            "Version.jar ->",
+            "test.Version",
+            "test.Version"
         );
 
         r = run("jdeps --multi-release 9  -v Version.jar");
         checkResult(r, true,
-                "Version.jar ->",
-                "9/test.NonPublic",
-                "9/test.NonPublic",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version"
+            "Version.jar ->",
+            "9/test.NonPublic",
+            "9/test.NonPublic",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version"
         );
 
         r = run("jdeps --multi-release 10  -v Version.jar");
         checkResult(r, true,
-                "Version.jar ->",
-                "10/test.Version",
-                "10/test.Version",
-                "10/test.Version",
-                "10/test.Version",
-                "9/test.NonPublic",
-                "9/test.NonPublic"
+            "Version.jar ->",
+            "10/test.Version",
+            "10/test.Version",
+            "10/test.Version",
+            "10/test.Version",
+            "9/test.NonPublic",
+            "9/test.NonPublic"
         );
 
         r = run("jdeps --multi-release 8  -v Version.jar");
@@ -111,82 +115,90 @@
         r = run("jdeps --multi-release 9.1  -v Version.jar");
         checkResult(r, false, "Error: invalid argument for option: 9.1");
 
-        r = run("jdeps -v -R -cp Version.jar test/Main.class");
+        runJdeps("test/Main.class");
+        runJdeps("Main.jar");
+    }
+
+
+    private void runJdeps(String path) throws Exception {
+        Result r = run("jdeps -v -R -cp Version.jar " + path);
         checkResult(r, false, "--multi-release option is not set");
 
-        r = run("jdeps -v -R -cp Version.jar -multi-release 9 test/Main.class");
+        r = run("jdeps -v -R -cp Version.jar -multi-release 9 " + path);
         checkResult(r, false,
-                "Error: unknown option: -multi-release",
-                "Usage: jdeps <options> <path",
-                "use --help"
+            "Error: unknown option: -multi-release",
+            "Usage: jdeps <options> <path",
+            "use --help"
         );
 
-        r = run("jdeps -v -R -cp Version.jar --multi-release 9 test/Main.class");
+        r = run("jdeps -v -R -cp Version.jar --multi-release 9 " + path);
+
+        String name = path;
+        int index = path.lastIndexOf('/');
+        if (index >= 0) {
+            name = path.substring(index + 1, path.length());
+        }
         checkResult(r, true,
-                "Main.class ->",
-                "Main.class ->",
-                "test.Main",
-                "test.Main",
-                "test.Main",
-                "Version.jar ->",
-                "9/test.NonPublic",
-                "9/test.NonPublic",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version"
+            name + " ->",
+            name + " ->",
+            "test.Main",
+            "test.Main",
+            "test.Main",
+            "Version.jar ->",
+            "9/test.NonPublic",
+            "9/test.NonPublic",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version"
         );
 
-        r = run("jdeps -v -R -cp Version.jar --multi-release 10 test/Main.class");
+        r = run("jdeps -v -R -cp Version.jar --multi-release 10 " + path);
         checkResult(r, true,
-                "Main.class ->",
-                "Main.class ->",
-                "test.Main",
-                "test.Main",
-                "test.Main",
-                "Version.jar ->",
-                "10/test.Version",
-                "10/test.Version",
-                "10/test.Version",
-                "10/test.Version",
-                "9/test.NonPublic",
-                "9/test.NonPublic"
+            name + " ->",
+            name + " ->",
+            "test.Main",
+            "test.Main",
+            "test.Main",
+            "Version.jar ->",
+            "10/test.Version",
+            "10/test.Version",
+            "10/test.Version",
+            "10/test.Version",
+            "9/test.NonPublic",
+            "9/test.NonPublic"
         );
 
-        r = run("jdeps -v -R -cp Version.jar --multi-release base test/Main.class");
+        r = run("jdeps -v -R -cp Version.jar --multi-release base " + path);
         checkResult(r, true,
-                "Main.class ->",
-                "Main.class ->",
-                "test.Main",
-                "test.Main",
-                "test.Main",
-                "Version.jar ->",
-                "test.Version",
-                "test.Version"
+            name + " ->",
+            name + " ->",
+            "test.Main",
+            "test.Main",
+            "test.Main",
+            "Version.jar ->",
+            "test.Version",
+            "test.Version"
         );
 
-        r = run("jdeps -v -R -cp Version.jar --multi-release 9.1 test/Main.class");
+        r = run("jdeps -v -R -cp Version.jar --multi-release 9.1 " + path);
         checkResult(r, false, "Error: invalid argument for option: 9.1");
 
-        // Rebuild jar without version 10
-        r = run("jar -cf Version.jar -C base test --release 9 -C 9 test");
-        checkResult(r);
-
-        // but ask for version 10
-        r = run("jdeps -v -R -cp Version.jar --multi-release 10 test/Main.class");
+        // Version_9.jar does not have any version 10 entry
+        r = run("jdeps -v -R -cp Version_9.jar --multi-release 10 " + path);
         checkResult(r, true,
-                "Main.class ->",
-                "Main.class ->",
-                "test.Main",
-                "test.Main",
-                "test.Main",
-                "Version.jar ->",
-                "9/test.NonPublic",
-                "9/test.NonPublic",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version",
-                "9/test.Version"
+            name + " ->",
+            name + " ->",
+            "test.Main",
+            "test.Main",
+            "test.Main",
+            "Version_9.jar ->",
+            "9/test.NonPublic",
+            "9/test.NonPublic",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version",
+            "9/test.Version"
         );
     }
 
@@ -198,27 +210,72 @@
 
         r = run("jdeps -v -R -cp PQ.jar --multi-release base PQ.jar");
         checkResult(r, true,
-                "PQ.jar -> java.base",
-                "p.Foo"
+            "PQ.jar -> java.base",
+            "p.Foo"
         );
 
         r = run("jdeps -v -R -cp PQ.jar --multi-release 9 PQ.jar");
         checkResult(r, true,
-                "PQ.jar -> java.base",
-                "9/p.Foo",
-                "9/p.Foo",
-                "9/q.Bar"
+            "PQ.jar -> java.base",
+            "9/p.Foo",
+            "9/p.Foo",
+            "9/q.Bar"
         );
 
 
         r = run("jdeps -v -R -cp PQ.jar --multi-release 10 PQ.jar");
         checkResult(r, true,
-                "PQ.jar -> java.base",
-                "10/q.Bar",
-                "10/q.Bar",
-                "10/q.Gee",
-                "9/p.Foo",
-                "9/p.Foo"
+            "PQ.jar -> java.base",
+            "10/q.Bar",
+            "10/q.Bar",
+            "10/q.Gee",
+            "9/p.Foo",
+            "9/p.Foo"
+        );
+    }
+
+    @Test
+    public void modularJar() throws Exception {
+        Result r = run("jar -cf foo.jar -C base p");
+        checkResult(r);
+
+        Path foo = Paths.get(System.getProperty("test.classes")).resolve("modules").resolve("foo");
+        r = run("jar -uf foo.jar --release 9 -C " + foo.toString() + " module-info.class --release 10 -C v10 q");
+        checkResult(r);
+
+        r = run("jdeps -v --multi-release 10 --module-path foo.jar -m foo");
+        checkResult(r, true,
+            "foo",                   // module name
+            "foo.jar",                      // the path to foo.jar
+            "requires mandated java.base",  // module dependences
+            "foo -> java.base",
+            "10/q.Bar",
+            "10/q.Bar",
+            "10/q.Gee",
+            "p.Foo"
+        );
+
+        r = run("jdeps --multi-release 9 --module-path foo.jar Main.jar");
+        checkResult(r, true,
+            "Main.jar ->",
+            "test",
+            "foo",                          // module name
+            "foo.jar",                      // the path to foo.jar
+            "requires mandated java.base",  // module dependences
+            "foo -> java.base",
+            "p"
+        );
+
+        r = run("jdeps --multi-release 10 --module-path foo.jar Main.jar");
+        checkResult(r, true,
+            "Main.jar ->",
+            "test",
+            "foo",                          // module name
+            "foo.jar",                      // the path to foo.jar
+            "requires mandated java.base",  // module dependences
+            "foo -> java.base",
+            "p",
+            "q"
         );
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/jdeps/foo/module-info.java	Mon Sep 10 12:48:57 2018 -0700
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2018, 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.
+ *
+ * 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.
+ */
+module foo {
+}