8344252: SM cleanup in java.util classes

Reviewed-by: naoto, smarks
This commit is contained in:
Roger Riggs 2024-11-15 20:29:14 +00:00
parent 59ffac84d3
commit c5b6ed8ca0
13 changed files with 160 additions and 330 deletions

View file

@ -42,7 +42,6 @@ package java.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
@ -53,10 +52,6 @@ import java.lang.reflect.Modifier;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.jar.JarEntry;
@ -70,12 +65,9 @@ import jdk.internal.access.SharedSecrets;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.util.ReferencedKeyMap;
import sun.security.action.GetPropertyAction;
import sun.util.locale.BaseLocale;
import sun.util.resources.Bundles;
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
/**
*
@ -581,10 +573,8 @@ public abstract class ResourceBundle {
return locale;
}
@SuppressWarnings("removal")
private static ClassLoader getLoader(Module module) {
PrivilegedAction<ClassLoader> pa = module::getClassLoader;
return AccessController.doPrivileged(pa);
return module.getClassLoader();
}
/**
@ -1506,15 +1496,12 @@ public abstract class ResourceBundle {
}
private static class ResourceBundleControlProviderHolder {
private static final PrivilegedAction<List<ResourceBundleControlProvider>> pa =
() -> ServiceLoader.load(ResourceBundleControlProvider.class,
ClassLoader.getSystemClassLoader()).stream()
.map(ServiceLoader.Provider::get)
.toList();
@SuppressWarnings("removal")
private static final List<ResourceBundleControlProvider> CONTROL_PROVIDERS =
AccessController.doPrivileged(pa);
ServiceLoader.load(ResourceBundleControlProvider.class,
ClassLoader.getSystemClassLoader()).stream()
.map(ServiceLoader.Provider::get)
.toList();
private static Control getControl(String baseName) {
return CONTROL_PROVIDERS.isEmpty() ?
@ -1594,13 +1581,6 @@ public abstract class ResourceBundle {
Control control) {
Objects.requireNonNull(module);
Module callerModule = getCallerModule(caller);
if (callerModule != module) {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(GET_CLASSLOADER_PERMISSION);
}
}
return getBundleImpl(callerModule, module, baseName, locale, control);
}
@ -1885,7 +1865,6 @@ public abstract class ResourceBundle {
* Returns the service type of the given baseName that is visible
* to the given class loader
*/
@SuppressWarnings("removal")
private static Class<ResourceBundleProvider>
getResourceBundleProviderType(String baseName, ClassLoader loader)
{
@ -1900,27 +1879,20 @@ public abstract class ResourceBundle {
// Use the class loader of the getBundle caller so that the caller's
// visibility of the provider type is checked.
return AccessController.doPrivileged(
new PrivilegedAction<>() {
@Override
public Class<ResourceBundleProvider> run() {
try {
Class<?> c = Class.forName(providerName, false, loader);
if (ResourceBundleProvider.class.isAssignableFrom(c)) {
@SuppressWarnings("unchecked")
Class<ResourceBundleProvider> s = (Class<ResourceBundleProvider>) c;
return s;
}
} catch (ClassNotFoundException e) {}
return null;
}
});
try {
Class<?> c = Class.forName(providerName, false, loader);
if (ResourceBundleProvider.class.isAssignableFrom(c)) {
@SuppressWarnings("unchecked")
Class<ResourceBundleProvider> s = (Class<ResourceBundleProvider>) c;
return s;
}
} catch (ClassNotFoundException _) {}
return null;
}
/**
* Loads ResourceBundle from service providers.
*/
@SuppressWarnings("removal")
private static ResourceBundle loadBundleFromProviders(String baseName,
Locale locale,
ServiceLoader<ResourceBundleProvider> providers,
@ -1928,34 +1900,28 @@ public abstract class ResourceBundle {
{
if (providers == null) return null;
return AccessController.doPrivileged(
new PrivilegedAction<>() {
public ResourceBundle run() {
for (Iterator<ResourceBundleProvider> itr = providers.iterator(); itr.hasNext(); ) {
try {
ResourceBundleProvider provider = itr.next();
if (cacheKey != null && cacheKey.callerHasProvider == null
&& cacheKey.getModule() == provider.getClass().getModule()) {
cacheKey.callerHasProvider = Boolean.TRUE;
}
ResourceBundle bundle = provider.getBundle(baseName, locale);
trace("provider %s %s locale: %s bundle: %s%n", provider, baseName, locale, bundle);
if (bundle != null) {
return bundle;
}
} catch (ServiceConfigurationError | SecurityException e) {
if (cacheKey != null) {
cacheKey.setCause(e);
}
}
}
if (cacheKey != null && cacheKey.callerHasProvider == null) {
cacheKey.callerHasProvider = Boolean.FALSE;
}
return null;
}
});
for (Iterator<ResourceBundleProvider> itr = providers.iterator(); itr.hasNext(); ) {
try {
ResourceBundleProvider provider = itr.next();
if (cacheKey != null && cacheKey.callerHasProvider == null
&& cacheKey.getModule() == provider.getClass().getModule()) {
cacheKey.callerHasProvider = Boolean.TRUE;
}
ResourceBundle bundle = provider.getBundle(baseName, locale);
trace("provider %s %s locale: %s bundle: %s%n", provider, baseName, locale, bundle);
if (bundle != null) {
return bundle;
}
} catch (ServiceConfigurationError e) {
if (cacheKey != null) {
cacheKey.setCause(e);
}
}
}
if (cacheKey != null && cacheKey.callerHasProvider == null) {
cacheKey.callerHasProvider = Boolean.FALSE;
}
return null;
}
/*
@ -3153,7 +3119,6 @@ public abstract class ResourceBundle {
return bundle;
}
@SuppressWarnings("removal")
private ResourceBundle newBundle0(String bundleName, String format,
ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
@ -3177,28 +3142,20 @@ public abstract class ResourceBundle {
bundleClass.getName() + " in " + m.toString());
}
try {
Constructor<ResourceBundle> ctor = AccessController.doPrivileged(
new PrivilegedExceptionAction<>() {
@Override
public Constructor<ResourceBundle> run() throws NoSuchMethodException {
return bundleClass.getDeclaredConstructor();
}
});
Constructor<ResourceBundle> ctor = bundleClass.getDeclaredConstructor();
if (!Modifier.isPublic(ctor.getModifiers())) {
throw new IllegalAccessException("no-arg constructor in " +
bundleClass.getName() + " is not publicly accessible.");
}
// java.base may not be able to read the bundleClass's module.
PrivilegedAction<Void> pa1 = () -> { ctor.setAccessible(true); return null; };
AccessController.doPrivileged(pa1);
ctor.setAccessible(true);
bundle = ctor.newInstance((Object[]) null);
} catch (InvocationTargetException e) {
uncheckedThrow(e);
} catch (PrivilegedActionException e) {
assert e.getCause() instanceof NoSuchMethodException;
} catch (NoSuchMethodException e) {
throw new InstantiationException("public no-arg constructor " +
"does not exist in " + bundleClass.getName());
"does not exist in " + bundleClass.getName());
}
} else {
throw new ClassCastException(c.getName()
@ -3212,27 +3169,16 @@ public abstract class ResourceBundle {
return bundle;
}
final boolean reloadFlag = reload;
InputStream stream = null;
try {
stream = AccessController.doPrivileged(
new PrivilegedExceptionAction<>() {
public InputStream run() throws IOException {
URL url = loader.getResource(resourceName);
if (url == null) return null;
URL url = loader.getResource(resourceName);
if (url == null) return null;
URLConnection connection = url.openConnection();
if (reloadFlag) {
// Disable caches to get fresh data for
// reloading.
connection.setUseCaches(false);
}
return connection.getInputStream();
}
});
} catch (PrivilegedActionException e) {
throw (IOException) e.getCause();
URLConnection connection = url.openConnection();
if (reload) {
// Disable caches to get fresh data for
// reloading.
connection.setUseCaches(false);
}
InputStream stream = connection.getInputStream();
if (stream != null) {
try {
bundle = new PropertyResourceBundle(stream);
@ -3563,7 +3509,6 @@ public abstract class ResourceBundle {
/**
* Returns a new ResourceBundle instance of the given bundleClass
*/
@SuppressWarnings("removal")
static ResourceBundle newResourceBundle(Class<? extends ResourceBundle> bundleClass) {
try {
@SuppressWarnings("unchecked")
@ -3573,8 +3518,7 @@ public abstract class ResourceBundle {
return null;
}
// java.base may not be able to read the bundleClass's module.
PrivilegedAction<Void> pa = () -> { ctor.setAccessible(true); return null;};
AccessController.doPrivileged(pa);
ctor.setAccessible(true);
try {
return ctor.newInstance((Object[]) null);
} catch (InvocationTargetException e) {
@ -3602,9 +3546,7 @@ public abstract class ResourceBundle {
{
String bundleName = Control.INSTANCE.toBundleName(baseName, locale);
try {
PrivilegedAction<Class<?>> pa = () -> Class.forName(module, bundleName);
@SuppressWarnings("removal")
Class<?> c = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION);
Class<?> c = Class.forName(module, bundleName);
trace("local in %s %s caller %s: %s%n", module, bundleName, callerModule, c);
if (c == null) {
@ -3662,56 +3604,46 @@ public abstract class ResourceBundle {
{
String bundleName = Control.INSTANCE.toBundleName(baseName, locale);
PrivilegedAction<InputStream> pa = () -> {
try {
String resourceName = Control.INSTANCE
.toResourceName0(bundleName, "properties");
if (resourceName == null) {
return null;
}
trace("local in %s %s caller %s%n", module, resourceName, callerModule);
String resourceName = Control.INSTANCE
.toResourceName0(bundleName, "properties");
if (resourceName == null) {
return null;
}
trace("local in %s %s caller %s%n", module, resourceName, callerModule);
// if the package is in the given module but not opened
// locate it from the given module first.
String pn = toPackageName(bundleName);
trace(" %s/%s is accessible to %s : %s%n",
module.getName(), pn, callerModule,
isAccessible(callerModule, module, pn));
if (isAccessible(callerModule, module, pn)) {
InputStream in = module.getResourceAsStream(resourceName);
if (in != null) {
return in;
}
}
ClassLoader loader = module.getClassLoader();
trace("loader for %s %s caller %s%n", module, resourceName, callerModule);
try {
if (loader != null) {
return loader.getResourceAsStream(resourceName);
} else {
URL url = BootLoader.findResource(resourceName);
if (url != null) {
return url.openStream();
}
}
} catch (Exception e) {}
return null;
} catch (IOException e) {
throw new UncheckedIOException(e);
// if the package is in the given module but not opened
// locate it from the given module first.
String pn = toPackageName(bundleName);
trace(" %s/%s is accessible to %s : %s%n",
module.getName(), pn, callerModule,
isAccessible(callerModule, module, pn));
if (isAccessible(callerModule, module, pn)) {
InputStream in = module.getResourceAsStream(resourceName);
if (in != null) {
return new PropertyResourceBundle(in);
}
};
}
ClassLoader loader = module.getClassLoader();
trace("loader for %s %s caller %s%n", module, resourceName, callerModule);
try (@SuppressWarnings("removal") InputStream stream = AccessController.doPrivileged(pa)) {
try {
InputStream stream = null;
if (loader != null) {
stream = loader.getResourceAsStream(resourceName);
} else {
URL url = BootLoader.findResource(resourceName);
if (url != null) {
stream = url.openStream();
}
}
if (stream != null) {
return new PropertyResourceBundle(stream);
} else {
return null;
}
} catch (UncheckedIOException e) {
throw e.getCause();
} catch (Exception e) {
return null;
}
}
@ -3722,8 +3654,8 @@ public abstract class ResourceBundle {
}
private static final boolean TRACE_ON = Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("resource.bundle.debug", "false"));
private static final boolean TRACE_ON = Boolean.getBoolean(
System.getProperty("resource.bundle.debug", "false"));
private static void trace(String format, Object... params) {
if (TRACE_ON)