8169069: Module system implementation refresh (11/2016)

Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Reviewed-by: alanb, mchung, sundar
diff --git a/nashorn/make/project.properties b/nashorn/make/project.properties
index 695992f..6fe7a05 100644
--- a/nashorn/make/project.properties
+++ b/nashorn/make/project.properties
@@ -154,7 +154,9 @@
     --add-exports jdk.scripting.nashorn/jdk.nashorn.internal.runtime.regexp=ALL-UNNAMED \
     --add-exports jdk.scripting.nashorn/jdk.nashorn.internal.runtime.regexp.joni=ALL-UNNAMED \
     --add-exports jdk.scripting.nashorn/jdk.nashorn.tools=ALL-UNNAMED \
-    --add-exports java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED
+    --add-exports java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED \
+    --add-opens jdk.scripting.nashorn/jdk.nashorn.internal.runtime=ALL-UNNAMED \
+    --add-opens jdk.scripting.nashorn/jdk.nashorn.internal.runtime.doubleconv=ALL-UNNAMED
 
 meta.inf.dir=${nashorn.module.src.dir}/META-INF
 
diff --git a/nashorn/src/jdk.dynalink/share/classes/module-info.java b/nashorn/src/jdk.dynalink/share/classes/module-info.java
index b9abdc3..5110d01 100644
--- a/nashorn/src/jdk.dynalink/share/classes/module-info.java
+++ b/nashorn/src/jdk.dynalink/share/classes/module-info.java
@@ -23,6 +23,9 @@
  * questions.
  */
 
