Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit db7ee3d

Browse files
committed
8344223: Remove calls to SecurityManager and doPrivileged in java.net.URLClassLoader after JEP 486 integration
Reviewed-by: alanb, yzheng
1 parent 6f4dfa6 commit db7ee3d

File tree

3 files changed

+112
-397
lines changed

3 files changed

+112
-397
lines changed

src/java.base/share/classes/java/net/URLClassLoader.java

Lines changed: 31 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,10 @@
3030
import java.io.FilePermission;
3131
import java.io.IOException;
3232
import java.io.InputStream;
33-
import java.security.AccessControlContext;
34-
import java.security.AccessController;
3533
import java.security.CodeSigner;
3634
import java.security.CodeSource;
3735
import java.security.Permission;
3836
import java.security.PermissionCollection;
39-
import java.security.PrivilegedAction;
40-
import java.security.PrivilegedExceptionAction;
4137
import java.security.SecureClassLoader;
4238
import java.util.Enumeration;
4339
import java.util.List;
@@ -76,10 +72,6 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
7672
/* The search path for classes and resources */
7773
private final URLClassPath ucp;
7874

79-
/* The context to be used when loading classes and resources */
80-
@SuppressWarnings("removal")
81-
private final AccessControlContext acc;
82-
8375
/**
8476
* Constructs a new URLClassLoader for the given URLs. The URLs will be
8577
* searched in the order specified for classes and resources after first
@@ -94,18 +86,9 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
9486
* @throws NullPointerException if {@code urls} or any of its
9587
* elements is {@code null}.
9688
*/
97-
@SuppressWarnings("removal")
9889
public URLClassLoader(URL[] urls, ClassLoader parent) {
9990
super(parent);
100-
this.acc = AccessController.getContext();
101-
this.ucp = new URLClassPath(urls, acc);
102-
}
103-
104-
URLClassLoader(String name, URL[] urls, ClassLoader parent,
105-
@SuppressWarnings("removal") AccessControlContext acc) {
106-
super(name, parent);
107-
this.acc = acc;
108-
this.ucp = new URLClassPath(urls, acc);
91+
this.ucp = new URLClassPath(urls);
10992
}
11093

11194
/**
@@ -122,17 +105,9 @@ public URLClassLoader(URL[] urls, ClassLoader parent) {
122105
* @throws NullPointerException if {@code urls} or any of its
123106
* elements is {@code null}.
124107
*/
125-
@SuppressWarnings("removal")
126108
public URLClassLoader(URL[] urls) {
127109
super();
128-
this.acc = AccessController.getContext();
129-
this.ucp = new URLClassPath(urls, acc);
130-
}
131-
132-
URLClassLoader(URL[] urls, @SuppressWarnings("removal") AccessControlContext acc) {
133-
super();
134-
this.acc = acc;
135-
this.ucp = new URLClassPath(urls, acc);
110+
this.ucp = new URLClassPath(urls);
136111
}
137112

138113
/**
@@ -149,12 +124,10 @@ public URLClassLoader(URL[] urls) {
149124
* @throws NullPointerException if {@code urls} or any of its
150125
* elements is {@code null}.
151126
*/
152-
@SuppressWarnings("removal")
153127
public URLClassLoader(URL[] urls, ClassLoader parent,
154128
URLStreamHandlerFactory factory) {
155129
super(parent);
156-
this.acc = AccessController.getContext();
157-
this.ucp = new URLClassPath(urls, factory, acc);
130+
this.ucp = new URLClassPath(urls, factory);
158131
}
159132

160133

