changeset 4202:b27ce9a1374e

8157512: AssertionError in javac when module-info < v53.0 Reviewed-by: jjg
author sadayapalam
date Tue, 16 Aug 2016 09:45:19 +0530
parents ec3c64f398de
children 17640a96c1a9
files src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples.not-yet.txt test/tools/javac/modules/AnachronisticModuleInfo/AnachronisticModuleInfoTest.java test/tools/javac/modules/AnachronisticModuleInfo/module-info.jcod
diffstat 5 files changed, 138 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Aug 16 09:13:33 2016 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Aug 16 09:45:19 2016 +0530
@@ -566,6 +566,11 @@
      * class name is of the form module-name.module-info.
      */
     Name readModuleInfoName(int i) {
+        if (majorVersion < Version.V53.major) {
+            throw badClassFile("anachronistic.module.info",
+                    Integer.toString(majorVersion),
+                    Integer.toString(minorVersion));
+        }
         int classIndex = poolIdx[i];
         if (buf[classIndex] == CONSTANT_Class) {
             int utf8Index = poolIdx[getChar(classIndex + 1)];
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Aug 16 09:13:33 2016 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Aug 16 09:45:19 2016 +0530
@@ -1871,6 +1871,10 @@
 compiler.misc.invalid.static.interface=\
     static method found in version {0}.{1} classfile
 
+# 0: string (classfile major version), 1: string (classfile minor version)
+compiler.misc.anachronistic.module.info=\
+    module declaration found in version {0}.{1} classfile
+
 # 0: name
 compiler.misc.file.doesnt.contain.class=\
     file does not contain class {0}
--- a/test/tools/javac/diags/examples.not-yet.txt	Tue Aug 16 09:13:33 2016 +0530
+++ b/test/tools/javac/diags/examples.not-yet.txt	Tue Aug 16 09:45:19 2016 +0530
@@ -117,6 +117,7 @@
 compiler.warn.outdir.is.in.exploded.module                    # No control over -d specified by test infrastructure
 compiler.err.invalid.module.specifier                         # Not possible (?)
 compiler.err.locn.cant.get.module.name.for.jar                # bad binary ? Infer module name failure
+compiler.misc.anachronistic.module.info                       # requires binaries compiled with EA compilers.
 compiler.misc.bad.module-info.name                            # bad class file
 compiler.err.locn.bad.module-info                             # bad class file
 compiler.err.locn.cant.read.file                              # bad class file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/modules/AnachronisticModuleInfo/AnachronisticModuleInfoTest.java	Tue Aug 16 09:45:19 2016 +0530
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/*
+ * @test
+ * @bug 8157512
+ * @summary AssertionError in javac when module-info < v53.0
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox toolbox.JavacTask module-info
+ * @run main AnachronisticModuleInfoTest
+ */
+
+import toolbox.JavacTask;
+import toolbox.Task;
+import toolbox.TestRunner;
+import toolbox.ToolBox;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class AnachronisticModuleInfoTest extends TestRunner {
+
+    protected ToolBox tb;
+
+    AnachronisticModuleInfoTest() {
+        super(System.err);
+        tb = new ToolBox();
+    }
+
+    public static void main(String... args) throws Exception {
+        AnachronisticModuleInfoTest t = new AnachronisticModuleInfoTest();
+        t.runTests();
+    }
+
+    /**
+     * Run all methods annotated with @Test, and throw an exception if any
+     * errors are reported..
+     *
+     * @throws Exception if any errors occurred
+     */
+    protected void runTests() throws Exception {
+        runTests(m -> new Object[] { Paths.get(m.getName()) });
+    }
+
+    Path[] findJavaFiles(Path... paths) throws IOException {
+        return tb.findJavaFiles(paths);
+    }
+
+    @Test
+    public void anachronisticModuleInfoTest(Path base) throws Exception {
+        Path src = base.resolve("src");
+        tb.writeJavaFiles(src, "class C { }");
+        String modulePath = System.getProperty("test.classes");
+
+        String log = new JavacTask(tb, Task.Mode.CMDLINE)
+                .options("-XDrawDiagnostics",
+                        "-upgrademodulepath", modulePath)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutput(Task.OutputKind.DIRECT);
+        String expected = "- compiler.err.cant.access: foo.module-info, (compiler.misc.bad.class.file.header: module-info.class, (compiler.misc.anachronistic.module.info: 52, 0))";
+        if (!log.contains(expected))
+            throw new Exception("expected output not found" + log);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/modules/AnachronisticModuleInfo/module-info.jcod	Tue Aug 16 09:45:19 2016 +0530
@@ -0,0 +1,38 @@
+class foo/module-info {
+  0xCAFEBABE;
+  0; // minor version
+  52; // version
+  [] { // Constant Pool
+    ; // first element is empty
+    class #6; // #1    
+    Utf8 "SourceFile"; // #2    
+    Utf8 "module-info.java"; // #3    
+    Utf8 "Module"; // #4    
+    Utf8 "java.base"; // #5    
+    Utf8 "foo/module-info"; // #6    
+  } // Constant Pool
+
+  0x8000; // access
+  #1;// this_cpx
+  #0;// super_cpx
+
+  [] { // Interfaces
+  } // Interfaces
+
+  [] { // fields
+  } // fields
+
+  [] { // methods
+  } // methods
+
+  [] { // Attributes
+    Attr(#2) { // SourceFile
+      #3;
+    } // end SourceFile
+    ;
+    Attr(#4) { // Module
+      0x0001000580000000;
+      0x00000000;
+    } // end Module
+  } // Attributes
+} // end class foo/module-info