changeset 9293:f423d490ae1b 9-b86

8139450: JavaFX applications cannot be run with JDK 9-b85 promoted build Reviewed-by: jgiles
author kcr
date Mon, 12 Oct 2015 12:34:02 -0700
parents 924279a338d6
children c80562beebd4 bc6b18a6309a cad3c852c59b
files modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassScene.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassStage.java
diffstat 2 files changed, 50 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassScene.java	Fri Oct 09 13:53:15 2015 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassScene.java	Mon Oct 12 12:34:02 2015 -0700
@@ -48,14 +48,9 @@
 import com.sun.prism.impl.PrismSettings;
 import com.sun.prism.paint.Color;
 import com.sun.prism.paint.Paint;
-import sun.misc.JavaSecurityAccess;
-import sun.misc.SharedSecrets;
 
 abstract class GlassScene implements TKScene {
 
-    private static final JavaSecurityAccess javaSecurityAccess =
-            SharedSecrets.getJavaSecurityAccess();
-
     private GlassStage stage;
 
     protected TKSceneListener sceneListener;
@@ -119,7 +114,7 @@
         AccessControlContext acc = AccessController.getContext();
         // JDK doesn't provide public APIs to get ACC intersection,
         // so using this ugly workaround
-        accessCtrlCtx = javaSecurityAccess.doIntersectionPrivilege(
+        accessCtrlCtx = GlassStage.doIntersectionPrivilege(
                 () -> AccessController.getContext(), acc, ctx);
     }
 
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassStage.java	Fri Oct 09 13:53:15 2015 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassStage.java	Mon Oct 12 12:34:02 2015 -0700
@@ -25,6 +25,8 @@
 
 package com.sun.javafx.tk.quantum;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -40,13 +42,43 @@
 import com.sun.javafx.tk.TKStageListener;
 import com.sun.javafx.tk.Toolkit;
 
-import sun.misc.JavaSecurityAccess;
-import sun.misc.SharedSecrets;
-
 abstract class GlassStage implements TKStage {
 
-    private static final JavaSecurityAccess javaSecurityAccess =
-            SharedSecrets.getJavaSecurityAccess();
+    // Need to access via reflection since the SharedSecrets class moved to
+    // a new package in JDK 9 and we still build and test with JDK 8u.
+    private static final Object javaSecurityAccess;
+    private static final Method m_doIntersectionPrivilege;
+
+    static {
+        Class cls_SharedSecrets;
+        Class cls_JavaSecurityAccess;
+
+        try {
+            try {
+                // First look for these classes in their new location
+                cls_SharedSecrets = Class.forName("jdk.internal.misc.SharedSecrets");
+                cls_JavaSecurityAccess = Class.forName("jdk.internal.misc.JavaSecurityAccess");
+            } catch (ClassNotFoundException ex) {
+                // As a fallback, look for these classes in their old location
+                cls_SharedSecrets = Class.forName("sun.misc.SharedSecrets");
+                cls_JavaSecurityAccess = Class.forName("sun.misc.JavaSecurityAccess");
+            }
+
+            // JavaSecurityAccess jsa = SharedSecrets.getJavaSecurityAccess();
+            Method m_getJavaSecurityAccess = cls_SharedSecrets.getMethod("getJavaSecurityAccess",
+                    new Class[0]);
+            javaSecurityAccess = m_getJavaSecurityAccess.invoke(null);
+            m_doIntersectionPrivilege = cls_JavaSecurityAccess.getMethod("doIntersectionPrivilege",
+                    PrivilegedAction.class, AccessControlContext.class, AccessControlContext.class);
+
+        } catch (ClassNotFoundException
+                | NoSuchMethodException
+                | IllegalAccessException
+                | InvocationTargetException ex)
+        {
+            throw new SecurityException(ex);
+        }
+    }
 
     // A list of all GlassStage objects regardless of visibility.
     private static final List<GlassStage> windows = new ArrayList<>();
@@ -109,6 +141,17 @@
         return accessCtrlCtx;
     }
 
+    static AccessControlContext doIntersectionPrivilege(PrivilegedAction<AccessControlContext> action,
+                                                       AccessControlContext stack,
+                                                       AccessControlContext context) {
+        try {
+            return (AccessControlContext) m_doIntersectionPrivilege.invoke(
+                    javaSecurityAccess, action, stack, context);
+        } catch (IllegalAccessException | InvocationTargetException ex) {
+            throw new SecurityException(ex);
+        }
+    }
+
     public final void setSecurityContext(AccessControlContext ctx) {
         if (accessCtrlCtx != null) {
             throw new RuntimeException("Stage security context has been already set!");
@@ -116,7 +159,7 @@
         AccessControlContext acc = AccessController.getContext();
         // JDK doesn't provide public APIs to get ACC intersection,
         // so using this ugly workaround
-        accessCtrlCtx = javaSecurityAccess.doIntersectionPrivilege(
+        accessCtrlCtx = doIntersectionPrivilege(
                 () -> AccessController.getContext(), acc, ctx);
     }