8212700: Change the mechanism by which JDK loads the platform-specific AWT Toolkit
authorprr
Fri, 10 May 2019 16:22:35 -0700
changeset 54877 dde07ac16610
parent 54876 da3834261f0c
child 54878 b267b838272f
8212700: Change the mechanism by which JDK loads the platform-specific AWT Toolkit Reviewed-by: serb, rriggs
src/java.base/share/classes/jdk/internal/util/SystemProps.java
src/java.base/share/native/libjava/System.c
src/java.base/share/native/libjava/java_props.h
src/java.base/unix/native/libjava/java_props_md.c
src/java.base/windows/native/libjava/java_props_md.c
src/java.desktop/macosx/classes/sun/awt/PlatformGraphicsInfo.java
src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java
src/java.desktop/share/classes/java/awt/Toolkit.java
src/java.desktop/unix/classes/sun/awt/PlatformGraphicsInfo.java
src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java
src/java.desktop/windows/classes/sun/awt/PlatformGraphicsInfo.java
src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java
test/jdk/java/awt/Toolkit/ToolkitPropertyTest/CheckToolkitSystemProperty.java
--- a/src/java.base/share/classes/jdk/internal/util/SystemProps.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.base/share/classes/jdk/internal/util/SystemProps.java	Fri May 10 16:22:35 2019 -0700
@@ -90,7 +90,6 @@
         putIfAbsent(props, "http.nonProxyHosts", raw.propDefault(Raw._http_nonProxyHosts_NDX));
         putIfAbsent(props, "ftp.nonProxyHosts", raw.propDefault(Raw._ftp_nonProxyHosts_NDX));
         putIfAbsent(props, "socksNonProxyHosts", raw.propDefault(Raw._socksNonProxyHosts_NDX));
-        putIfAbsent(props, "awt.toolkit", raw.propDefault(Raw._awt_toolkit_NDX));
         putIfAbsent(props, "sun.arch.abi", raw.propDefault(Raw._sun_arch_abi_NDX));
         putIfAbsent(props, "sun.arch.data.model", raw.propDefault(Raw._sun_arch_data_model_NDX));
         putIfAbsent(props, "sun.os.patch.level", raw.propDefault(Raw._sun_os_patch_level_NDX));
