8166860: Add magic number to jmod file
authormchung
Tue, 04 Oct 2016 18:56:03 -0700
changeset 41440 abd777fa486c
parent 41439 ee4857e4fa85
child 41441 8fb8d9c6c687
8166860: Add magic number to jmod file Reviewed-by: alanb, jjg
langtools/make/tools/crules/MutableFieldsAnalyzer.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JDK9Wrappers.java
langtools/test/tools/javac/T8003967/DetectMutableStaticFields.java
--- a/langtools/make/tools/crules/MutableFieldsAnalyzer.java	Tue Oct 04 18:43:36 2016 -0700
+++ b/langtools/make/tools/crules/MutableFieldsAnalyzer.java	Tue Oct 04 18:56:03 2016 -0700
@@ -111,6 +111,8 @@
                 "loadMethod");
         ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$VMHelper",
                 "vmClass", "getRuntimeArgumentsMethod");
+        ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$JmodFile",
+                "jmodFileClass", "checkMagicMethod");
     }
 
 }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java	Tue Oct 04 18:43:36 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java	Tue Oct 04 18:56:03 2016 -0700
@@ -78,6 +78,7 @@
 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
 import com.sun.tools.javac.util.DefinedBy;
 import com.sun.tools.javac.util.DefinedBy.Api;
+import com.sun.tools.javac.util.JDK9Wrappers;
 import com.sun.tools.javac.util.ListBuffer;
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.jvm.ModuleNameReader;
@@ -1103,6 +1104,11 @@
 
                 if (p.getFileName().toString().endsWith(".jmod")) {
                     try {
+                        // check if the JMOD file is valid
+                        JDK9Wrappers.JmodFile.checkMagic(p);
+
+                        // No JMOD file system.  Use JarFileSystem to
+                        // workaround for now
                         FileSystem fs = fileSystems.get(p);
                         if (fs == null) {
                             URI uri = URI.create("jar:" + p.toUri());
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JDK9Wrappers.java	Tue Oct 04 18:43:36 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JDK9Wrappers.java	Tue Oct 04 18:56:03 2016 -0700
@@ -25,6 +25,7 @@
 
 package com.sun.tools.javac.util;
 
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.file.Path;
@@ -367,4 +368,41 @@
             }
         }
     }
+
+    /**
+     * Helper class for new method in jdk.internal.jmod.JmodFile
+     */
+    public static final class JmodFile {
+        public static final String JMOD_FILE_CLASSNAME = "jdk.internal.jmod.JmodFile";
+
+        public static void checkMagic(Path file) throws IOException {
+            try {
+                init();
+                checkMagicMethod.invoke(null, file);
+            } catch (InvocationTargetException ex) {
+                if (ex.getCause() instanceof IOException) {
+                    throw IOException.class.cast(ex.getCause());
+                }
+                throw new Abort(ex);
+            } catch (IllegalAccessException | IllegalArgumentException | SecurityException ex) {
+                throw new Abort(ex);
+            }
+        }
+
+        // -----------------------------------------------------------------------------------------
+
+        private static Class<?> jmodFileClass = null;
+        private static Method checkMagicMethod = null;
+
+        private static void init() {
+            if (jmodFileClass == null) {
+                try {
+                    jmodFileClass = Class.forName(JMOD_FILE_CLASSNAME, false, null);
+                    checkMagicMethod = jmodFileClass.getDeclaredMethod("checkMagic", Path.class);
+                } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
+                    throw new Abort(ex);
+                }
+            }
+        }
+    }
 }
--- a/langtools/test/tools/javac/T8003967/DetectMutableStaticFields.java	Tue Oct 04 18:43:36 2016 -0700
+++ b/langtools/test/tools/javac/T8003967/DetectMutableStaticFields.java	Tue Oct 04 18:56:03 2016 -0700
@@ -118,6 +118,8 @@
                 "loadMethod");
         ignore("com/sun/tools/javac/util/JDK9Wrappers$VMHelper",
                 "vmClass", "getRuntimeArgumentsMethod");
+        ignore("com/sun/tools/javac/util/JDK9Wrappers$JmodFile",
+                "jmodFileClass", "checkMagicMethod");
     }
 
     private final List<String> errors = new ArrayList<>();