8212700: Change the mechanism by which JDK loads the platform-specific AWT Toolkit

Reviewed-by: serb, rriggs
This commit is contained in:
Phil Race 2019-05-10 16:22:35 -07:00
parent 7f841d6d9a
commit 8ca1211aab
13 changed files with 95 additions and 66 deletions

View file

@ -90,7 +90,6 @@ public final class SystemProps {
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 final class SystemProps {
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;

View file

@ -204,8 +204,6 @@ Java_jdk_internal_util_SystemProps_00024Raw_platformProperties(JNIEnv *env, jcla
/* 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);

View file

@ -68,8 +68,6 @@ typedef struct {
char *sun_stdout_encoding;
char *sun_stderr_encoding;
char *awt_toolkit;
char *unicode_encoding; /* The default endianness of unicode
i.e. UnicodeBig or UnicodeLittle */

View file

@ -394,14 +394,6 @@ GetJavaProperties(JNIEnv *env)
/* 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 */
{

View file

@ -365,9 +365,6 @@ GetJavaProperties(JNIEnv* env)
return &sprops;
}
/* AWT properties */
sprops.awt_toolkit = "sun.awt.windows.WToolkit";
/* tmp dir */
{
WCHAR tmpdir[MAX_PATH + 1];

View file

@ -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 @@ public class PlatformGraphicsInfo {
return new CGraphicsEnvironment();
}
public static Toolkit createToolkit() {
return new sun.lwawt.macosx.LWCToolkit();
}
/**
* Returns true if the WindowServer is available, false otherwise.
*

View file

@ -190,10 +190,16 @@ public final class LWCToolkit extends LWToolkit {
private static final boolean inAWT;
public LWCToolkit() {
areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
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("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
initAppkit(ThreadGroupUtils.getRootThreadGroup(), GraphicsEnvironment.isHeadless());
System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
initAppkit(ThreadGroupUtils.getRootThreadGroup(),
GraphicsEnvironment.isHeadless());
return null;
});
}
/*

View file

@ -78,6 +78,7 @@ import sun.awt.AWTPermissions;
import sun.awt.AppContext;
import sun.awt.HeadlessToolkit;
import sun.awt.PeerEvent;
import sun.awt.PlatformGraphicsInfo;
import sun.awt.SunToolkit;
/**
@ -582,43 +583,17 @@ public abstract class Toolkit {
* 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 = PlatformGraphicsInfo.createToolkit();
if (GraphicsEnvironment.isHeadless() &&
!(toolkit instanceof HeadlessToolkit)) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (final ReflectiveOperationException ignored) {
throw new AWTError("Could not create Toolkit: " + nm);
}
return null;
}
});
if (!GraphicsEnvironment.isHeadless()) {
loadAssistiveTechnologies();
}

View file

@ -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 @@ public class PlatformGraphicsInfo {
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

View file

@ -346,10 +346,14 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
arrowCursor = XlibWrapper.XCreateFontCursor(XToolkit.getDisplay(),
XCursorFontConstants.XC_arrow);
areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
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("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
return null;
});
// Detect display mode changes
XlibWrapper.XSelectInput(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), XConstants.StructureNotifyMask);
XToolkit.addEventDispatcher(XToolkit.getDefaultRootWindow(), new XEventDispatcher() {

View file

@ -26,6 +26,7 @@
package sun.awt;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
public class PlatformGraphicsInfo {
@ -33,6 +34,10 @@ public class PlatformGraphicsInfo {
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.

View file

@ -245,10 +245,13 @@ public final class WToolkit extends SunToolkit implements Runnable {
ThreadGroup rootTG = AccessController.doPrivileged(
(PrivilegedAction<ThreadGroup>) ThreadGroupUtils::getRootThreadGroup);
if (!startToolkitThread(this, rootTG)) {
String name = "AWT-Windows";
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 @@ public final class WToolkit extends SunToolkit implements Runnable {
// Enabled "live resizing" by default. It remains controlled
// by the native system though.
setDynamicLayout(true);
areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
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("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
System.setProperty(extraButtons, ""+areExtraMouseButtonsEnabled);
return null;
});
setExtraMouseButtonsEnabledNative(areExtraMouseButtonsEnabled);
}

View file

@ -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);
}
}
}