@@ -176,13 +149,11 @@ public URLClassLoader(URL[] urls, ClassLoader parent,
176149
*
177150
* @since 9
178151
*/
179-
@SuppressWarnings("removal")
180152
public URLClassLoader(String name,
181153
URL[] urls,
182154
ClassLoader parent) {
183155
super(name, parent);
184-
this.acc = AccessController.getContext();
185-
this.ucp = new URLClassPath(urls, acc);
156+
this.ucp = new URLClassPath(urls);
186157
}
187158

188159
/**
@@ -203,12 +174,10 @@ public URLClassLoader(String name,
203174
*
204175
* @since 9
205176
*/
206-
@SuppressWarnings("removal")
207177
public URLClassLoader(String name, URL[] urls, ClassLoader parent,
208178
URLStreamHandlerFactory factory) {
209179
super(name, parent);
210-
this.acc = AccessController.getContext();
211-
this.ucp = new URLClassPath(urls, factory, acc);
180+
this.ucp = new URLClassPath(urls, factory);
212181
}
213182

214183
/* A map (used as a set) to keep track of closeable local resources
@@ -299,11 +268,6 @@ public InputStream getResourceAsStream(String name) {
299268
* @since 1.7
300269
*/
301270
public void close() throws IOException {
302-
@SuppressWarnings("removal")
303-
SecurityManager security = System.getSecurityManager();
304-
if (security != null) {
305-
security.checkPermission(new RuntimePermission("closeClassLoader"));
306-
}
307271
List<IOException> errors = ucp.closeLoaders();
308272

309273
// now close any remaining streams.
@@ -369,40 +333,24 @@ public URL[] getURLs() {
369333
* or if the loader is closed.
370334
* @throws NullPointerException if {@code name} is {@code null}.
371335
*/
372-
@SuppressWarnings("removal")
373336
protected Class<?> findClass(final String name)
374337
throws ClassNotFoundException
375338
{
376-
final Class<?> result;
377-
try {
378-
result = AccessController.doPrivileged(
379-
new PrivilegedExceptionAction<>() {
380-
public Class<?> run() throws ClassNotFoundException {
381-
String path = name.replace('.', '/').concat(".class");
382-
Resource res = ucp.getResource(path, false);
383-
if (res != null) {
384-
try {
385-
return defineClass(name, res);
386-
} catch (IOException e) {
387-
throw new ClassNotFoundException(name, e);
388-
} catch (ClassFormatError e2) {
389-
if (res.getDataError() != null) {
390-
e2.addSuppressed(res.getDataError());
391-
}
392-
throw e2;
393-
}
394-
} else {
395-
return null;
396-
}
397-
}
398-
}, acc);
399-
} catch (java.security.PrivilegedActionException pae) {
400-
throw (ClassNotFoundException) pae.getException();
401-
}
402-
if (result == null) {
403-
throw new ClassNotFoundException(name);
339+
String path = name.replace('.', '/').concat(".class");
340+
Resource res = ucp.getResource(path);
341+
if (res != null) {
342+
try {
343+
return defineClass(name, res);
344+
} catch (IOException e) {
345+
throw new ClassNotFoundException(name, e);
346+
} catch (ClassFormatError e2) {
347+
if (res.getDataError() != null) {
348+
e2.addSuppressed(res.getDataError());
349+
}
350+
throw e2;
351+
}
404352
}
405-
return result;
353+
throw new ClassNotFoundException(name);
406354
}
407355

408356
/*
@@ -575,18 +523,7 @@ private boolean isSealed(String name, Manifest man) {
575523
* if the resource could not be found, or if the loader is closed.
576524
*/
577525
public URL findResource(final String name) {
578-
/*
579-
* The same restriction to finding classes applies to resources
580-
*/
581-
@SuppressWarnings("removal")
582-
URL url = AccessController.doPrivileged(
583-
new PrivilegedAction<>() {
584-
public URL run() {
585-
return ucp.findResource(name, true);
586-
}
587-
}, acc);
588-
589-
return url != null ? URLClassPath.checkURL(url) : null;
526+
return ucp.findResource(name);
590527
}
591528

592529
/**
@@ -598,10 +535,11 @@ public URL run() {
598535
* @return An {@code Enumeration} of {@code URL}s.
599536
* If the loader is closed, the Enumeration contains no elements.
600537
*/
538+
@Override
601539
public Enumeration<URL> findResources(final String name)
602540
throws IOException
603541
{
604-
final Enumeration<URL> e = ucp.findResources(name, true);
542+
final Enumeration<URL> e = ucp.findResources(name);
605543

606544
return new Enumeration<>() {
607545
private URL url = null;
@@ -610,23 +548,14 @@ private boolean next() {
610548
if (url != null) {
611549
return true;
612550
}
613-
do {
614-
@SuppressWarnings("removal")
615-
URL u = AccessController.doPrivileged(
616-
new PrivilegedAction<>() {
617-
public URL run() {
618-
if (!e.hasMoreElements())
619-
return null;
620-
return e.nextElement();
621-
}
622-
}, acc);
623-
if (u == null)
624-
break;
625-
url = URLClassPath.checkURL(u);
626-
} while (url == null);
551+
if (!e.hasMoreElements()) {
552+
return false;
553+
}
554+
url = e.nextElement();
627555
return url != null;
628556
}
629557

558+
@Override
630559
public URL nextElement() {
631560
if (!next()) {
632561
throw new NoSuchElementException();
@@ -636,6 +565,7 @@ public URL nextElement() {
636565
return u;
637566
}
638567

568+
@Override
639569
public boolean hasMoreElements() {
640570
return next();
641571
}
@@ -666,7 +596,6 @@ public boolean hasMoreElements() {
666596
* @throws NullPointerException if {@code codesource} is {@code null}.
667597
* @return the permissions for the codesource
668598
*/
669-
@SuppressWarnings("removal")
670599
protected PermissionCollection getPermissions(CodeSource codesource)
671600
{
672601
PermissionCollection perms = super.getPermissions(codesource);
@@ -712,23 +641,13 @@ protected PermissionCollection getPermissions(CodeSource codesource)
712641
String host = locUrl.getHost();
713642
if (host != null && !host.isEmpty())
714643
p = new SocketPermission(host,
715-
SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
644+
SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION);
716645
}
717646

718647
// make sure the person that created this class loader
719648
// would have this permission
720649

721650
if (p != null) {
722-
final SecurityManager sm = System.getSecurityManager();
723-
if (sm != null) {
724-
final Permission fp = p;
725-
AccessController.doPrivileged(new PrivilegedAction<>() {
726-
public Void run() throws SecurityException {
727-
sm.checkPermission(fp);
728-
return null;
729-
}
730-
}, acc);
731-
}
732651
perms.add(p);
733652
}
734653
return perms;
@@ -746,18 +665,7 @@ public Void run() throws SecurityException {
746665
*/
747666
public static URLClassLoader newInstance(final URL[] urls,
748667
final ClassLoader parent) {
749-
// Save the caller's context
750-
@SuppressWarnings("removal")
751-
final AccessControlContext acc = AccessController.getContext();
752-
// Need a privileged block to create the class loader
753-
@SuppressWarnings("removal")
754-
URLClassLoader ucl = AccessController.doPrivileged(
755-
new PrivilegedAction<>() {
756-
public URLClassLoader run() {
757-
return new FactoryURLClassLoader(null, urls, parent, acc);
758-
}
759-
});
760-
return ucl;
668+
return new URLClassLoader(null, urls, parent);
761669
}
762670

763671
/**
@@ -770,53 +678,10 @@ public URLClassLoader run() {
770678
* @return the resulting class loader
771679
*/
772680
public static URLClassLoader newInstance(final URL[] urls) {
773-
// Save the caller's context
774-
@SuppressWarnings("removal")
775-
final AccessControlContext acc = AccessController.getContext();
776-
// Need a privileged block to create the class loader
777-
@SuppressWarnings("removal")
778-
URLClassLoader ucl = AccessController.doPrivileged(
779-
new PrivilegedAction<>() {
780-
public URLClassLoader run() {
781-
return new FactoryURLClassLoader(urls, acc);
782-
}
783-
});
784-
return ucl;
681+
return new URLClassLoader(urls);
785682
}
786683

787684
static {
788685
ClassLoader.registerAsParallelCapable();
789686
}
790687
}
791-
792-
final class FactoryURLClassLoader extends URLClassLoader {
793-
794-
static {
795-
ClassLoader.registerAsParallelCapable();
796-
}
797-
798-
FactoryURLClassLoader(String name, URL[] urls, ClassLoader parent,
799-
@SuppressWarnings("removal") AccessControlContext acc) {
800-
super(name, urls, parent, acc);
801-
}
802-
803-
FactoryURLClassLoader(URL[] urls, @SuppressWarnings("removal") AccessControlContext acc) {
804-
super(urls, acc);
805-
}
806-
807-
public final Class<?> loadClass(String name, boolean resolve)
808-
throws ClassNotFoundException
809-
{
810-
// First check if we have permission to access the package. This
811-
// should go away once we've added support for exported packages.
812-
@SuppressWarnings("removal")
813-
SecurityManager sm = System.getSecurityManager();
814-
if (sm != null) {
815-
int i = name.lastIndexOf('.');
816-
if (i != -1) {
817-
sm.checkPackageAccess(name.substring(0, i));
818-
}
819-
}
820-
return super.loadClass(name, resolve);
821-
}
822-
}

0 commit comments

Comments
 (0)