jdk/src/java.base/share/classes/java/lang/ClassLoader.java
changeset 42338 a60f280f803c
parent 41911 b3bb62588635
child 42339 455e651aa073
equal deleted inserted replaced
42148:7a4a59859ac0 42338:a60f280f803c
    40 import java.security.ProtectionDomain;
    40 import java.security.ProtectionDomain;
    41 import java.security.cert.Certificate;
    41 import java.security.cert.Certificate;
    42 import java.util.Collections;
    42 import java.util.Collections;
    43 import java.util.Enumeration;
    43 import java.util.Enumeration;
    44 import java.util.HashMap;
    44 import java.util.HashMap;
    45 import java.util.HashSet;
       
    46 import java.util.Hashtable;
    45 import java.util.Hashtable;
    47 import java.util.Map;
    46 import java.util.Map;
       
    47 import java.util.NoSuchElementException;
    48 import java.util.Objects;
    48 import java.util.Objects;
    49 import java.util.Set;
    49 import java.util.Set;
    50 import java.util.Spliterator;
    50 import java.util.Spliterator;
    51 import java.util.Spliterators;
    51 import java.util.Spliterators;
    52 import java.util.Stack;
    52 import java.util.Stack;
    53 import java.util.NoSuchElementException;
       
    54 import java.util.Vector;
    53 import java.util.Vector;
    55 import java.util.WeakHashMap;
    54 import java.util.WeakHashMap;
    56 import java.util.concurrent.ConcurrentHashMap;
    55 import java.util.concurrent.ConcurrentHashMap;
    57 import java.util.function.Supplier;
    56 import java.util.function.Supplier;
    58 import java.util.stream.Stream;
    57 import java.util.stream.Stream;
    59 import java.util.stream.StreamSupport;
    58 import java.util.stream.StreamSupport;
    60 
    59 
    61 import jdk.internal.perf.PerfCounter;
    60 import jdk.internal.perf.PerfCounter;
    62 import jdk.internal.module.ServicesCatalog;
       
    63 import jdk.internal.loader.BootLoader;
    61 import jdk.internal.loader.BootLoader;
    64 import jdk.internal.loader.ClassLoaders;
    62 import jdk.internal.loader.ClassLoaders;
    65 import jdk.internal.misc.SharedSecrets;
    63 import jdk.internal.misc.SharedSecrets;
    66 import jdk.internal.misc.Unsafe;
    64 import jdk.internal.misc.Unsafe;
    67 import jdk.internal.misc.VM;
    65 import jdk.internal.misc.VM;
   409      */
   407      */
   410     protected ClassLoader(ClassLoader parent) {
   408     protected ClassLoader(ClassLoader parent) {
   411         this(checkCreateClassLoader(), null, parent);
   409         this(checkCreateClassLoader(), null, parent);
   412     }
   410     }
   413 
   411 
   414 
       
   415     /**
   412     /**
   416      * Creates a new class loader using the <tt>ClassLoader</tt> returned by
   413      * Creates a new class loader using the <tt>ClassLoader</tt> returned by
   417      * the method {@link #getSystemClassLoader()
   414      * the method {@link #getSystemClassLoader()
   418      * <tt>getSystemClassLoader()</tt>} as the parent class loader.
   415      * <tt>getSystemClassLoader()</tt>} as the parent class loader.
   419      *
   416      *
   428      *          of a new class loader.
   425      *          of a new class loader.
   429      */
   426      */
   430     protected ClassLoader() {
   427     protected ClassLoader() {
   431         this(checkCreateClassLoader(), null, getSystemClassLoader());
   428         this(checkCreateClassLoader(), null, getSystemClassLoader());
   432     }
   429     }
   433 
       
   434 
   430 
   435     /**
   431     /**
   436      * Returns the name of this class loader or {@code null} if
   432      * Returns the name of this class loader or {@code null} if
   437      * this class loader is not named.
   433      * this class loader is not named.
   438      *
   434      *
   579      *         The <a href="#name">binary name</a> of the class
   575      *         The <a href="#name">binary name</a> of the class
   580      *
   576      *
   581      * @return The resulting {@code Class} object in a module defined by
   577      * @return The resulting {@code Class} object in a module defined by
   582      *         this class loader, or {@code null} if the class could not be found.
   578      *         this class loader, or {@code null} if the class could not be found.
   583      */
   579      */
   584     final Class<?> loadLocalClass(Module module, String name) {
   580     final Class<?> loadClass(Module module, String name) {
   585         synchronized (getClassLoadingLock(name)) {
   581         synchronized (getClassLoadingLock(name)) {
   586             // First, check if the class has already been loaded
   582             // First, check if the class has already been loaded
   587             Class<?> c = findLoadedClass(name);
   583             Class<?> c = findLoadedClass(name);
   588             if (c == null) {
   584             if (c == null) {
   589                 c = findClass(module.getName(), name);
   585                 c = findClass(module.getName(), name);
   591             if (c != null && c.getModule() == module) {
   587             if (c != null && c.getModule() == module) {
   592                 return c;
   588                 return c;
   593             } else {
   589             } else {
   594                 return null;
   590                 return null;
   595             }
   591             }
   596         }
       
   597     }
       
   598 
       
   599     /**
       
   600      * Loads the class with the specified <a href="#name">binary name</a>
       
   601      * defined by this class loader.  This method returns {@code null}
       
   602      * if the class could not be found.
       
   603      *
       
   604      * @apiNote This method does not delegate to the parent class loader.
       
   605      *
       
   606      * @param  name
       
   607      *         The <a href="#name">binary name</a> of the class
       
   608      *
       
   609      * @return The resulting {@code Class} object in a module defined by
       
   610      *         this class loader, or {@code null} if the class could not be found.
       
   611      */
       
   612     final Class<?> loadLocalClass(String name) {
       
   613         synchronized (getClassLoadingLock(name)) {
       
   614             // First, check if the class has already been loaded
       
   615             Class<?> c = findLoadedClass(name);
       
   616             if (c == null) {
       
   617                 try {
       
   618                     return findClass(name);
       
   619                 } catch (ClassNotFoundException e) {
       
   620                     // ignore
       
   621                 }
       
   622             }
       
   623             return c;
       
   624         }
   592         }
   625     }
   593     }
   626 
   594 
   627     /**
   595     /**
   628      * Returns the lock object for class loading operations.
   596      * Returns the lock object for class loading operations.
   722      * in a module defined to this class loader.
   690      * in a module defined to this class loader.
   723      * Class loader implementations that support the loading from modules
   691      * Class loader implementations that support the loading from modules
   724      * should override this method.
   692      * should override this method.
   725      *
   693      *
   726      * @apiNote This method returns {@code null} rather than throwing
   694      * @apiNote This method returns {@code null} rather than throwing
   727      *          {@code ClassNotFoundException} if the class could not be found
   695      *          {@code ClassNotFoundException} if the class could not be found.
   728      *
   696      *
   729      * @implSpec The default implementation returns {@code null}.
   697      * @implSpec The default implementation attempts to find the class by
       
   698      * invoking {@link #findClass(String)} when the {@code moduleName} is
       
   699      * {@code null}. It otherwise returns {@code null}.
   730      *
   700      *
   731      * @param  moduleName
   701      * @param  moduleName
   732      *         The module name
   702      *         The module name; or {@code null} to find the class in the
       
   703      *         {@linkplain #getUnnamedModule() unnamed module} for this
       
   704      *         class loader
       
   705 
   733      * @param  name
   706      * @param  name
   734      *         The <a href="#name">binary name</a> of the class
   707      *         The <a href="#name">binary name</a> of the class
   735      *
   708      *
   736      * @return The resulting {@code Class} object, or {@code null}
   709      * @return The resulting {@code Class} object, or {@code null}
   737      *         if the class could not be found.
   710      *         if the class could not be found.
   738      *
   711      *
   739      * @since 9
   712      * @since 9
   740      */
   713      */
   741     protected Class<?> findClass(String moduleName, String name) {
   714     protected Class<?> findClass(String moduleName, String name) {
       
   715         if (moduleName == null) {
       
   716             try {
       
   717                 return findClass(name);
       
   718             } catch (ClassNotFoundException ignore) { }
       
   719         }
   742         return null;
   720         return null;
   743     }
   721     }
   744 
   722 
   745 
   723 
   746     /**
   724     /**
  1284     /**
  1262     /**
  1285      * Returns a URL to a resource in a module defined to this class loader.
  1263      * Returns a URL to a resource in a module defined to this class loader.
  1286      * Class loader implementations that support the loading from modules
  1264      * Class loader implementations that support the loading from modules
  1287      * should override this method.
  1265      * should override this method.
  1288      *
  1266      *
  1289      * @implSpec The default implementation returns {@code null}.
  1267      * @apiNote This method is the basis for the {@code Class} {@link
       
  1268      * Class#getResource getResource} and {@link Class#getResourceAsStream
       
  1269      * getResourceAsStream} methods. It is not subject to the rules for
       
  1270      * encapsulation specified by {@code Module} {@link
       
  1271      * Module#getResourceAsStream getResourceAsStream}.
       
  1272      *
       
  1273      * @implSpec The default implementation attempts to find the resource by
       
  1274      * invoking {@link #findResource(String)} when the {@code moduleName} is
       
  1275      * {@code null}. It otherwise returns {@code null}.
  1290      *
  1276      *
  1291      * @param  moduleName
  1277      * @param  moduleName
  1292      *         The module name
  1278      *         The module name; or {@code null} to find a resource in the
       
  1279      *         {@linkplain #getUnnamedModule() unnamed module} for this
       
  1280      *         class loader
  1293      * @param  name
  1281      * @param  name
  1294      *         The resource name
  1282      *         The resource name
  1295      *
  1283      *
  1296      * @return A URL to the resource; {@code null} if the resource could not be
  1284      * @return A URL to the resource; {@code null} if the resource could not be
  1297      *         found, a URL could not be constructed to locate the resource,
  1285      *         found, a URL could not be constructed to locate the resource,
  1304      *
  1292      *
  1305      * @see java.lang.module.ModuleReader#find(String)
  1293      * @see java.lang.module.ModuleReader#find(String)
  1306      * @since 9
  1294      * @since 9
  1307      */
  1295      */
  1308     protected URL findResource(String moduleName, String name) throws IOException {
  1296     protected URL findResource(String moduleName, String name) throws IOException {
  1309         return null;
  1297         if (moduleName == null) {
       
  1298             return findResource(name);
       
  1299         } else {
       
  1300             return null;
       
  1301         }
  1310     }
  1302     }
  1311 
  1303 
  1312     /**
  1304     /**
  1313      * Finds the resource with the given name.  A resource is some data
  1305      * Finds the resource with the given name.  A resource is some data
  1314      * (images, audio, text, etc) that can be accessed by class code in a way
  1306      * (images, audio, text, etc) that can be accessed by class code in a way
  1315      * that is independent of the location of the code.
  1307      * that is independent of the location of the code.
  1316      *
       
  1317      * Resources in a named module are private to that module. This method does
       
  1318      * not find resource in named modules.
       
  1319      *
  1308      *
  1320      * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
  1309      * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
  1321      * identifies the resource.
  1310      * identifies the resource.
  1322      *
  1311      *
  1323      * <p> This method will first search the parent class loader for the
  1312      * <p> This method will first search the parent class loader for the
  1324      * resource; if the parent is <tt>null</tt> the path of the class loader
  1313      * resource; if the parent is <tt>null</tt> the path of the class loader
  1325      * built-in to the virtual machine is searched.  That failing, this method
  1314      * built-in to the virtual machine is searched.  That failing, this method
  1326      * will invoke {@link #findResource(String)} to find the resource.  </p>
  1315      * will invoke {@link #findResource(String)} to find the resource.  </p>
  1327      *
  1316      *
  1328      * @apiNote When overriding this method it is recommended that an
  1317      * <p> Resources in named modules are subject to the encapsulation rules
  1329      * implementation ensures that any delegation is consistent with the {@link
  1318      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1319      * Additionally, and except for the special case where the resource has a
       
  1320      * name ending with "{@code .class}", this method will only find resources in
       
  1321      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1322      * opened} unconditionally (even if the caller of this method is in the
       
  1323      * same module as the resource). </p>
       
  1324      *
       
  1325      * @apiNote Where several modules are defined to the same class loader,
       
  1326      * and where more than one module contains a resource with the given name,
       
  1327      * then the ordering that modules are searched is not specified and may be
       
  1328      * very unpredictable.
       
  1329      * When overriding this method it is recommended that an implementation
       
  1330      * ensures that any delegation is consistent with the {@link
  1330      * #getResources(java.lang.String) getResources(String)} method.
  1331      * #getResources(java.lang.String) getResources(String)} method.
  1331      *
  1332      *
  1332      * @param  name
  1333      * @param  name
  1333      *         The resource name
  1334      *         The resource name
  1334      *
  1335      *
  1335      * @return  A <tt>URL</tt> object for reading the resource, or
  1336      * @return  {@code URL} object for reading the resource; {@code null} if
  1336      *          <tt>null</tt> if the resource could not be found or the invoker
  1337      *          the resource could not be found, a {@code URL} could not be
  1337      *          doesn't have adequate  privileges to get the resource.
  1338      *          constructed to locate the resource, the resource is in a package
       
  1339      *          that is not opened unconditionally, or access to the resource is
       
  1340      *          denied by the security manager.
  1338      *
  1341      *
  1339      * @since  1.1
  1342      * @since  1.1
  1340      */
  1343      */
  1341     public URL getResource(String name) {
  1344     public URL getResource(String name) {
  1342         URL url;
  1345         URL url;
  1354     /**
  1357     /**
  1355      * Finds all the resources with the given name. A resource is some data
  1358      * Finds all the resources with the given name. A resource is some data
  1356      * (images, audio, text, etc) that can be accessed by class code in a way
  1359      * (images, audio, text, etc) that can be accessed by class code in a way
  1357      * that is independent of the location of the code.
  1360      * that is independent of the location of the code.
  1358      *
  1361      *
  1359      * Resources in a named module are private to that module. This method does
  1362      * <p> The name of a resource is a <tt>/</tt>-separated path name that
  1360      * not find resources in named modules.
       
  1361      *
       
  1362      * <p>The name of a resource is a <tt>/</tt>-separated path name that
       
  1363      * identifies the resource.
  1363      * identifies the resource.
  1364      *
  1364      *
  1365      * <p> The search order is described in the documentation for {@link
  1365      * <p> The delegation order for searching is described in the documentation
  1366      * #getResource(String)}.  </p>
  1366      * for {@link #getResource(String)}.  </p>
  1367      *
  1367      *
  1368      * @apiNote When overriding this method it is recommended that an
  1368      * <p> Resources in named modules are subject to the encapsulation rules
       
  1369      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1370      * Additionally, and except for the special case where the resource has a
       
  1371      * name ending with "{@code .class}", this method will only find resources in
       
  1372      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1373      * opened} unconditionally (even if the caller of this method is in the
       
  1374      * same module as the resource).</p>
       
  1375      *
       
  1376      * @apiNote Where several modules are defined to the same class loader,
       
  1377      * and where more than one module contains a resource with the given name,
       
  1378      * then the ordering is not specified and may be very unpredictable.
       
  1379      * When overriding this method it is recommended that an
  1369      * implementation ensures that any delegation is consistent with the {@link
  1380      * implementation ensures that any delegation is consistent with the {@link
  1370      * #getResource(java.lang.String) getResource(String)} method. This should
  1381      * #getResource(java.lang.String) getResource(String)} method. This should
  1371      * ensure that the first element returned by the Enumeration's
  1382      * ensure that the first element returned by the Enumeration's
  1372      * {@code nextElement} method is the same resource that the
  1383      * {@code nextElement} method is the same resource that the
  1373      * {@code getResource(String)} method would return.
  1384      * {@code getResource(String)} method would return.
  1374      *
  1385      *
  1375      * @param  name
  1386      * @param  name
  1376      *         The resource name
  1387      *         The resource name
  1377      *
  1388      *
  1378      * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  1389      * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  1379      *          the resource.  If no resources could  be found, the enumeration
  1390      *          the resource. If no resources could  be found, the enumeration
  1380      *          will be empty.  Resources that the class loader doesn't have
  1391      *          will be empty. Resources for which a {@code URL} cannot be
  1381      *          access to will not be in the enumeration.
  1392      *          constructed, are in package that is not opened unconditionally,
       
  1393      *          or access to the resource is denied by the security manager,
       
  1394      *          are not returned in the enumeration.
  1382      *
  1395      *
  1383      * @throws  IOException
  1396      * @throws  IOException
  1384      *          If I/O errors occur
  1397      *          If I/O errors occur
  1385      *
  1398      *
  1386      * @see  #findResources(String)
  1399      * @see  #findResources(String)
  1404      * Returns a stream whose elements are the URLs of all the resources with
  1417      * Returns a stream whose elements are the URLs of all the resources with
  1405      * the given name. A resource is some data (images, audio, text, etc) that
  1418      * the given name. A resource is some data (images, audio, text, etc) that
  1406      * can be accessed by class code in a way that is independent of the
  1419      * can be accessed by class code in a way that is independent of the
  1407      * location of the code.
  1420      * location of the code.
  1408      *
  1421      *
  1409      * Resources in a named module are private to that module. This method does
       
  1410      * not find resources in named modules.
       
  1411      *
       
  1412      * <p> The name of a resource is a {@code /}-separated path name that
  1422      * <p> The name of a resource is a {@code /}-separated path name that
  1413      * identifies the resource.
  1423      * identifies the resource.
  1414      *
  1424      *
  1415      * <p> The search order is described in the documentation for {@link
  1425      * <p> The search order is described in the documentation for {@link
  1416      * #getResource(String)}.
  1426      * #getResource(String)}.
  1417      *
  1427      *
  1418      * <p> The resources will be located when the returned stream is evaluated.
  1428      * <p> The resources will be located when the returned stream is evaluated.
  1419      * If the evaluation results in an {@code IOException} then the I/O
  1429      * If the evaluation results in an {@code IOException} then the I/O
  1420      * exception is wrapped in an {@link UncheckedIOException} that is then
  1430      * exception is wrapped in an {@link UncheckedIOException} that is then
  1421      * thrown.
  1431      * thrown.
       
  1432      *
       
  1433      * <p> Resources in named modules are subject to the encapsulation rules
       
  1434      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1435      * Additionally, and except for the special case where the resource has a
       
  1436      * name ending with "{@code .class}", this method will only find resources in
       
  1437      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1438      * opened} unconditionally (even if the caller of this method is in the
       
  1439      * same module as the resource). </p>
  1422      *
  1440      *
  1423      * @apiNote When overriding this method it is recommended that an
  1441      * @apiNote When overriding this method it is recommended that an
  1424      * implementation ensures that any delegation is consistent with the {@link
  1442      * implementation ensures that any delegation is consistent with the {@link
  1425      * #getResource(java.lang.String) getResource(String)} method. This should
  1443      * #getResource(java.lang.String) getResource(String)} method. This should
  1426      * ensure that the first element returned by the stream is the same
  1444      * ensure that the first element returned by the stream is the same
  1428      *
  1446      *
  1429      * @param  name
  1447      * @param  name
  1430      *         The resource name
  1448      *         The resource name
  1431      *
  1449      *
  1432      * @return  A stream of resource {@link java.net.URL URL} objects. If no
  1450      * @return  A stream of resource {@link java.net.URL URL} objects. If no
  1433      *          resources could  be found, the stream will be empty.  Resources
  1451      *          resources could  be found, the stream will be empty. Resources
  1434      *          that the class loader doesn't have access to will not be in the
  1452      *          for which a {@code URL} cannot be constructed, are in a package
  1435      *          stream.
  1453      *          that is not opened unconditionally, or access to the resource
       
  1454      *          is denied by the security manager, will not be in the stream.
  1436      *
  1455      *
  1437      * @see  #findResources(String)
  1456      * @see  #findResources(String)
  1438      *
  1457      *
  1439      * @since  9
  1458      * @since  9
  1440      */
  1459      */
  1453 
  1472 
  1454     /**
  1473     /**
  1455      * Finds the resource with the given name. Class loader implementations
  1474      * Finds the resource with the given name. Class loader implementations
  1456      * should override this method to specify where to find resources.
  1475      * should override this method to specify where to find resources.
  1457      *
  1476      *
  1458      * Resources in a named module are private to that module. This method does
  1477      * <p> For resources in named modules then the method must implement the
  1459      * not find resources in named modules defined to this class loader.
  1478      * rules for encapsulation specified in the {@code Module} {@link
       
  1479      * Module#getResourceAsStream getResourceAsStream} method. Additionally,
       
  1480      * it must not find non-"{@code .class}" resources in packages of named
       
  1481      * modules unless the package is {@link Module#isOpen(String) opened}
       
  1482      * unconditionally. </p>
  1460      *
  1483      *
  1461      * @param  name
  1484      * @param  name
  1462      *         The resource name
  1485      *         The resource name
  1463      *
  1486      *
  1464      * @return  A <tt>URL</tt> object for reading the resource, or
  1487      * @return  {@code URL} object for reading the resource; {@code null} if
  1465      *          <tt>null</tt> if the resource could not be found
  1488      *          the resource could not be found, a {@code URL} could not be
       
  1489      *          constructed to locate the resource, the resource is in a package
       
  1490      *          that is not opened unconditionally, or access to the resource is
       
  1491      *          denied by the security manager.
  1466      *
  1492      *
  1467      * @since  1.2
  1493      * @since  1.2
  1468      */
  1494      */
  1469     protected URL findResource(String name) {
  1495     protected URL findResource(String name) {
  1470         return null;
  1496         return null;
  1474      * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
  1500      * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
  1475      * representing all the resources with the given name. Class loader
  1501      * representing all the resources with the given name. Class loader
  1476      * implementations should override this method to specify where to load
  1502      * implementations should override this method to specify where to load
  1477      * resources from.
  1503      * resources from.
  1478      *
  1504      *
  1479      * Resources in a named module are private to that module. This method does
  1505      * <p> For resources in named modules then the method must implement the
  1480      * not find resources in named modules defined to this class loader.
  1506      * rules for encapsulation specified in the {@code Module} {@link
       
  1507      * Module#getResourceAsStream getResourceAsStream} method. Additionally,
       
  1508      * it must not find non-"{@code .class}" resources in packages of named
       
  1509      * modules unless the package is {@link Module#isOpen(String) opened}
       
  1510      * unconditionally. </p>
  1481      *
  1511      *
  1482      * @param  name
  1512      * @param  name
  1483      *         The resource name
  1513      *         The resource name
  1484      *
  1514      *
  1485      * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  1515      * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  1486      *          the resources
  1516      *          the resource. If no resources could  be found, the enumeration
       
  1517      *          will be empty. Resources for which a {@code URL} cannot be
       
  1518      *          constructed, are in a package that is not opened unconditionally,
       
  1519      *          or access to the resource is denied by the security manager,
       
  1520      *          are not returned in the enumeration.
  1487      *
  1521      *
  1488      * @throws  IOException
  1522      * @throws  IOException
  1489      *          If I/O errors occur
  1523      *          If I/O errors occur
  1490      *
  1524      *
  1491      * @since  1.2
  1525      * @since  1.2
  1492      */
  1526      */
  1493     protected Enumeration<URL> findResources(String name) throws IOException {
  1527     protected Enumeration<URL> findResources(String name) throws IOException {
  1494         return java.util.Collections.emptyEnumeration();
  1528         return Collections.emptyEnumeration();
  1495     }
  1529     }
  1496 
  1530 
  1497     /**
  1531     /**
  1498      * Registers the caller as {@linkplain #isParallelCapable() parallel capable}.
  1532      * Registers the caller as {@linkplain #isParallelCapable() parallel capable}.
  1499      * The registration succeeds if and only if all of the following
  1533      * The registration succeeds if and only if all of the following
  1539     /**
  1573     /**
  1540      * Find a resource of the specified name from the search path used to load
  1574      * Find a resource of the specified name from the search path used to load
  1541      * classes.  This method locates the resource through the system class
  1575      * classes.  This method locates the resource through the system class
  1542      * loader (see {@link #getSystemClassLoader()}).
  1576      * loader (see {@link #getSystemClassLoader()}).
  1543      *
  1577      *
  1544      * Resources in a named module are private to that module. This method does
  1578      * <p> Resources in named modules are subject to the encapsulation rules
  1545      * not find resources in named modules.
  1579      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1580      * Additionally, and except for the special case where the resource has a
       
  1581      * name ending with "{@code .class}", this method will only find resources in
       
  1582      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1583      * opened} unconditionally. </p>
  1546      *
  1584      *
  1547      * @param  name
  1585      * @param  name
  1548      *         The resource name
  1586      *         The resource name
  1549      *
  1587      *
  1550      * @return  A {@link java.net.URL <tt>URL</tt>} object for reading the
  1588      * @return  A {@link java.net.URL <tt>URL</tt>} to the resource; {@code
  1551      *          resource, or <tt>null</tt> if the resource could not be found
  1589      *          null} if the resource could not be found, a URL could not be
       
  1590      *          constructed to locate the resource, the resource is in a package
       
  1591      *          that is not opened unconditionally or access to the resource is
       
  1592      *          denied by the security manager.
  1552      *
  1593      *
  1553      * @since  1.1
  1594      * @since  1.1
  1554      */
  1595      */
  1555     public static URL getSystemResource(String name) {
  1596     public static URL getSystemResource(String name) {
  1556         return getSystemClassLoader().getResource(name);
  1597         return getSystemClassLoader().getResource(name);
  1560      * Finds all resources of the specified name from the search path used to
  1601      * Finds all resources of the specified name from the search path used to
  1561      * load classes.  The resources thus found are returned as an
  1602      * load classes.  The resources thus found are returned as an
  1562      * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
  1603      * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
  1563      * java.net.URL <tt>URL</tt>} objects.
  1604      * java.net.URL <tt>URL</tt>} objects.
  1564      *
  1605      *
  1565      * Resources in a named module are private to that module. This method does
       
  1566      * not find resources in named modules.
       
  1567      *
       
  1568      * <p> The search order is described in the documentation for {@link
  1606      * <p> The search order is described in the documentation for {@link
  1569      * #getSystemResource(String)}.  </p>
  1607      * #getSystemResource(String)}.  </p>
  1570      *
  1608      *
       
  1609      * <p> Resources in named modules are subject to the encapsulation rules
       
  1610      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1611      * Additionally, and except for the special case where the resource has a
       
  1612      * name ending with "{@code .class}", this method will only find resources in
       
  1613      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1614      * opened} unconditionally. </p>
       
  1615      *
  1571      * @param  name
  1616      * @param  name
  1572      *         The resource name
  1617      *         The resource name
  1573      *
  1618      *
  1574      * @return  An enumeration of resource {@link java.net.URL <tt>URL</tt>}
  1619      * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
  1575      *          objects
  1620      *          the resource. If no resources could  be found, the enumeration
       
  1621      *          will be empty. Resources for which a {@code URL} cannot be
       
  1622      *          constructed, are in a package that is not opened unconditionally,
       
  1623      *          or access to the resource is denied by the security manager,
       
  1624      *          are not returned in the enumeration.
  1576      *
  1625      *
  1577      * @throws  IOException
  1626      * @throws  IOException
  1578      *          If I/O errors occur
  1627      *          If I/O errors occur
  1579      *
  1628      *
  1580      * @since  1.2
  1629      * @since  1.2
  1586     }
  1635     }
  1587 
  1636 
  1588     /**
  1637     /**
  1589      * Returns an input stream for reading the specified resource.
  1638      * Returns an input stream for reading the specified resource.
  1590      *
  1639      *
  1591      * Resources in a named module are private to that module. This method does
       
  1592      * not find resources in named modules.
       
  1593      *
       
  1594      * <p> The search order is described in the documentation for {@link
  1640      * <p> The search order is described in the documentation for {@link
  1595      * #getResource(String)}.  </p>
  1641      * #getResource(String)}.  </p>
  1596      *
  1642      *
       
  1643      * <p> Resources in named modules are subject to the encapsulation rules
       
  1644      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1645      * Additionally, and except for the special case where the resource has a
       
  1646      * name ending with "{@code .class}", this method will only find resources in
       
  1647      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1648      * opened} unconditionally. </p>
       
  1649      *
  1597      * @param  name
  1650      * @param  name
  1598      *         The resource name
  1651      *         The resource name
  1599      *
  1652      *
  1600      * @return  An input stream for reading the resource, or <tt>null</tt>
  1653      * @return  An input stream for reading the resource; {@code null} if the
  1601      *          if the resource could not be found
  1654      *          resource could not be found, the resource is in a package that
       
  1655      *          is not opened unconditionally, or access to the resource is
       
  1656      *          denied by the security manager.
  1602      *
  1657      *
  1603      * @since  1.1
  1658      * @since  1.1
  1604      */
  1659      */
  1605     public InputStream getResourceAsStream(String name) {
  1660     public InputStream getResourceAsStream(String name) {
  1606         URL url = getResource(name);
  1661         URL url = getResource(name);
  1614     /**
  1669     /**
  1615      * Open for reading, a resource of the specified name from the search path
  1670      * Open for reading, a resource of the specified name from the search path
  1616      * used to load classes.  This method locates the resource through the
  1671      * used to load classes.  This method locates the resource through the
  1617      * system class loader (see {@link #getSystemClassLoader()}).
  1672      * system class loader (see {@link #getSystemClassLoader()}).
  1618      *
  1673      *
  1619      * Resources in a named module are private to that module. This method does
  1674      * <p> Resources in named modules are subject to the encapsulation rules
  1620      * not find resources in named modules.
  1675      * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
       
  1676      * Additionally, and except for the special case where the resource has a
       
  1677      * name ending with "{@code .class}", this method will only find resources in
       
  1678      * packages of named modules when the package is {@link Module#isOpen(String)
       
  1679      * opened} unconditionally. </p>
  1621      *
  1680      *
  1622      * @param  name
  1681      * @param  name
  1623      *         The resource name
  1682      *         The resource name
  1624      *
  1683      *
  1625      * @return  An input stream for reading the resource, or <tt>null</tt>
  1684      * @return  An input stream for reading the resource; {@code null} if the
  1626      *          if the resource could not be found
  1685      *          resource could not be found, the resource is in a package that
       
  1686      *          is not opened unconditionally, or access to the resource is
       
  1687      *          denied by the security manager.
  1627      *
  1688      *
  1628      * @since  1.1
  1689      * @since  1.1
  1629      */
  1690      */
  1630     public static InputStream getSystemResourceAsStream(String name) {
  1691     public static InputStream getSystemResourceAsStream(String name) {
  1631         URL url = getSystemResource(name);
  1692         URL url = getSystemResource(name);
  2707 
  2768 
  2708     // Retrieves the assertion directives from the VM.
  2769     // Retrieves the assertion directives from the VM.
  2709     private static native AssertionStatusDirectives retrieveDirectives();
  2770     private static native AssertionStatusDirectives retrieveDirectives();
  2710 
  2771 
  2711 
  2772 
  2712     /**
  2773     // -- Misc --
  2713      * Returns the ServiceCatalog for modules defined to this class loader
       
  2714      * or {@code null} if this class loader does not have a services catalog.
       
  2715      */
       
  2716     ServicesCatalog getServicesCatalog() {
       
  2717         return servicesCatalog;
       
  2718     }
       
  2719 
       
  2720     /**
       
  2721      * Returns the ServiceCatalog for modules defined to this class loader,
       
  2722      * creating it if it doesn't already exist.
       
  2723      */
       
  2724     ServicesCatalog createOrGetServicesCatalog() {
       
  2725         ServicesCatalog catalog = servicesCatalog;
       
  2726         if (catalog == null) {
       
  2727             catalog = ServicesCatalog.create();
       
  2728             boolean set = trySetObjectField("servicesCatalog", catalog);
       
  2729             if (!set) {
       
  2730                 // beaten by someone else
       
  2731                 catalog = servicesCatalog;
       
  2732             }
       
  2733         }
       
  2734         return catalog;
       
  2735     }
       
  2736 
       
  2737     // the ServiceCatalog for modules associated with this class loader.
       
  2738     private volatile ServicesCatalog servicesCatalog;
       
  2739 
  2774 
  2740     /**
  2775     /**
  2741      * Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s)
  2776      * Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s)
  2742      * associated with this ClassLoader, creating it if it doesn't already exist.
  2777      * associated with this ClassLoader, creating it if it doesn't already exist.
  2743      */
  2778      */