91 * classes. |
91 * classes. |
92 * |
92 * |
93 * <p> Class loaders may typically be used by security managers to indicate |
93 * <p> Class loaders may typically be used by security managers to indicate |
94 * security domains. |
94 * security domains. |
95 * |
95 * |
|
96 * <p> In addition to loading classes, a class loader is also responsible for |
|
97 * locating resources. A resource is some data (a "{@code .class}" file, |
|
98 * configuration data, or an image for example) that is identified with an |
|
99 * abstract '/'-separated path name. Resources are typically packaged with an |
|
100 * application or library so that they can be located by code in the |
|
101 * application or library. In some cases, the resources are included so that |
|
102 * they can be located by other libraries. |
|
103 * |
96 * <p> The {@code ClassLoader} class uses a delegation model to search for |
104 * <p> The {@code ClassLoader} class uses a delegation model to search for |
97 * classes and resources. Each instance of {@code ClassLoader} has an |
105 * classes and resources. Each instance of {@code ClassLoader} has an |
98 * associated parent class loader. When requested to find a class or |
106 * associated parent class loader. When requested to find a class or |
99 * resource, a {@code ClassLoader} instance will delegate the search for the |
107 * resource, a {@code ClassLoader} instance will usually delegate the search |
100 * class or resource to its parent class loader before attempting to find the |
108 * for the class or resource to its parent class loader before attempting to |
101 * class or resource itself. |
109 * find the class or resource itself. |
102 * |
110 * |
103 * <p> Class loaders that support concurrent loading of classes are known as |
111 * <p> Class loaders that support concurrent loading of classes are known as |
104 * <em>{@linkplain #isRegisteredAsParallelCapable() parallel capable}</em> class |
112 * <em>{@linkplain #isRegisteredAsParallelCapable() parallel capable}</em> class |
105 * loaders and are required to register themselves at their class initialization |
113 * loaders and are required to register themselves at their class initialization |
106 * time by invoking the {@link |
114 * time by invoking the {@link |
127 * that can be used as the parent of a {@code ClassLoader} instance. |
135 * that can be used as the parent of a {@code ClassLoader} instance. |
128 * Platform classes include Java SE platform APIs, their implementation |
136 * Platform classes include Java SE platform APIs, their implementation |
129 * classes and JDK-specific run-time classes that are defined by the |
137 * classes and JDK-specific run-time classes that are defined by the |
130 * platform class loader or its ancestors. |
138 * platform class loader or its ancestors. |
131 * <p> To allow for upgrading/overriding of modules defined to the platform |
139 * <p> To allow for upgrading/overriding of modules defined to the platform |
132 * class loader, and where classes in the upgraded version link to |
140 * class loader, and where upgraded modules read modules defined to class |
133 * classes in modules defined to the application class loader, the |
141 * loaders other than the platform class loader and its ancestors, then |
134 * platform class loader may delegate to the application class loader. |
142 * the platform class loader may have to delegate to other class loaders, |
135 * In other words, classes in named modules defined to the application |
143 * the application class loader for example. |
136 * class loader may be visible to the platform class loader. </li> |
144 * In other words, classes in named modules defined to class loaders |
|
145 * other than the platform class loader and its ancestors may be visible |
|
146 * to the platform class loader. </li> |
137 * <li><p>{@linkplain #getSystemClassLoader() System class loader}. |
147 * <li><p>{@linkplain #getSystemClassLoader() System class loader}. |
138 * It is also known as <em>application class loader</em> and is distinct |
148 * It is also known as <em>application class loader</em> and is distinct |
139 * from the platform class loader. |
149 * from the platform class loader. |
140 * The system class loader is typically used to define classes on the |
150 * The system class loader is typically used to define classes on the |
141 * application class path, module path, and JDK-specific tools. |
151 * application class path, module path, and JDK-specific tools. |
496 * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class |
506 * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class |
497 * has already been loaded. </p></li> |
507 * has already been loaded. </p></li> |
498 * |
508 * |
499 * <li><p> Invoke the {@link #loadClass(String) loadClass} method |
509 * <li><p> Invoke the {@link #loadClass(String) loadClass} method |
500 * on the parent class loader. If the parent is {@code null} the class |
510 * on the parent class loader. If the parent is {@code null} the class |
501 * loader built-in to the virtual machine is used, instead. </p></li> |
511 * loader built into the virtual machine is used, instead. </p></li> |
502 * |
512 * |
503 * <li><p> Invoke the {@link #findClass(String)} method to find the |
513 * <li><p> Invoke the {@link #findClass(String)} method to find the |
504 * class. </p></li> |
514 * class. </p></li> |
505 * |
515 * |
506 * </ol> |
516 * </ol> |
679 /** |
689 /** |
680 * Finds the class with the specified <a href="#name">binary name</a>. |
690 * Finds the class with the specified <a href="#name">binary name</a>. |
681 * This method should be overridden by class loader implementations that |
691 * This method should be overridden by class loader implementations that |
682 * follow the delegation model for loading classes, and will be invoked by |
692 * follow the delegation model for loading classes, and will be invoked by |
683 * the {@link #loadClass loadClass} method after checking the |
693 * the {@link #loadClass loadClass} method after checking the |
684 * parent class loader for the requested class. The default implementation |
694 * parent class loader for the requested class. |
685 * throws a {@code ClassNotFoundException}. |
695 * |
|
696 * @implSpec The default implementation throws {@code ClassNotFoundException}. |
686 * |
697 * |
687 * @param name |
698 * @param name |
688 * The <a href="#name">binary name</a> of the class |
699 * The <a href="#name">binary name</a> of the class |
689 * |
700 * |
690 * @return The resulting {@code Class} object |
701 * @return The resulting {@code Class} object |
1125 } else { |
1136 } else { |
1126 pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs). |
1137 pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs). |
1127 putIfAbsent(pname, (certs == null? nocerts:certs)); |
1138 putIfAbsent(pname, (certs == null? nocerts:certs)); |
1128 } |
1139 } |
1129 if (pcerts != null && !compareCerts(pcerts, certs)) { |
1140 if (pcerts != null && !compareCerts(pcerts, certs)) { |
1130 throw new SecurityException("class \""+ name + |
1141 throw new SecurityException("class \"" + name |
1131 "\"'s signer information does not match signer information of other classes in the same package"); |
1142 + "\"'s signer information does not match signer information" |
|
1143 + " of other classes in the same package"); |
1132 } |
1144 } |
1133 } |
1145 } |
1134 |
1146 |
1135 /** |
1147 /** |
1136 * check to make sure the certs for the new class (certs) are the same as |
1148 * check to make sure the certs for the new class (certs) are the same as |
1327 * Finds the resource with the given name. A resource is some data |
1339 * Finds the resource with the given name. A resource is some data |
1328 * (images, audio, text, etc) that can be accessed by class code in a way |
1340 * (images, audio, text, etc) that can be accessed by class code in a way |
1329 * that is independent of the location of the code. |
1341 * that is independent of the location of the code. |
1330 * |
1342 * |
1331 * <p> The name of a resource is a '{@code /}'-separated path name that |
1343 * <p> The name of a resource is a '{@code /}'-separated path name that |
1332 * identifies the resource. |
1344 * identifies the resource. </p> |
1333 * |
|
1334 * <p> This method will first search the parent class loader for the |
|
1335 * resource; if the parent is {@code null} the path of the class loader |
|
1336 * built-in to the virtual machine is searched. That failing, this method |
|
1337 * will invoke {@link #findResource(String)} to find the resource. </p> |
|
1338 * |
1345 * |
1339 * <p> Resources in named modules are subject to the encapsulation rules |
1346 * <p> Resources in named modules are subject to the encapsulation rules |
1340 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}. |
1347 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}. |
1341 * Additionally, and except for the special case where the resource has a |
1348 * Additionally, and except for the special case where the resource has a |
1342 * name ending with "{@code .class}", this method will only find resources in |
1349 * name ending with "{@code .class}", this method will only find resources in |
1343 * packages of named modules when the package is {@link Module#isOpen(String) |
1350 * packages of named modules when the package is {@link Module#isOpen(String) |
1344 * opened} unconditionally (even if the caller of this method is in the |
1351 * opened} unconditionally (even if the caller of this method is in the |
1345 * same module as the resource). </p> |
1352 * same module as the resource). </p> |
1346 * |
1353 * |
|
1354 * @implSpec The default implementation will first search the parent class |
|
1355 * loader for the resource; if the parent is {@code null} the path of the |
|
1356 * class loader built into the virtual machine is searched. If not found, |
|
1357 * this method will invoke {@link #findResource(String)} to find the resource. |
|
1358 * |
1347 * @apiNote Where several modules are defined to the same class loader, |
1359 * @apiNote Where several modules are defined to the same class loader, |
1348 * and where more than one module contains a resource with the given name, |
1360 * and where more than one module contains a resource with the given name, |
1349 * then the ordering that modules are searched is not specified and may be |
1361 * then the ordering that modules are searched is not specified and may be |
1350 * very unpredictable. |
1362 * very unpredictable. |
1351 * When overriding this method it is recommended that an implementation |
1363 * When overriding this method it is recommended that an implementation |
1385 * Finds all the resources with the given name. A resource is some data |
1397 * Finds all the resources with the given name. A resource is some data |
1386 * (images, audio, text, etc) that can be accessed by class code in a way |
1398 * (images, audio, text, etc) that can be accessed by class code in a way |
1387 * that is independent of the location of the code. |
1399 * that is independent of the location of the code. |
1388 * |
1400 * |
1389 * <p> The name of a resource is a {@code /}-separated path name that |
1401 * <p> The name of a resource is a {@code /}-separated path name that |
1390 * identifies the resource. |
1402 * identifies the resource. </p> |
1391 * |
|
1392 * <p> The delegation order for searching is described in the documentation |
|
1393 * for {@link #getResource(String)}. </p> |
|
1394 * |
1403 * |
1395 * <p> Resources in named modules are subject to the encapsulation rules |
1404 * <p> Resources in named modules are subject to the encapsulation rules |
1396 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}. |
1405 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}. |
1397 * Additionally, and except for the special case where the resource has a |
1406 * Additionally, and except for the special case where the resource has a |
1398 * name ending with "{@code .class}", this method will only find resources in |
1407 * name ending with "{@code .class}", this method will only find resources in |
1399 * packages of named modules when the package is {@link Module#isOpen(String) |
1408 * packages of named modules when the package is {@link Module#isOpen(String) |
1400 * opened} unconditionally (even if the caller of this method is in the |
1409 * opened} unconditionally (even if the caller of this method is in the |
1401 * same module as the resource).</p> |
1410 * same module as the resource). </p> |
|
1411 * |
|
1412 * @implSpec The default implementation will first search the parent class |
|
1413 * loader for the resource; if the parent is {@code null} the path of the |
|
1414 * class loader built into the virtual machine is searched. It then |
|
1415 * invokes {@link #findResources(String)} to find the resources with the |
|
1416 * name in this class loader. It returns an enumeration whose elements |
|
1417 * are the URLs found by searching the parent class loader followed by |
|
1418 * the elements found with {@code findResources}. |
1402 * |
1419 * |
1403 * @apiNote Where several modules are defined to the same class loader, |
1420 * @apiNote Where several modules are defined to the same class loader, |
1404 * and where more than one module contains a resource with the given name, |
1421 * and where more than one module contains a resource with the given name, |
1405 * then the ordering is not specified and may be very unpredictable. |
1422 * then the ordering is not specified and may be very unpredictable. |
1406 * When overriding this method it is recommended that an |
1423 * When overriding this method it is recommended that an |
1451 * location of the code. |
1466 * location of the code. |
1452 * |
1467 * |
1453 * <p> The name of a resource is a {@code /}-separated path name that |
1468 * <p> The name of a resource is a {@code /}-separated path name that |
1454 * identifies the resource. |
1469 * identifies the resource. |
1455 * |
1470 * |
1456 * <p> The search order is described in the documentation for {@link |
|
1457 * #getResource(String)}. |
|
1458 * |
|
1459 * <p> The resources will be located when the returned stream is evaluated. |
1471 * <p> The resources will be located when the returned stream is evaluated. |
1460 * If the evaluation results in an {@code IOException} then the I/O |
1472 * If the evaluation results in an {@code IOException} then the I/O |
1461 * exception is wrapped in an {@link UncheckedIOException} that is then |
1473 * exception is wrapped in an {@link UncheckedIOException} that is then |
1462 * thrown. |
1474 * thrown. |
1463 * |
1475 * |
1467 * name ending with "{@code .class}", this method will only find resources in |
1479 * name ending with "{@code .class}", this method will only find resources in |
1468 * packages of named modules when the package is {@link Module#isOpen(String) |
1480 * packages of named modules when the package is {@link Module#isOpen(String) |
1469 * opened} unconditionally (even if the caller of this method is in the |
1481 * opened} unconditionally (even if the caller of this method is in the |
1470 * same module as the resource). </p> |
1482 * same module as the resource). </p> |
1471 * |
1483 * |
|
1484 * @implSpec The default implementation invokes {@link #getResources(String) |
|
1485 * getResources} to find all the resources with the given name and returns |
|
1486 * a stream with the elements in the enumeration as the source. |
|
1487 * |
1472 * @apiNote When overriding this method it is recommended that an |
1488 * @apiNote When overriding this method it is recommended that an |
1473 * implementation ensures that any delegation is consistent with the {@link |
1489 * implementation ensures that any delegation is consistent with the {@link |
1474 * #getResource(java.lang.String) getResource(String)} method. This should |
1490 * #getResource(java.lang.String) getResource(String)} method. This should |
1475 * ensure that the first element returned by the stream is the same |
1491 * ensure that the first element returned by the stream is the same |
1476 * resource that the {@code getResource(String)} method would return. |
1492 * resource that the {@code getResource(String)} method would return. |
1483 * for which a {@code URL} cannot be constructed, are in a package |
1499 * for which a {@code URL} cannot be constructed, are in a package |
1484 * that is not opened unconditionally, or access to the resource |
1500 * that is not opened unconditionally, or access to the resource |
1485 * is denied by the security manager, will not be in the stream. |
1501 * is denied by the security manager, will not be in the stream. |
1486 * |
1502 * |
1487 * @throws NullPointerException If {@code name} is {@code null} |
1503 * @throws NullPointerException If {@code name} is {@code null} |
1488 * |
|
1489 * @see #findResources(String) |
|
1490 * |
1504 * |
1491 * @since 9 |
1505 * @since 9 |
1492 */ |
1506 */ |
1493 public Stream<URL> resources(String name) { |
1507 public Stream<URL> resources(String name) { |
1494 Objects.requireNonNull(name); |
1508 Objects.requireNonNull(name); |
1504 return StreamSupport.stream(si, characteristics, false); |
1518 return StreamSupport.stream(si, characteristics, false); |
1505 } |
1519 } |
1506 |
1520 |
1507 /** |
1521 /** |
1508 * Finds the resource with the given name. Class loader implementations |
1522 * Finds the resource with the given name. Class loader implementations |
1509 * should override this method to specify where to find resources. |
1523 * should override this method. |
1510 * |
1524 * |
1511 * <p> For resources in named modules then the method must implement the |
1525 * <p> For resources in named modules then the method must implement the |
1512 * rules for encapsulation specified in the {@code Module} {@link |
1526 * rules for encapsulation specified in the {@code Module} {@link |
1513 * Module#getResourceAsStream getResourceAsStream} method. Additionally, |
1527 * Module#getResourceAsStream getResourceAsStream} method. Additionally, |
1514 * it must not find non-"{@code .class}" resources in packages of named |
1528 * it must not find non-"{@code .class}" resources in packages of named |
1515 * modules unless the package is {@link Module#isOpen(String) opened} |
1529 * modules unless the package is {@link Module#isOpen(String) opened} |
1516 * unconditionally. </p> |
1530 * unconditionally. </p> |
1517 * |
1531 * |
|
1532 * @implSpec The default implementation returns {@code null}. |
|
1533 * |
1518 * @param name |
1534 * @param name |
1519 * The resource name |
1535 * The resource name |
1520 * |
1536 * |
1521 * @return {@code URL} object for reading the resource; {@code null} if |
1537 * @return {@code URL} object for reading the resource; {@code null} if |
1522 * the resource could not be found, a {@code URL} could not be |
1538 * the resource could not be found, a {@code URL} could not be |
1533 } |
1549 } |
1534 |
1550 |
1535 /** |
1551 /** |
1536 * Returns an enumeration of {@link java.net.URL URL} objects |
1552 * Returns an enumeration of {@link java.net.URL URL} objects |
1537 * representing all the resources with the given name. Class loader |
1553 * representing all the resources with the given name. Class loader |
1538 * implementations should override this method to specify where to load |
1554 * implementations should override this method. |
1539 * resources from. |
|
1540 * |
1555 * |
1541 * <p> For resources in named modules then the method must implement the |
1556 * <p> For resources in named modules then the method must implement the |
1542 * rules for encapsulation specified in the {@code Module} {@link |
1557 * rules for encapsulation specified in the {@code Module} {@link |
1543 * Module#getResourceAsStream getResourceAsStream} method. Additionally, |
1558 * Module#getResourceAsStream getResourceAsStream} method. Additionally, |
1544 * it must not find non-"{@code .class}" resources in packages of named |
1559 * it must not find non-"{@code .class}" resources in packages of named |
1545 * modules unless the package is {@link Module#isOpen(String) opened} |
1560 * modules unless the package is {@link Module#isOpen(String) opened} |
1546 * unconditionally. </p> |
1561 * unconditionally. </p> |
|
1562 * |
|
1563 * @implSpec The default implementation returns an enumeration that |
|
1564 * contains no elements. |
1547 * |
1565 * |
1548 * @param name |
1566 * @param name |
1549 * The resource name |
1567 * The resource name |
1550 * |
1568 * |
1551 * @return An enumeration of {@link java.net.URL URL} objects for |
1569 * @return An enumeration of {@link java.net.URL URL} objects for |
1897 case 1: |
1915 case 1: |
1898 case 2: |
1916 case 2: |
1899 // the system class loader is the built-in app class loader during startup |
1917 // the system class loader is the built-in app class loader during startup |
1900 return getBuiltinAppClassLoader(); |
1918 return getBuiltinAppClassLoader(); |
1901 case 3: |
1919 case 3: |
1902 throw new InternalError("getSystemClassLoader should only be called after VM booted"); |
1920 String msg = "getSystemClassLoader should only be called after VM booted"; |
|
1921 throw new InternalError(msg); |
1903 case 4: |
1922 case 4: |
1904 // system fully initialized |
1923 // system fully initialized |
1905 assert VM.isBooted() && scl != null; |
1924 assert VM.isBooted() && scl != null; |
1906 SecurityManager sm = System.getSecurityManager(); |
1925 SecurityManager sm = System.getSecurityManager(); |
1907 if (sm != null) { |
1926 if (sm != null) { |