< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page




1796             // permission check is performed.
1797             checkClassLoaderPermission(parent, Reflection.getCallerClass());
1798         }
1799         return parent;
1800     }
1801 
1802     /**
1803      * Returns the unnamed {@code Module} for this class loader.
1804      *
1805      * @return The unnamed Module for this class loader
1806      *
1807      * @see Module#isNamed()
1808      * @since 9
1809      * @spec JPMS
1810      */
1811     public final Module getUnnamedModule() {
1812         return unnamedModule;
1813     }
1814 
1815     /**
1816      * Returns the platform class loader for delegation.  All
1817      * <a href="#builtinLoaders">platform classes</a> are visible to
1818      * the platform class loader.
1819      *
1820      * @implNote The name of the builtin platform class loader is
1821      * {@code "platform"}.
1822      *
1823      * @return  The platform {@code ClassLoader}.
1824      *
1825      * @throws  SecurityException
1826      *          If a security manager is present, and the caller's class loader is
1827      *          not {@code null}, and the caller's class loader is not the same
1828      *          as or an ancestor of the platform class loader,
1829      *          and the caller does not have the
1830      *          {@link RuntimePermission}{@code ("getClassLoader")}
1831      *
1832      * @since 9
1833      * @spec JPMS
1834      */
1835     @CallerSensitive
1836     public static ClassLoader getPlatformClassLoader() {
1837         SecurityManager sm = System.getSecurityManager();
1838         ClassLoader loader = getBuiltinPlatformClassLoader();
1839         if (sm != null) {
1840             checkClassLoaderPermission(loader, Reflection.getCallerClass());
1841         }
1842         return loader;
1843     }
1844 
1845     /**
1846      * Returns the system class loader for delegation.  This is the default
1847      * delegation parent for new {@code ClassLoader} instances, and is
1848      * typically the class loader used to start the application.
1849      *
1850      * <p> This method is first invoked early in the runtime's startup
1851      * sequence, at which point it creates the system class loader. This
1852      * class loader will be the context class loader for the main application
1853      * thread (for example, the thread that invokes the {@code main} method of
1854      * the main class).
1855      *
1856      * <p> The default system class loader is an implementation-dependent
1857      * instance of this class.
1858      *
1859      * <p> If the system property "{@code java.system.class.loader}" is defined
1860      * when this method is first invoked then the value of that property is
1861      * taken to be the name of a class that will be returned as the system
1862      * class loader.  The class is loaded using the default system class loader
1863      * and must define a public constructor that takes a single parameter of
1864      * type {@code ClassLoader} which is used as the delegation parent.  An
1865      * instance is then created using this constructor with the default system
1866      * class loader as the parameter.  The resulting class loader is defined
1867      * to be the system class loader. During construction, the class loader
1868      * should take great care to avoid calling {@code getSystemClassLoader()}.
1869      * If circular initialization of the system class loader is detected then
1870      * an unspecified error or exception is thrown.
1871      *
1872      * @implNote The system property to override the system class loader is not
1873      * examined until the VM is almost fully initialized. Code that executes
1874      * this method during startup should take care not to cache the return
1875      * value until the system is fully initialized.
1876      *
1877      * <p> The name of the built-in system class loader is {@code "app"}.
1878      * The class path used by the built-in system class loader is determined
1879      * by the system property "{@code java.class.path}" during early
1880      * initialization of the VM. If the system property is not defined,
1881      * or its value is an empty string, then there is no class path
1882      * when the initial module is a module on the application module path,
1883      * i.e. <em>a named module</em>. If the initial module is not on
1884      * the application module path then the class path defaults to
1885      * the current working directory.
1886      *
1887      * @return  The system {@code ClassLoader} for delegation
1888      *
1889      * @throws  SecurityException
1890      *          If a security manager is present, and the caller's class loader
1891      *          is not {@code null} and is not the same as or an ancestor of the
1892      *          system class loader, and the caller does not have the
1893      *          {@link RuntimePermission}{@code ("getClassLoader")}
1894      *
1895      * @throws  IllegalStateException
1896      *          If invoked recursively during the construction of the class
1897      *          loader specified by the "{@code java.system.class.loader}"
1898      *          property.
1899      *
1900      * @throws  Error
1901      *          If the system property "{@code java.system.class.loader}"
1902      *          is defined but the named class could not be loaded, the
1903      *          provider class does not define the required constructor, or an
1904      *          exception is thrown by that constructor when it is invoked. The
1905      *          underlying cause of the error can be retrieved via the
1906      *          {@link Throwable#getCause()} method.
1907      *




1796             // permission check is performed.
1797             checkClassLoaderPermission(parent, Reflection.getCallerClass());
1798         }
1799         return parent;
1800     }
1801 
1802     /**
1803      * Returns the unnamed {@code Module} for this class loader.
1804      *
1805      * @return The unnamed Module for this class loader
1806      *
1807      * @see Module#isNamed()
1808      * @since 9
1809      * @spec JPMS
1810      */
1811     public final Module getUnnamedModule() {
1812         return unnamedModule;
1813     }
1814 
1815     /**
1816      * Returns the platform class loader.  All
1817      * <a href="#builtinLoaders">platform classes</a> are visible to
1818      * the platform class loader.
1819      *
1820      * @implNote The name of the builtin platform class loader is
1821      * {@code "platform"}.
1822      *
1823      * @return  The platform {@code ClassLoader}.
1824      *
1825      * @throws  SecurityException
1826      *          If a security manager is present, and the caller's class loader is
1827      *          not {@code null}, and the caller's class loader is not the same
1828      *          as or an ancestor of the platform class loader,
1829      *          and the caller does not have the
1830      *          {@link RuntimePermission}{@code ("getClassLoader")}
1831      *
1832      * @since 9
1833      * @spec JPMS
1834      */
1835     @CallerSensitive
1836     public static ClassLoader getPlatformClassLoader() {
1837         SecurityManager sm = System.getSecurityManager();
1838         ClassLoader loader = getBuiltinPlatformClassLoader();
1839         if (sm != null) {
1840             checkClassLoaderPermission(loader, Reflection.getCallerClass());
1841         }
1842         return loader;
1843     }
1844 
1845     /**
1846      * Returns the system class loader.  This is the default
1847      * delegation parent for new {@code ClassLoader} instances, and is
1848      * typically the class loader used to start the application.
1849      *
1850      * <p> This method is first invoked early in the runtime's startup
1851      * sequence, at which point it creates the system class loader. This
1852      * class loader will be the context class loader for the main application
1853      * thread (for example, the thread that invokes the {@code main} method of
1854      * the main class).
1855      *
1856      * <p> The default system class loader is an implementation-dependent
1857      * instance of this class.
1858      *
1859      * <p> If the system property "{@code java.system.class.loader}" is defined
1860      * when this method is first invoked then the value of that property is
1861      * taken to be the name of a class that will be returned as the system
1862      * class loader.  The class is loaded using the default system class loader
1863      * and must define a public constructor that takes a single parameter of
1864      * type {@code ClassLoader} which is used as the delegation parent.  An
1865      * instance is then created using this constructor with the default system
1866      * class loader as the parameter.  The resulting class loader is defined
1867      * to be the system class loader. During construction, the class loader
1868      * should take great care to avoid calling {@code getSystemClassLoader()}.
1869      * If circular initialization of the system class loader is detected then
1870      * an unspecified error or exception is thrown.
1871      *
1872      * @implNote The system property to override the system class loader is not
1873      * examined until the VM is almost fully initialized. Code that executes
1874      * this method during startup should take care not to cache the return
1875      * value until the system is fully initialized.
1876      *
1877      * <p> The name of the built-in system class loader is {@code "app"}.
1878      * The class path used by the built-in system class loader is determined
1879      * by the system property "{@code java.class.path}" during early
1880      * initialization of the VM. If the system property is not defined,
1881      * or its value is an empty string, then there is no class path
1882      * when the initial module is a module on the application module path,
1883      * i.e. <em>a named module</em>. If the initial module is not on
1884      * the application module path then the class path defaults to
1885      * the current working directory.
1886      *
1887      * @return  The system {@code ClassLoader}
1888      *
1889      * @throws  SecurityException
1890      *          If a security manager is present, and the caller's class loader
1891      *          is not {@code null} and is not the same as or an ancestor of the
1892      *          system class loader, and the caller does not have the
1893      *          {@link RuntimePermission}{@code ("getClassLoader")}
1894      *
1895      * @throws  IllegalStateException
1896      *          If invoked recursively during the construction of the class
1897      *          loader specified by the "{@code java.system.class.loader}"
1898      *          property.
1899      *
1900      * @throws  Error
1901      *          If the system property "{@code java.system.class.loader}"
1902      *          is defined but the named class could not be loaded, the
1903      *          provider class does not define the required constructor, or an
1904      *          exception is thrown by that constructor when it is invoked. The
1905      *          underlying cause of the error can be retrieved via the
1906      *          {@link Throwable#getCause()} method.
1907      *


< prev index next >