+/**
+ * Dynalink
+ */
 module jdk.dynalink {
     requires java.logging;
 
diff --git a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java
index 953ca18..790fc62 100644
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/module-info.java
@@ -23,6 +23,9 @@
  * questions.
  */
 
+/**
+ * Nashorn shell module
+ */
 module jdk.scripting.nashorn.shell {
     requires java.desktop;
     requires java.compiler;
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
index 96bfda4..c40943b 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
@@ -320,7 +320,7 @@
     private final WeakValueCache<CodeSource, Class<?>> anonymousHostClasses = new WeakValueCache<>();
 
     private static final class AnonymousContextCodeInstaller extends ContextCodeInstaller {
-        private static final Unsafe UNSAFE = getUnsafe();
+        private static final Unsafe UNSAFE = Unsafe.getUnsafe();
         private static final String ANONYMOUS_HOST_CLASS_NAME = Compiler.SCRIPTS_PACKAGE.replace('/', '.') + ".AnonymousHost";
         private static final byte[] ANONYMOUS_HOST_CLASS_BYTES = getAnonymousHostClassBytes();
 
@@ -357,21 +357,6 @@
             cw.visitEnd();
             return cw.toByteArray();
         }
-
-        private static Unsafe getUnsafe() {
-            return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
-                @Override
-                public Unsafe run() {
-                    try {
-                        final Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
-                        theUnsafeField.setAccessible(true);
-                        return (Unsafe)theUnsafeField.get(null);
-                    } catch (final ReflectiveOperationException e) {
-                        throw new RuntimeException(e);
-                    }
-                }
-            });
-        }
     }
 
     /** Is Context global debug mode enabled ? */
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java
index c37f199..4a22352 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java
@@ -69,12 +69,12 @@
     private Module createModule(final String moduleName) {
         final Module structMod = context.getStructLoader().getModule();
         final ModuleDescriptor.Builder builder =
-            new ModuleDescriptor.Builder(moduleName)
+            ModuleDescriptor.module(moduleName)
                     .requires("java.base")
                     .requires("java.logging")
                     .requires(NASHORN_MODULE.getName())
                     .requires(structMod.getName())
-                    .conceals(SCRIPTS_PKG);
+                    .contains(SCRIPTS_PKG);
 
         if (Context.javaSqlFound) {
             builder.requires("java.sql");
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java
index be7d38b..075540a 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java
@@ -63,10 +63,10 @@
 
     private Module createModule(final String moduleName) {
         final ModuleDescriptor descriptor
-                = new ModuleDescriptor.Builder(moduleName)
+                = ModuleDescriptor.module(moduleName)
                     .requires("java.base")
                     .requires(NASHORN_MODULE.getName())
-                    .conceals(SCRIPTS_PKG)
+                    .contains(SCRIPTS_PKG)
                     .build();
 
         final Module mod = Context.createModuleTrusted(descriptor, this);
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/scripts/ModuleGraphManipulator.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/scripts/ModuleGraphManipulator.java
index cc9e76e..b3e165a 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/scripts/ModuleGraphManipulator.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/scripts/ModuleGraphManipulator.java
@@ -61,8 +61,8 @@
             throw new IllegalStateException(myClass + " loaded by wrong loader!");
         }
 
-        // From this module add a qualified export to nashorn module
-        MY_MODULE.addExports(MY_PKG_NAME, nashornModule);
+        // open package to nashorn module
+        MY_MODULE.addOpens(MY_PKG_NAME, nashornModule);
     }
 
     // The following method is reflectively invoked from Nashorn
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/module-info.java b/nashorn/src/jdk.scripting.nashorn/share/classes/module-info.java
index ab18e22..9a7dcaf 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/module-info.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/module-info.java
@@ -23,9 +23,12 @@
  * questions.
  */
 
+/**
+ * Nashorn
+ */
 module jdk.scripting.nashorn {
     requires java.logging;
-    requires public java.scripting;
+    requires transitive java.scripting;
     requires jdk.dynalink;
 
     exports jdk.nashorn.api.scripting;
diff --git a/nashorn/test/TEST.ROOT b/nashorn/test/TEST.ROOT
index f8dabb2..08e6c7f 100644
--- a/nashorn/test/TEST.ROOT
+++ b/nashorn/test/TEST.ROOT
@@ -8,7 +8,7 @@
 groups=TEST.groups
 
 # Minimum jtreg version
-requiredVersion=4.2 b03
+requiredVersion=4.2 b04
 
 # Use new module options
 useNewOptions=true
diff --git a/nashorn/test/script/nosecurity/JDK-8044798.js b/nashorn/test/script/nosecurity/JDK-8044798.js
index dcf1796..77e67f5 100644
--- a/nashorn/test/script/nosecurity/JDK-8044798.js
+++ b/nashorn/test/script/nosecurity/JDK-8044798.js
@@ -33,13 +33,14 @@
 
 var Arrays = Java.type("java.util.Arrays");
 var CharArray = Java.type("char[]");
+var Reflector = Java.type("jdk.nashorn.test.models.Reflector");
 var DebuggerSupport = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport");
 var DebuggerValueDesc = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport.DebuggerValueDesc");
 
 var valueDescFields = DebuggerValueDesc.class.declaredFields;
 Arrays.sort(valueDescFields, function(f1, f2) f1.name.compareTo(f2.name));
 for each (var f in valueDescFields) {
-    f.accessible = true;
+    Reflector.setAccessible(f);
 }
 
 var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;
@@ -49,7 +50,7 @@
 var getSourceInfoMethod, valueAsStringMethod;
 
 for each (var m in debuggerSupportMethods) {
-    m.accessible = true;
+    Reflector.setAccessible(m);
     switch (m.name) {
         case "eval":
             evalMethod = m;
@@ -129,10 +130,10 @@
 // private compile method of Context class
 var compileMethod = contextCls.getDeclaredMethod("compile",
                 sourceCls, errorMgrCls, booleanCls, booleanCls);
-compileMethod.accessible = true;
+Reflector.setAccessible(compileMethod);
 
 var getContextMethod = contextCls.getMethod("getContext");
-getContextMethod.accessible = true;
+Reflector.setAccessible(getContextMethod);
 
 var sourceForMethod = sourceCls.getMethod("sourceFor", stringCls, stringCls);
 var scriptCls = compileMethod.invoke(getContextMethod.invoke(null),
@@ -149,7 +150,7 @@
 
 print("Source info");
 for each (var f in srcInfoFields) {
-    f.accessible = true;
+    Reflector.setAccessible(f);
     var fieldValue = f.get(srcInfo);
     if (fieldValue instanceof CharArray) {
         fieldValue = new java.lang.String(fieldValue);
diff --git a/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/BignumTest.java b/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/BignumTest.java
index b34e105..9e2dc7d 100644
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/BignumTest.java
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/BignumTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -69,7 +69,7 @@
  * Bignum class tests
  *
  * @test
- * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.doubleconv
+ * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.doubleconv:open
  * @run testng jdk.nashorn.internal.runtime.doubleconv.test.BignumTest
  */
 @SuppressWarnings("javadoc")
diff --git a/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/DiyFpTest.java b/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/DiyFpTest.java
index 5432e53..662e380 100644
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/DiyFpTest.java
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/doubleconv/test/DiyFpTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -44,7 +44,7 @@
  * DiyFp class tests
  *
  * @test
- * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.doubleconv
+ * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime.doubleconv:open
  * @run testng jdk.nashorn.internal.runtime.doubleconv.test.DiyFpTest
  */
 @SuppressWarnings("javadoc")
diff --git a/nashorn/test/src/jdk/nashorn/test/models/Reflector.java b/nashorn/test/src/jdk/nashorn/test/models/Reflector.java
index decdcf3..9722694 100644
--- a/nashorn/test/src/jdk/nashorn/test/models/Reflector.java
+++ b/nashorn/test/src/jdk/nashorn/test/models/Reflector.java
@@ -26,6 +26,7 @@
 package jdk.nashorn.test.models;
 
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Module;
@@ -42,6 +43,22 @@
     private Reflector() {}
     private static final Module NASHORN_MOD = Context.class.getModule();
 
+    public static void setAccessible(Executable e) {
+        if (e.getDeclaringClass().getModule() != NASHORN_MOD) {
+            throw new RuntimeException(e + " is not from Nashorn module");
+        }
+
+        e.setAccessible(true);
+    }
+
+    public static void setAccessible(Field f) {
+        if (f.getDeclaringClass().getModule() != NASHORN_MOD) {
+            throw new RuntimeException(f + " is not from Nashorn module");
+        }
+
+        f.setAccessible(true);
+    }
+
     public static Object invoke(final Method m, final Object self, final Object...args) {
         if (m.getDeclaringClass().getModule() != NASHORN_MOD) {
             throw new RuntimeException(m + " is not from Nashorn module");