From 961dcffc862a4830fbf26791835a98c12d4b513e Mon Sep 17 00:00:00 2001 From: Alexander Scherbatiy Date: Tue, 28 Sep 2021 09:26:51 +0000 Subject: [PATCH] 8273581: Change the mechanism by which JDK loads the platform-specific FontManager class Reviewed-by: prr, psadhukhan, azvegint, aivanov, serb --- .../classes/sun/font/PlatformFontInfo.java | 37 +++++++++++ .../classes/sun/font/FontManagerFactory.java | 61 ++++--------------- .../classes/sun/font/PlatformFontInfo.java | 39 ++++++++++++ .../unix/native/libawt/awt/awt_LoadLibrary.c | 28 +-------- .../classes/sun/font/PlatformFontInfo.java | 39 ++++++++++++ .../font/CheckFontManagerSystemProperty.java | 46 ++++++++++++++ 6 files changed, 174 insertions(+), 76 deletions(-) create mode 100644 src/java.desktop/macosx/classes/sun/font/PlatformFontInfo.java create mode 100644 src/java.desktop/unix/classes/sun/font/PlatformFontInfo.java create mode 100644 src/java.desktop/windows/classes/sun/font/PlatformFontInfo.java create mode 100644 test/jdk/sun/awt/font/CheckFontManagerSystemProperty.java diff --git a/src/java.desktop/macosx/classes/sun/font/PlatformFontInfo.java b/src/java.desktop/macosx/classes/sun/font/PlatformFontInfo.java new file mode 100644 index 00000000000..b0ac181a884 --- /dev/null +++ b/src/java.desktop/macosx/classes/sun/font/PlatformFontInfo.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package sun.font; + +final class PlatformFontInfo { + + /** + * The method is only to be called via the + * {@code FontManagerFactory.getInstance()} factory method. + */ + static FontManager createFontManager() { + return new CFontManager(); + } +} diff --git a/src/java.desktop/share/classes/sun/font/FontManagerFactory.java b/src/java.desktop/share/classes/sun/font/FontManagerFactory.java index fb8aa81f994..fa634c68e0c 100644 --- a/src/java.desktop/share/classes/sun/font/FontManagerFactory.java +++ b/src/java.desktop/share/classes/sun/font/FontManagerFactory.java @@ -25,72 +25,33 @@ package sun.font; -import java.awt.AWTError; -import java.awt.Font; -import java.awt.GraphicsEnvironment; -import java.awt.Toolkit; -import java.security.AccessController; -import java.security.PrivilegedAction; - -import sun.security.action.GetPropertyAction; - - /** * Factory class used to retrieve a valid FontManager instance for the current * platform. * - * A default implementation is given for Linux, Solaris and Windows. - * You can alter the behaviour of the {@link #getInstance()} method by setting - * the {@code sun.font.fontmanager} property. For example: - * {@code sun.font.fontmanager=sun.awt.X11FontManager} + * A default implementation is given for Linux, Mac OS and Windows. */ public final class FontManagerFactory { /** Our singleton instance. */ - private static FontManager instance = null; - - private static final String DEFAULT_CLASS; - static { - if (FontUtilities.isWindows) { - DEFAULT_CLASS = "sun.awt.Win32FontManager"; - } else if (FontUtilities.isMacOSX) { - DEFAULT_CLASS = "sun.font.CFontManager"; - } else { - DEFAULT_CLASS = "sun.awt.X11FontManager"; - } - } + private static volatile FontManager instance; /** * Get a valid FontManager implementation for the current platform. * * @return a valid FontManager instance for the current platform */ - @SuppressWarnings("removal") - public static synchronized FontManager getInstance() { - - if (instance != null) { - return instance; - } - - AccessController.doPrivileged(new PrivilegedAction() { - - public Object run() { - try { - String fmClassName = - System.getProperty("sun.font.fontmanager", - DEFAULT_CLASS); - ClassLoader cl = ClassLoader.getSystemClassLoader(); - Class fmClass = Class.forName(fmClassName, true, cl); - instance = - (FontManager) fmClass.getDeclaredConstructor().newInstance(); - } catch (ReflectiveOperationException ex) { - throw new InternalError(ex); + public static FontManager getInstance() { + FontManager result = instance; + if (result == null) { + synchronized (FontManagerFactory.class) { + result = instance; + if (result == null) { + instance = result = PlatformFontInfo.createFontManager(); } - return null; } - }); - - return instance; + } + return result; } } diff --git a/src/java.desktop/unix/classes/sun/font/PlatformFontInfo.java b/src/java.desktop/unix/classes/sun/font/PlatformFontInfo.java new file mode 100644 index 00000000000..db25b731bb6 --- /dev/null +++ b/src/java.desktop/unix/classes/sun/font/PlatformFontInfo.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package sun.font; + +import sun.awt.X11FontManager; + +final class PlatformFontInfo { + + /** + * The method is only to be called via the + * {@code FontManagerFactory.getInstance()} factory method. + */ + static FontManager createFontManager() { + return new X11FontManager(); + } +} diff --git a/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c b/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c index fbd6ce9d14d..b2dedca351c 100644 --- a/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c +++ b/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c @@ -109,8 +109,6 @@ AWT_OnLoad(JavaVM *vm, void *reserved) struct utsname name; JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2); void *v; - jstring fmanager = NULL; - jstring fmProp = NULL; if (awtHandle != NULL) { /* Avoid several loading attempts */ @@ -126,29 +124,15 @@ AWT_OnLoad(JavaVM *vm, void *reserved) p = strrchr(buf, '/'); #endif /* - * The code below is responsible for: - * 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless - * 2. Set the "sun.font.fontmanager" system property. + * The code below is responsible for + * loading appropriate awt library, i.e. libawt_xawt or libawt_headless */ - fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager"); - CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property"); - #ifdef MACOSX - fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager"); tk = LWAWT_PATH; #else - fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager"); tk = XAWT_PATH; #endif - CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name"); - - if (fmanager && fmProp) { - JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", - "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", - fmProp, fmanager); - CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties"); - } #ifndef MACOSX if (AWTIsHeadless()) { @@ -161,14 +145,6 @@ AWT_OnLoad(JavaVM *vm, void *reserved) strncpy(p, tk, MAXPATHLEN-len-1); #endif - if (fmProp) { - (*env)->DeleteLocalRef(env, fmProp); - } - if (fmanager) { - (*env)->DeleteLocalRef(env, fmanager); - } - - #ifndef STATIC_BUILD jstring jbuf = JNU_NewStringPlatform(env, buf); CHECK_EXCEPTION_FATAL(env, "Could not allocate library name"); diff --git a/src/java.desktop/windows/classes/sun/font/PlatformFontInfo.java b/src/java.desktop/windows/classes/sun/font/PlatformFontInfo.java new file mode 100644 index 00000000000..ca3a21a2137 --- /dev/null +++ b/src/java.desktop/windows/classes/sun/font/PlatformFontInfo.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package sun.font; + +import sun.awt.Win32FontManager; + +final class PlatformFontInfo { + + /** + * The method is only to be called via the + * {@code FontManagerFactory.getInstance()} factory method. + */ + static FontManager createFontManager() { + return new Win32FontManager(); + } +} diff --git a/test/jdk/sun/awt/font/CheckFontManagerSystemProperty.java b/test/jdk/sun/awt/font/CheckFontManagerSystemProperty.java new file mode 100644 index 00000000000..197c5784c5d --- /dev/null +++ b/test/jdk/sun/awt/font/CheckFontManagerSystemProperty.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021, 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. + */ + +import java.awt.Toolkit; + +/** + * @test + * @bug 8273581 + * @summary verify the "sun.font.fontmanager" system property is not set + * @run main/othervm -Djava.awt.headless=true CheckFontManagerSystemProperty + */ + +public class CheckFontManagerSystemProperty { + + public static void main(String[] args) { + // force AWT library loading + Toolkit toolkit = Toolkit.getDefaultToolkit(); + if (toolkit == null) { + throw new RuntimeException("Toolkit not found!"); + } + String tkProp = System.getProperty("sun.font.fontmanager"); + if (tkProp != null) { + throw new RuntimeException("tkProp = " + tkProp); + } + } +}