Skip to content

Commit

Permalink
8344059: Remove doPrivileged calls from windows platform sources in t…
Browse files Browse the repository at this point in the history
…he java.desktop module

Reviewed-by: kcr, prr
  • Loading branch information
prsadhuk committed Nov 19, 2024
1 parent 3729884 commit 9e92a9e
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

package com.sun.java.swing.plaf.windows;

import java.security.AccessController;
import sun.security.action.GetBooleanAction;

import java.util.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
Expand Down Expand Up @@ -67,9 +64,8 @@
*/
class AnimationController implements ActionListener, PropertyChangeListener {

@SuppressWarnings("removal")
private static final boolean VISTA_ANIMATION_DISABLED =
AccessController.doPrivileged(new GetBooleanAction("swing.disablevistaanimation"));
Boolean.getBoolean("swing.disablevistaanimation");


private static final Object ANIMATION_CONTROLLER_KEY =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.security.AccessController;

import javax.swing.AbstractAction;
import javax.swing.Action;
Expand Down Expand Up @@ -90,7 +89,6 @@
import sun.awt.SunToolkit;
import sun.awt.shell.ShellFolder;
import sun.font.FontUtilities;
import sun.security.action.GetPropertyAction;
import sun.swing.DefaultLayoutStyle;
import sun.swing.ImageIconUIResource;
import sun.swing.MnemonicHandler;
Expand Down Expand Up @@ -184,9 +182,7 @@ public void initialize() {
// performance and compatibility issues, so allow this feature
// to be switched off either at runtime or programmatically
//
@SuppressWarnings("removal")
String systemFonts = java.security.AccessController.doPrivileged(
new GetPropertyAction("swing.useSystemFontSettings"));
String systemFonts = System.getProperty("swing.useSystemFontSettings");
useSystemFontSettings = systemFonts == null || Boolean.parseBoolean(systemFonts);

if (useSystemFontSettings) {
Expand Down Expand Up @@ -596,8 +592,7 @@ protected void initComponentDefaults(UIDefaults table)
if (!(this instanceof WindowsClassicLookAndFeel) &&
(OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0)) {
@SuppressWarnings("removal")
String prop = AccessController.doPrivileged(new GetPropertyAction("swing.noxp"));
String prop = System.getProperty("swing.noxp");
if (prop == null) {

// These desktop properties are not used directly, but are needed to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.security.AccessController;
import java.util.HashMap;

import javax.swing.AbstractButton;
Expand All @@ -79,7 +78,6 @@

import sun.awt.image.SunWritableRaster;
import sun.awt.windows.ThemeReader;
import sun.security.action.GetPropertyAction;
import sun.swing.CachedPainter;

import static com.sun.java.swing.plaf.windows.TMSchema.Part;
Expand Down Expand Up @@ -124,7 +122,6 @@ static synchronized void invalidateStyle() {
* @return the singleton instance of this class or null if XP styles
* are not active or if this is not Windows XP
*/
@SuppressWarnings("removal")
static synchronized XPStyle getXP() {
if (themeActive == null) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Expand All @@ -134,9 +131,8 @@ static synchronized XPStyle getXP() {
themeActive = Boolean.FALSE;
}
if (themeActive.booleanValue()) {
GetPropertyAction propertyAction =
new GetPropertyAction("swing.noxp");
if (AccessController.doPrivileged(propertyAction) == null &&
String propertyAction = System.getProperty("swing.noxp");
if (propertyAction == null &&
ThemeReader.isThemed() &&
!(UIManager.getLookAndFeel()
instanceof WindowsClassicLookAndFeel)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ public class PlatformGraphicsInfo {
hasDisplays = hasDisplays0();
}

@SuppressWarnings({"removal", "restricted"})
@SuppressWarnings("restricted")
private static void loadAWTLibrary() {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("awt");
return null;
}
});
System.loadLibrary("awt");
}

private static native boolean hasDisplays0();
Expand Down
98 changes: 37 additions & 61 deletions src/java.desktop/windows/classes/sun/awt/Win32FontManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.function.Supplier;

import sun.awt.windows.WFontConfiguration;
import sun.font.FontManager;
Expand All @@ -47,24 +46,21 @@
*/
public final class Win32FontManager extends SunFontManager {

@SuppressWarnings("removal")
private static final TrueTypeFont eudcFont =
AccessController.doPrivileged(new PrivilegedAction<TrueTypeFont>() {
public TrueTypeFont run() {
String eudcFile = getEUDCFontFile();
if (eudcFile != null) {
try {
/* Must use Java rasteriser since GDI doesn't
* enumerate (allow direct use) of EUDC fonts.
*/
return new TrueTypeFont(eudcFile, null, 0,
true, false);
} catch (FontFormatException e) {
}
((Supplier<TrueTypeFont>) () -> {
String eudcFile = getEUDCFontFile();
if (eudcFile != null) {
try {
/* Must use Java rasteriser since GDI doesn't
* enumerate (allow direct use) of EUDC fonts.
*/
return new TrueTypeFont(eudcFile, null, 0,
true, false);
} catch (FontFormatException e) {
}
return null;
}
});
return null;
}).get();

/* Used on Windows to obtain from the windows registry the name
* of a file containing the system EUFC font. If running in one of
Expand All @@ -78,20 +74,14 @@ public TrueTypeFont getEUDCFont() {
return eudcFont;
}

@SuppressWarnings("removal")
public Win32FontManager() {
super();
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {

/* Register the JRE fonts so that the native platform can
* access them. This is used only on Windows so that when
* printing the printer driver can access the fonts.
*/
registerJREFontsWithPlatform(jreFontDirName);
return null;
}
});

/* Register the JRE fonts so that the native platform can
* access them. This is used only on Windows so that when
* printing the printer driver can access the fonts.
*/
registerJREFontsWithPlatform(jreFontDirName);
}

/**
Expand Down Expand Up @@ -213,21 +203,15 @@ protected String[] getDefaultPlatformFont() {
info[1] = "c:\\windows\\fonts";
final String[] dirs = getPlatformFontDirs(true);
if (dirs.length > 1) {
@SuppressWarnings("removal")
String dir = (String)
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
for (int i=0; i<dirs.length; i++) {
String path =
dirs[i] + File.separator + "arial.ttf";
File file = new File(path);
if (file.exists()) {
return dirs[i];
}
}
return null;
}
});
String dir = null;
for (int i=0; i<dirs.length; i++) {
String path = dirs[i] + File.separator + "arial.ttf";
File file = new File(path);
if (file.exists()) {
dir = dirs[i];
break;
}
}
if (dir != null) {
info[1] = dir;
}
Expand All @@ -248,7 +232,6 @@ protected void registerJREFontsWithPlatform(String pathName) {
fontsForPrinting = pathName;
}

@SuppressWarnings("removal")
public static void registerJREFontsForPrinting() {
final String pathName;
synchronized (Win32GraphicsEnvironment.class) {
Expand All @@ -259,22 +242,15 @@ public static void registerJREFontsForPrinting() {
pathName = fontsForPrinting;
fontsForPrinting = null;
}
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Object>() {
public Object run() {
File f1 = new File(pathName);
String[] ls = f1.list(SunFontManager.getInstance().
getTrueTypeFilter());
if (ls == null) {
return null;
}
for (int i=0; i <ls.length; i++ ) {
File fontFile = new File(f1, ls[i]);
registerFontWithPlatform(fontFile.getAbsolutePath());
}
return null;
}
});
File f1 = new File(pathName);
String[] ls = f1.list(SunFontManager.getInstance().
getTrueTypeFilter());
if (ls != null) {
for (int i=0; i <ls.length; i++ ) {
File fontFile = new File(f1, ls[i]);
registerFontWithPlatform(fontFile.getAbsolutePath());
}
}
}

private static native void registerFontWithPlatform(String fontName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ public class Win32GraphicsDevice extends GraphicsDevice implements
// is run as an NT service. To prevent the loading of ddraw.dll
// completely, sun.awt.nopixfmt should be set as well. Apps which use
// OpenGL w/ Java probably don't want to set this.
@SuppressWarnings("removal")
String nopixfmt = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.awt.nopixfmt"));
String nopixfmt = System.getProperty("sun.awt.nopixfmt");
pfDisabled = (nopixfmt != null);
initIDs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -478,12 +476,7 @@ public boolean isComputerNode(final File dir) {
if (dir != null && dir == getDrives()) {
return true;
} else {
@SuppressWarnings("removal")
String path = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return dir.getAbsolutePath();
}
});
String path = dir.getAbsolutePath();

return (path.startsWith("\\\\") && path.indexOf("\\", 2) < 0); //Network path
}
Expand Down Expand Up @@ -572,25 +565,17 @@ protected Invoker createInvoker() {
private static class ComInvoker extends ThreadPoolExecutor implements ThreadFactory, ShellFolder.Invoker {
private static Thread comThread;

@SuppressWarnings("removal")
private ComInvoker() {
super(1, 1, 0, TimeUnit.DAYS, new LinkedBlockingQueue<>());
allowCoreThreadTimeOut(false);
setThreadFactory(this);
final Runnable shutdownHook = () -> AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
shutdownNow();
return null;
});
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
Thread t = new Thread(
ThreadGroupUtils.getRootThreadGroup(), shutdownHook,
"ShellFolder", 0, false);
Runtime.getRuntime().addShutdownHook(t);
return null;
});
final Runnable shutdownHook = () -> shutdownNow();
Thread t = new Thread(
ThreadGroupUtils.getRootThreadGroup(), shutdownHook,
"ShellFolder", 0, false);
Runtime.getRuntime().addShutdownHook(t);
}

@SuppressWarnings("removal")
public synchronized Thread newThread(final Runnable task) {
final Runnable comRun = new Runnable() {
public void run() {
Expand All @@ -602,27 +587,22 @@ public void run() {
}
}
};
comThread = AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
String name = "Swing-Shell";
/* The thread must be a member of a thread group
* which will not get GCed before VM exit.
* Make its parent the top-level thread group.
*/
Thread thread = new Thread(
ThreadGroupUtils.getRootThreadGroup(), comRun, name,
0, false);
thread.setDaemon(true);
/* This is important, since this thread running at lower priority
leads to memory consumption when listDrives() function is called
repeatedly.
*/
thread.setPriority(Thread.MAX_PRIORITY);
return thread;
});
/* The thread must be a member of a thread group
* which will not get GCed before VM exit.
* Make its parent the top-level thread group.
*/
comThread = new Thread(
ThreadGroupUtils.getRootThreadGroup(), comRun, "Swing-Shell",
0, false);
comThread.setDaemon(true);
/* This is important, since this thread running at lower priority
leads to memory consumption when listDrives() function is called
repeatedly.
*/
comThread.setPriority(Thread.MAX_PRIORITY);
return comThread;
}

@SuppressWarnings("removal")
public <T> T invoke(Callable<T> task) throws Exception {
if (Thread.currentThread() == comThread) {
// if it's already called from the COM
Expand All @@ -640,13 +620,8 @@ public <T> T invoke(Callable<T> task) throws Exception {
try {
return future.get();
} catch (InterruptedException e) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
future.cancel(true);
future.cancel(true);

return null;
}
});

throw e;
} catch (ExecutionException e) {
Expand Down
Loading

0 comments on commit 9e92a9e

Please sign in to comment.