@@ -185,8 +184,7 @@
     public static class Raw {
         // Array indices written by native vmProperties()
         // The order is arbitrary (but alphabetic for convenience)
-        @Native private static final int _awt_toolkit_NDX = 0;
-        @Native private static final int _display_country_NDX = 1 + _awt_toolkit_NDX;
+        @Native private static final int _display_country_NDX = 0;
         @Native private static final int _display_language_NDX = 1 + _display_country_NDX;
         @Native private static final int _display_script_NDX = 1 + _display_language_NDX;
         @Native private static final int _display_variant_NDX = 1 + _display_script_NDX;
--- a/src/java.base/share/native/libjava/System.c	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.base/share/native/libjava/System.c	Fri May 10 16:22:35 2019 -0700
@@ -204,8 +204,6 @@
     /* patch level */
     PUTPROP(propArray, _sun_os_patch_level_NDX, sprops->patch_level);
 
-    PUTPROP(propArray, _awt_toolkit_NDX, sprops->awt_toolkit);
-
     PUTPROP_PlatformString(propArray, _java_io_tmpdir_NDX, sprops->tmp_dir);
 
     PUTPROP_PlatformString(propArray, _user_name_NDX, sprops->user_name);
--- a/src/java.base/share/native/libjava/java_props.h	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.base/share/native/libjava/java_props.h	Fri May 10 16:22:35 2019 -0700
@@ -68,8 +68,6 @@
     char *sun_stdout_encoding;
     char *sun_stderr_encoding;
 
-    char *awt_toolkit;
-
     char *unicode_encoding;     /* The default endianness of unicode
                                     i.e. UnicodeBig or UnicodeLittle   */
 
--- a/src/java.base/unix/native/libjava/java_props_md.c	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.base/unix/native/libjava/java_props_md.c	Fri May 10 16:22:35 2019 -0700
@@ -394,14 +394,6 @@
     /* patches/service packs installed */
     sprops.patch_level = NULL;      // leave it undefined
 
-    /* Java 2D/AWT properties */
-#ifdef MACOSX
-    // Always the same Toolkit on Mac OS X
-    sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit";
-#else
-    sprops.awt_toolkit = "sun.awt.X11.XToolkit";
-#endif
-
 #ifdef SI_ISALIST
     /* supported instruction sets */
     {
--- a/src/java.base/windows/native/libjava/java_props_md.c	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.base/windows/native/libjava/java_props_md.c	Fri May 10 16:22:35 2019 -0700
@@ -365,9 +365,6 @@
         return &sprops;
     }
 
-    /* AWT properties */
-    sprops.awt_toolkit = "sun.awt.windows.WToolkit";
-
     /* tmp dir */
     {
         WCHAR tmpdir[MAX_PATH + 1];
--- a/src/java.desktop/macosx/classes/sun/awt/PlatformGraphicsInfo.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/macosx/classes/sun/awt/PlatformGraphicsInfo.java	Fri May 10 16:22:35 2019 -0700
@@ -26,6 +26,7 @@
 package sun.awt;
 
 import java.awt.GraphicsEnvironment;
+import java.awt.Toolkit;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -42,6 +43,10 @@
         return new CGraphicsEnvironment();
     }
 
+    public static Toolkit createToolkit() {
+        return new sun.lwawt.macosx.LWCToolkit();
+    }
+
     /**
      * Returns true if the WindowServer is available, false otherwise.
      *
--- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Fri May 10 16:22:35 2019 -0700
@@ -190,10 +190,16 @@
     private static final boolean inAWT;
 
     public LWCToolkit() {
-        areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
-        //set system property if not yet assigned
-        System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
-        initAppkit(ThreadGroupUtils.getRootThreadGroup(), GraphicsEnvironment.isHeadless());
+        final String extraButtons = "sun.awt.enableExtraMouseButtons";
+        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+            areExtraMouseButtonsEnabled =
+                 Boolean.parseBoolean(System.getProperty(extraButtons, "true"));
+            //set system property if not yet assigned
+            System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
+            initAppkit(ThreadGroupUtils.getRootThreadGroup(),
+                       GraphicsEnvironment.isHeadless());
+            return null;
+        });
     }
 
     /*
--- a/src/java.desktop/share/classes/java/awt/Toolkit.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/share/classes/java/awt/Toolkit.java	Fri May 10 16:22:35 2019 -0700
@@ -78,6 +78,7 @@
 import sun.awt.AppContext;
 import sun.awt.HeadlessToolkit;
 import sun.awt.PeerEvent;
+import sun.awt.PlatformGraphicsInfo;
 import sun.awt.SunToolkit;
 
 /**
@@ -582,43 +583,17 @@
      * specified.
      *
      * @return     the default toolkit.
-     * @exception  AWTError  if a toolkit could not be found, or
-     *                 if one could not be accessed or instantiated.
+     * @throws  AWTError in case of an error loading assistive technologies.
      * @see java.util.ServiceLoader
      * @see javax.accessibility.AccessibilityProvider
      */
     public static synchronized Toolkit getDefaultToolkit() {
         if (toolkit == null) {
-            java.security.AccessController.doPrivileged(
-                    new java.security.PrivilegedAction<Void>() {
-                public Void run() {
-                    Class<?> cls = null;
-                    String nm = System.getProperty("awt.toolkit");
-                    try {
-                        cls = Class.forName(nm);
-                    } catch (ClassNotFoundException e) {
-                        ClassLoader cl = ClassLoader.getSystemClassLoader();
-                        if (cl != null) {
-                            try {
-                                cls = cl.loadClass(nm);
-                            } catch (final ClassNotFoundException ignored) {
-                                throw new AWTError("Toolkit not found: " + nm);
-                            }
-                        }
-                    }
-                    try {
-                        if (cls != null) {
-                            toolkit = (Toolkit)cls.getConstructor().newInstance();
-                            if (GraphicsEnvironment.isHeadless()) {
-                                toolkit = new HeadlessToolkit(toolkit);
-                            }
-                        }
-                    } catch (final ReflectiveOperationException ignored) {
-                        throw new AWTError("Could not create Toolkit: " + nm);
-                    }
-                    return null;
-                }
-            });
+            toolkit = PlatformGraphicsInfo.createToolkit();
+            if (GraphicsEnvironment.isHeadless() &&
+                !(toolkit instanceof HeadlessToolkit)) {
+                toolkit = new HeadlessToolkit(toolkit);
+            }
             if (!GraphicsEnvironment.isHeadless()) {
                 loadAssistiveTechnologies();
             }
--- a/src/java.desktop/unix/classes/sun/awt/PlatformGraphicsInfo.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/unix/classes/sun/awt/PlatformGraphicsInfo.java	Fri May 10 16:22:35 2019 -0700
@@ -26,6 +26,7 @@
 package sun.awt;
 
 import java.awt.GraphicsEnvironment;
+import java.awt.Toolkit;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -35,6 +36,10 @@
         return new X11GraphicsEnvironment();
     }
 
+    public static Toolkit createToolkit() {
+        return new sun.awt.X11.XToolkit();
+    }
+
     /**
       * Called from java.awt.GraphicsEnvironment when
       * to check if on this platform, the JDK should default to
--- a/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java	Fri May 10 16:22:35 2019 -0700
@@ -346,10 +346,14 @@
 
             arrowCursor = XlibWrapper.XCreateFontCursor(XToolkit.getDisplay(),
                 XCursorFontConstants.XC_arrow);
-            areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
-            //set system property if not yet assigned
-            System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
-
+            final String extraButtons = "sun.awt.enableExtraMouseButtons";
+            AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+                areExtraMouseButtonsEnabled =
+                    Boolean.parseBoolean(System.getProperty(extraButtons, "true"));
+                //set system property if not yet assigned
+                System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
+                return null;
+            });
             // Detect display mode changes
             XlibWrapper.XSelectInput(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), XConstants.StructureNotifyMask);
             XToolkit.addEventDispatcher(XToolkit.getDefaultRootWindow(), new XEventDispatcher() {
--- a/src/java.desktop/windows/classes/sun/awt/PlatformGraphicsInfo.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/windows/classes/sun/awt/PlatformGraphicsInfo.java	Fri May 10 16:22:35 2019 -0700
@@ -26,6 +26,7 @@
 package sun.awt;
 
 import java.awt.GraphicsEnvironment;
+import java.awt.Toolkit;
 
 public class PlatformGraphicsInfo {
 
@@ -33,6 +34,10 @@
         return new Win32GraphicsEnvironment();
     }
 
+    public static Toolkit createToolkit() {
+        return new sun.awt.windows.WToolkit();
+    }
+
     public static boolean getDefaultHeadlessProperty() {
         // On Windows, we assume we can always create headful apps.
         // Here is where we can add code that would actually check.
--- a/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java	Thu May 09 16:09:39 2019 -0700
+++ b/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java	Fri May 10 16:22:35 2019 -0700
@@ -245,10 +245,13 @@
         ThreadGroup rootTG = AccessController.doPrivileged(
                 (PrivilegedAction<ThreadGroup>) ThreadGroupUtils::getRootThreadGroup);
         if (!startToolkitThread(this, rootTG)) {
-            String name = "AWT-Windows";
-            Thread toolkitThread = new Thread(rootTG, this, name, 0, false);
-            toolkitThread.setDaemon(true);
-            toolkitThread.start();
+            final String name = "AWT-Windows";
+            AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+                Thread toolkitThread = new Thread(rootTG, this, name, 0, false);
+                toolkitThread.setDaemon(true);
+                toolkitThread.start();
+                return null;
+            });
         }
 
         try {
@@ -264,10 +267,14 @@
         // Enabled "live resizing" by default.  It remains controlled
         // by the native system though.
         setDynamicLayout(true);
-
-        areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
-        //set system property if not yet assigned
-        System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
+        final String extraButtons = "sun.awt.enableExtraMouseButtons";
+        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+            areExtraMouseButtonsEnabled =
+                 Boolean.parseBoolean(System.getProperty(extraButtons, "true"));
+            //set system property if not yet assigned
+            System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
+            return null;
+        });
         setExtraMouseButtonsEnabledNative(areExtraMouseButtonsEnabled);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/awt/Toolkit/ToolkitPropertyTest/CheckToolkitSystemProperty.java	Fri May 10 16:22:35 2019 -0700
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2019, 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 8212700
+ * @summary verify the AWT Toolkit implementation class name is not
+ *          polluting system properties
+ */
+
+public class CheckToolkitSystemProperty {
+
+     public static void main(String[] args) {
+         String tkProp = System.getProperty("awt.toolkit");
+         if (tkProp != null) {
+             throw new RuntimeException("tkProp = " + tkProp);
+         }
+     }
+}