jdk/src/java.base/share/classes/java/lang/ClassLoader.java
changeset 43712 5dfd0950317c
parent 43062 b9593792dfd9
child 44359 c6761862ca0b
equal deleted inserted replaced
43619:dc9102c475f3 43712:5dfd0950317c
   205  *
   205  *
   206  * @jls 6.7  Fully Qualified Names
   206  * @jls 6.7  Fully Qualified Names
   207  * @jls 13.1 The Form of a Binary
   207  * @jls 13.1 The Form of a Binary
   208  * @see      #resolveClass(Class)
   208  * @see      #resolveClass(Class)
   209  * @since 1.0
   209  * @since 1.0
       
   210  * @revised 9
       
   211  * @spec JPMS
   210  */
   212  */
   211 public abstract class ClassLoader {
   213 public abstract class ClassLoader {
   212 
   214 
   213     private static native void registerNatives();
   215     private static native void registerNatives();
   214     static {
   216     static {
   378      *         If a security manager exists and its
   380      *         If a security manager exists and its
   379      *         {@link SecurityManager#checkCreateClassLoader()}
   381      *         {@link SecurityManager#checkCreateClassLoader()}
   380      *         method doesn't allow creation of a new class loader.
   382      *         method doesn't allow creation of a new class loader.
   381      *
   383      *
   382      * @since  9
   384      * @since  9
       
   385      * @spec JPMS
   383      */
   386      */
   384     protected ClassLoader(String name, ClassLoader parent) {
   387     protected ClassLoader(String name, ClassLoader parent) {
   385         this(checkCreateClassLoader(name), name, parent);
   388         this(checkCreateClassLoader(name), name, parent);
   386     }
   389     }
   387 
       
   388 
   390 
   389     /**
   391     /**
   390      * Creates a new class loader using the specified parent class loader for
   392      * Creates a new class loader using the specified parent class loader for
   391      * delegation.
   393      * delegation.
   392      *
   394      *
   438      *
   440      *
   439      * @return name of this class loader; or {@code null} if
   441      * @return name of this class loader; or {@code null} if
   440      * this class loader is not named.
   442      * this class loader is not named.
   441      *
   443      *
   442      * @since 9
   444      * @since 9
       
   445      * @spec JPMS
   443      */
   446      */
   444     public String getName() {
   447     public String getName() {
   445         return name;
   448         return name;
   446     }
   449     }
   447 
   450 
   708      *
   711      *
   709      * @return The resulting {@code Class} object, or {@code null}
   712      * @return The resulting {@code Class} object, or {@code null}
   710      *         if the class could not be found.
   713      *         if the class could not be found.
   711      *
   714      *
   712      * @since 9
   715      * @since 9
       
   716      * @spec JPMS
   713      */
   717      */
   714     protected Class<?> findClass(String moduleName, String name) {
   718     protected Class<?> findClass(String moduleName, String name) {
   715         if (moduleName == null) {
   719         if (moduleName == null) {
   716             try {
   720             try {
   717                 return findClass(name);
   721                 return findClass(name);
   832      * @see  #resolveClass(Class)
   836      * @see  #resolveClass(Class)
   833      * @see  java.security.CodeSource
   837      * @see  java.security.CodeSource
   834      * @see  java.security.SecureClassLoader
   838      * @see  java.security.SecureClassLoader
   835      *
   839      *
   836      * @since  1.1
   840      * @since  1.1
       
   841      * @revised 9
       
   842      * @spec JPMS
   837      */
   843      */
   838     protected final Class<?> defineClass(String name, byte[] b, int off, int len)
   844     protected final Class<?> defineClass(String name, byte[] b, int off, int len)
   839         throws ClassFormatError
   845         throws ClassFormatError
   840     {
   846     {
   841         return defineClass(name, b, off, len, null);
   847         return defineClass(name, b, off, len, null);
   965      *          If an attempt is made to add this class to a package that
   971      *          If an attempt is made to add this class to a package that
   966      *          contains classes that were signed by a different set of
   972      *          contains classes that were signed by a different set of
   967      *          certificates than this class, or if {@code name} begins with
   973      *          certificates than this class, or if {@code name} begins with
   968      *          "{@code java.}" and this class loader is not the platform
   974      *          "{@code java.}" and this class loader is not the platform
   969      *          class loader or its ancestor.
   975      *          class loader or its ancestor.
       
   976      *
       
   977      * @revised 9
       
   978      * @spec JPMS
   970      */
   979      */
   971     protected final Class<?> defineClass(String name, byte[] b, int off, int len,
   980     protected final Class<?> defineClass(String name, byte[] b, int off, int len,
   972                                          ProtectionDomain protectionDomain)
   981                                          ProtectionDomain protectionDomain)
   973         throws ClassFormatError
   982         throws ClassFormatError
   974     {
   983     {
  1039      *          "{@code java.}".
  1048      *          "{@code java.}".
  1040      *
  1049      *
  1041      * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
  1050      * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
  1042      *
  1051      *
  1043      * @since  1.5
  1052      * @since  1.5
       
  1053      * @revised 9
       
  1054      * @spec JPMS
  1044      */
  1055      */
  1045     protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
  1056     protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
  1046                                          ProtectionDomain protectionDomain)
  1057                                          ProtectionDomain protectionDomain)
  1047         throws ClassFormatError
  1058         throws ClassFormatError
  1048     {
  1059     {
  1262     /**
  1273     /**
  1263      * Returns a URL to a resource in a module defined to this class loader.
  1274      * Returns a URL to a resource in a module defined to this class loader.
  1264      * Class loader implementations that support the loading from modules
  1275      * Class loader implementations that support the loading from modules
  1265      * should override this method.
  1276      * should override this method.
  1266      *
  1277      *
  1267      * @apiNote This method is the basis for the {@code Class} {@link
  1278      * @apiNote This method is the basis for the {@link
  1268      * Class#getResource getResource} and {@link Class#getResourceAsStream
  1279      * Class#getResource Class.getResource}, {@link Class#getResourceAsStream
  1269      * getResourceAsStream} methods. It is not subject to the rules for
  1280      * Class.getResourceAsStream}, and {@link Module#getResourceAsStream
  1270      * encapsulation specified by {@code Module} {@link
  1281      * Module.getResourceAsStream} methods. It is not subject to the rules for
  1271      * Module#getResourceAsStream getResourceAsStream}.
  1282      * encapsulation specified by {@code Module.getResourceAsStream}.
  1272      *
  1283      *
  1273      * @implSpec The default implementation attempts to find the resource by
  1284      * @implSpec The default implementation attempts to find the resource by
  1274      * invoking {@link #findResource(String)} when the {@code moduleName} is
  1285      * invoking {@link #findResource(String)} when the {@code moduleName} is
  1275      * {@code null}. It otherwise returns {@code null}.
  1286      * {@code null}. It otherwise returns {@code null}.
  1276      *
  1287      *
  1290      * @throws IOException
  1301      * @throws IOException
  1291      *         If I/O errors occur
  1302      *         If I/O errors occur
  1292      *
  1303      *
  1293      * @see java.lang.module.ModuleReader#find(String)
  1304      * @see java.lang.module.ModuleReader#find(String)
  1294      * @since 9
  1305      * @since 9
       
  1306      * @spec JPMS
  1295      */
  1307      */
  1296     protected URL findResource(String moduleName, String name) throws IOException {
  1308     protected URL findResource(String moduleName, String name) throws IOException {
  1297         if (moduleName == null) {
  1309         if (moduleName == null) {
  1298             return findResource(name);
  1310             return findResource(name);
  1299         } else {
  1311         } else {
  1340      *          denied by the security manager.
  1352      *          denied by the security manager.
  1341      *
  1353      *
  1342      * @throws  NullPointerException If {@code name} is {@code null}
  1354      * @throws  NullPointerException If {@code name} is {@code null}
  1343      *
  1355      *
  1344      * @since  1.1
  1356      * @since  1.1
       
  1357      * @revised 9
       
  1358      * @spec JPMS
  1345      */
  1359      */
  1346     public URL getResource(String name) {
  1360     public URL getResource(String name) {
  1347         Objects.requireNonNull(name);
  1361         Objects.requireNonNull(name);
  1348         URL url;
  1362         URL url;
  1349         if (parent != null) {
  1363         if (parent != null) {
  1401      * @throws  NullPointerException If {@code name} is {@code null}
  1415      * @throws  NullPointerException If {@code name} is {@code null}
  1402      *
  1416      *
  1403      * @see  #findResources(String)
  1417      * @see  #findResources(String)
  1404      *
  1418      *
  1405      * @since  1.2
  1419      * @since  1.2
       
  1420      * @revised 9
       
  1421      * @spec JPMS
  1406      */
  1422      */
  1407     public Enumeration<URL> getResources(String name) throws IOException {
  1423     public Enumeration<URL> getResources(String name) throws IOException {
  1408         Objects.requireNonNull(name);
  1424         Objects.requireNonNull(name);
  1409         @SuppressWarnings("unchecked")
  1425         @SuppressWarnings("unchecked")
  1410         Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
  1426         Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
  1497      *          constructed to locate the resource, the resource is in a package
  1513      *          constructed to locate the resource, the resource is in a package
  1498      *          that is not opened unconditionally, or access to the resource is
  1514      *          that is not opened unconditionally, or access to the resource is
  1499      *          denied by the security manager.
  1515      *          denied by the security manager.
  1500      *
  1516      *
  1501      * @since  1.2
  1517      * @since  1.2
       
  1518      * @revised 9
       
  1519      * @spec JPMS
  1502      */
  1520      */
  1503     protected URL findResource(String name) {
  1521     protected URL findResource(String name) {
  1504         return null;
  1522         return null;
  1505     }
  1523     }
  1506 
  1524 
  1529      *
  1547      *
  1530      * @throws  IOException
  1548      * @throws  IOException
  1531      *          If I/O errors occur
  1549      *          If I/O errors occur
  1532      *
  1550      *
  1533      * @since  1.2
  1551      * @since  1.2
       
  1552      * @revised 9
       
  1553      * @spec JPMS
  1534      */
  1554      */
  1535     protected Enumeration<URL> findResources(String name) throws IOException {
  1555     protected Enumeration<URL> findResources(String name) throws IOException {
  1536         return Collections.emptyEnumeration();
  1556         return Collections.emptyEnumeration();
  1537     }
  1557     }
  1538 
  1558 
  1599      *          constructed to locate the resource, the resource is in a package
  1619      *          constructed to locate the resource, the resource is in a package
  1600      *          that is not opened unconditionally or access to the resource is
  1620      *          that is not opened unconditionally or access to the resource is
  1601      *          denied by the security manager.
  1621      *          denied by the security manager.
  1602      *
  1622      *
  1603      * @since  1.1
  1623      * @since  1.1
       
  1624      * @revised 9
       
  1625      * @spec JPMS
  1604      */
  1626      */
  1605     public static URL getSystemResource(String name) {
  1627     public static URL getSystemResource(String name) {
  1606         return getSystemClassLoader().getResource(name);
  1628         return getSystemClassLoader().getResource(name);
  1607     }
  1629     }
  1608 
  1630 
  1634      *
  1656      *
  1635      * @throws  IOException
  1657      * @throws  IOException
  1636      *          If I/O errors occur
  1658      *          If I/O errors occur
  1637      *
  1659      *
  1638      * @since  1.2
  1660      * @since  1.2
       
  1661      * @revised 9
       
  1662      * @spec JPMS
  1639      */
  1663      */
  1640     public static Enumeration<URL> getSystemResources(String name)
  1664     public static Enumeration<URL> getSystemResources(String name)
  1641         throws IOException
  1665         throws IOException
  1642     {
  1666     {
  1643         return getSystemClassLoader().getResources(name);
  1667         return getSystemClassLoader().getResources(name);
  1665      *          denied by the security manager.
  1689      *          denied by the security manager.
  1666      *
  1690      *
  1667      * @throws  NullPointerException If {@code name} is {@code null}
  1691      * @throws  NullPointerException If {@code name} is {@code null}
  1668      *
  1692      *
  1669      * @since  1.1
  1693      * @since  1.1
       
  1694      * @revised 9
       
  1695      * @spec JPMS
  1670      */
  1696      */
  1671     public InputStream getResourceAsStream(String name) {
  1697     public InputStream getResourceAsStream(String name) {
  1672         Objects.requireNonNull(name);
  1698         Objects.requireNonNull(name);
  1673         URL url = getResource(name);
  1699         URL url = getResource(name);
  1674         try {
  1700         try {
  1697      *          resource could not be found, the resource is in a package that
  1723      *          resource could not be found, the resource is in a package that
  1698      *          is not opened unconditionally, or access to the resource is
  1724      *          is not opened unconditionally, or access to the resource is
  1699      *          denied by the security manager.
  1725      *          denied by the security manager.
  1700      *
  1726      *
  1701      * @since  1.1
  1727      * @since  1.1
       
  1728      * @revised 9
       
  1729      * @spec JPMS
  1702      */
  1730      */
  1703     public static InputStream getSystemResourceAsStream(String name) {
  1731     public static InputStream getSystemResourceAsStream(String name) {
  1704         URL url = getSystemResource(name);
  1732         URL url = getSystemResource(name);
  1705         try {
  1733         try {
  1706             return url != null ? url.openStream() : null;
  1734             return url != null ? url.openStream() : null;
  1747      *
  1775      *
  1748      * @return The unnamed Module for this class loader
  1776      * @return The unnamed Module for this class loader
  1749      *
  1777      *
  1750      * @see Module#isNamed()
  1778      * @see Module#isNamed()
  1751      * @since 9
  1779      * @since 9
       
  1780      * @spec JPMS
  1752      */
  1781      */
  1753     public final Module getUnnamedModule() {
  1782     public final Module getUnnamedModule() {
  1754         return unnamedModule;
  1783         return unnamedModule;
  1755     }
  1784     }
  1756 
  1785 
  1770      *          as or an ancestor of the platform class loader,
  1799      *          as or an ancestor of the platform class loader,
  1771      *          and the caller does not have the
  1800      *          and the caller does not have the
  1772      *          {@link RuntimePermission}{@code ("getClassLoader")}
  1801      *          {@link RuntimePermission}{@code ("getClassLoader")}
  1773      *
  1802      *
  1774      * @since 9
  1803      * @since 9
       
  1804      * @spec JPMS
  1775      */
  1805      */
  1776     @CallerSensitive
  1806     @CallerSensitive
  1777     public static ClassLoader getPlatformClassLoader() {
  1807     public static ClassLoader getPlatformClassLoader() {
  1778         SecurityManager sm = System.getSecurityManager();
  1808         SecurityManager sm = System.getSecurityManager();
  1779         ClassLoader loader = getBuiltinPlatformClassLoader();
  1809         ClassLoader loader = getBuiltinPlatformClassLoader();
  1845      *          exception is thrown by that constructor when it is invoked. The
  1875      *          exception is thrown by that constructor when it is invoked. The
  1846      *          underlying cause of the error can be retrieved via the
  1876      *          underlying cause of the error can be retrieved via the
  1847      *          {@link Throwable#getCause()} method.
  1877      *          {@link Throwable#getCause()} method.
  1848      *
  1878      *
  1849      * @revised  1.4
  1879      * @revised  1.4
       
  1880      * @revised 9
       
  1881      * @spec JPMS
  1850      */
  1882      */
  1851     @CallerSensitive
  1883     @CallerSensitive
  1852     public static ClassLoader getSystemClassLoader() {
  1884     public static ClassLoader getSystemClassLoader() {
  1853         switch (VM.initLevel()) {
  1885         switch (VM.initLevel()) {
  1854             case 0:
  1886             case 0:
  2099      * @throws  IllegalArgumentException
  2131      * @throws  IllegalArgumentException
  2100      *          if a package of the given {@code name} is already
  2132      *          if a package of the given {@code name} is already
  2101      *          defined by this class loader
  2133      *          defined by this class loader
  2102      *
  2134      *
  2103      * @since  1.2
  2135      * @since  1.2
       
  2136      * @revised 9
       
  2137      * @spec JPMS
  2104      *
  2138      *
  2105      * @see <a href="../../../technotes/guides/jar/jar.html#versioning">
  2139      * @see <a href="../../../technotes/guides/jar/jar.html#versioning">
  2106      *      The JAR File Specification: Package Versioning</a>
  2140      *      The JAR File Specification: Package Versioning</a>
  2107      * @see <a href="../../../technotes/guides/jar/jar.html#sealing">
  2141      * @see <a href="../../../technotes/guides/jar/jar.html#sealing">
  2108      *      The JAR File Specification: Package Sealing</a>
  2142      *      The JAR File Specification: Package Sealing</a>
  2136      *
  2170      *
  2137      * @throws  NullPointerException
  2171      * @throws  NullPointerException
  2138      *          if {@code name} is {@code null}.
  2172      *          if {@code name} is {@code null}.
  2139      *
  2173      *
  2140      * @since  9
  2174      * @since  9
       
  2175      * @spec JPMS
  2141      */
  2176      */
  2142     public final Package getDefinedPackage(String name) {
  2177     public final Package getDefinedPackage(String name) {
  2143         Objects.requireNonNull(name, "name cannot be null");
  2178         Objects.requireNonNull(name, "name cannot be null");
  2144 
  2179 
  2145         NamedPackage p = packages.get(name);
  2180         NamedPackage p = packages.get(name);
  2158      *
  2193      *
  2159      * @return The array of {@code Package} objects defined by this class loader;
  2194      * @return The array of {@code Package} objects defined by this class loader;
  2160      *         or an zero length array if no package has been defined by this class loader.
  2195      *         or an zero length array if no package has been defined by this class loader.
  2161      *
  2196      *
  2162      * @since  9
  2197      * @since  9
       
  2198      * @spec JPMS
  2163      */
  2199      */
  2164     public final Package[] getDefinedPackages() {
  2200     public final Package[] getDefinedPackages() {
  2165         return packages().toArray(Package[]::new);
  2201         return packages().toArray(Package[]::new);
  2166     }
  2202     }
  2167 
  2203 
  2194      * a child loader.  A more robust approach is to use the
  2230      * a child loader.  A more robust approach is to use the
  2195      * {@link ClassLoader#getDefinedPackage} method which returns
  2231      * {@link ClassLoader#getDefinedPackage} method which returns
  2196      * a {@code Package} for the specified class loader.
  2232      * a {@code Package} for the specified class loader.
  2197      *
  2233      *
  2198      * @since  1.2
  2234      * @since  1.2
       
  2235      * @revised 9
       
  2236      * @spec JPMS
  2199      */
  2237      */
  2200     @Deprecated(since="9")
  2238     @Deprecated(since="9")
  2201     protected Package getPackage(String name) {
  2239     protected Package getPackage(String name) {
  2202         Package pkg = getDefinedPackage(name);
  2240         Package pkg = getDefinedPackage(name);
  2203         if (pkg == null) {
  2241         if (pkg == null) {
  2218      *
  2256      *
  2219      * @return  The array of {@code Package} objects defined by this
  2257      * @return  The array of {@code Package} objects defined by this
  2220      *          class loader and its ancestors
  2258      *          class loader and its ancestors
  2221      *
  2259      *
  2222      * @since  1.2
  2260      * @since  1.2
       
  2261      * @revised 9
       
  2262      * @spec JPMS
  2223      */
  2263      */
  2224     protected Package[] getPackages() {
  2264     protected Package[] getPackages() {
  2225         Stream<Package> pkgs = packages();
  2265         Stream<Package> pkgs = packages();
  2226         ClassLoader ld = parent;
  2266         ClassLoader ld = parent;
  2227         while (ld != null) {
  2267         while (ld != null) {