13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.InputStream;
29 import java.io.IOException;
30 import java.io.UncheckedIOException;
31 import java.io.File;
32 import java.lang.reflect.Constructor;
33 import java.net.URL;
34 import java.security.AccessController;
35 import java.security.AccessControlContext;
36 import java.security.CodeSource;
37 import java.security.PrivilegedAction;
38 import java.security.ProtectionDomain;
39 import java.security.cert.Certificate;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.Deque;
43 import java.util.Enumeration;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Hashtable;
47 import java.util.LinkedList;
48 import java.util.Map;
49 import java.util.NoSuchElementException;
50 import java.util.Objects;
51 import java.util.Set;
52 import java.util.Spliterator;
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
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 *
1908 * @revised 1.4
1909 * @revised 9
1910 * @spec JPMS
1911 */
1912 @CallerSensitive
1913 public static ClassLoader getSystemClassLoader() {
1914 switch (VM.initLevel()) {
1915 case 0:
1916 case 1:
1917 case 2:
1918 // the system class loader is the built-in app class loader during startup
1919 return getBuiltinAppClassLoader();
1920 case 3:
1921 String msg = "getSystemClassLoader should only be called after VM booted";
1922 throw new InternalError(msg);
1923 case 4:
1924 // system fully initialized
1925 assert VM.isBooted() && scl != null;
1926 SecurityManager sm = System.getSecurityManager();
1927 if (sm != null) {
1928 checkClassLoaderPermission(scl, Reflection.getCallerClass());
1929 }
1930 return scl;
1931 default:
1932 throw new InternalError("should not reach here");
1933 }
1934 }
1935
1936 static ClassLoader getBuiltinPlatformClassLoader() {
1937 return ClassLoaders.platformClassLoader();
1938 }
1939
1940 static ClassLoader getBuiltinAppClassLoader() {
1941 return ClassLoaders.appClassLoader();
1942 }
1952 throw new InternalError("system class loader cannot be set at initLevel " +
1953 VM.initLevel());
1954 }
1955
1956 // detect recursive initialization
1957 if (scl != null) {
1958 throw new IllegalStateException("recursive invocation");
1959 }
1960
1961 ClassLoader builtinLoader = getBuiltinAppClassLoader();
1962
1963 // All are privileged frames. No need to call doPrivileged.
1964 String cn = System.getProperty("java.system.class.loader");
1965 if (cn != null) {
1966 try {
1967 // custom class loader is only supported to be loaded from unnamed module
1968 Constructor<?> ctor = Class.forName(cn, false, builtinLoader)
1969 .getDeclaredConstructor(ClassLoader.class);
1970 scl = (ClassLoader) ctor.newInstance(builtinLoader);
1971 } catch (Exception e) {
1972 throw new Error(e);
1973 }
1974 } else {
1975 scl = builtinLoader;
1976 }
1977 return scl;
1978 }
1979
1980 // Returns true if the specified class loader can be found in this class
1981 // loader's delegation chain.
1982 boolean isAncestor(ClassLoader cl) {
1983 ClassLoader acl = this;
1984 do {
1985 acl = acl.parent;
1986 if (cl == acl) {
1987 return true;
1988 }
1989 } while (acl != null);
1990 return false;
1991 }
1992
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.io.InputStream;
29 import java.io.IOException;
30 import java.io.UncheckedIOException;
31 import java.io.File;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.InvocationTargetException;
34 import java.net.URL;
35 import java.security.AccessController;
36 import java.security.AccessControlContext;
37 import java.security.CodeSource;
38 import java.security.PrivilegedAction;
39 import java.security.ProtectionDomain;
40 import java.security.cert.Certificate;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.Deque;
44 import java.util.Enumeration;
45 import java.util.HashMap;
46 import java.util.HashSet;
47 import java.util.Hashtable;
48 import java.util.LinkedList;
49 import java.util.Map;
50 import java.util.NoSuchElementException;
51 import java.util.Objects;
52 import java.util.Set;
53 import java.util.Spliterator;
1851 * <p> This method is first invoked early in the runtime's startup
1852 * sequence, at which point it creates the system class loader. This
1853 * class loader will be the context class loader for the main application
1854 * thread (for example, the thread that invokes the {@code main} method of
1855 * the main class).
1856 *
1857 * <p> The default system class loader is an implementation-dependent
1858 * instance of this class.
1859 *
1860 * <p> If the system property "{@code java.system.class.loader}" is defined
1861 * when this method is first invoked then the value of that property is
1862 * taken to be the name of a class that will be returned as the system
1863 * class loader. The class is loaded using the default system class loader
1864 * and must define a public constructor that takes a single parameter of
1865 * type {@code ClassLoader} which is used as the delegation parent. An
1866 * instance is then created using this constructor with the default system
1867 * class loader as the parameter. The resulting class loader is defined
1868 * to be the system class loader. During construction, the class loader
1869 * should take great care to avoid calling {@code getSystemClassLoader()}.
1870 * If circular initialization of the system class loader is detected then
1871 * an {@code IllegalStateException} is thrown.
1872 *
1873 * @implNote The system property to override the system class loader is not
1874 * examined until the VM is almost fully initialized. Code that executes
1875 * this method during startup should take care not to cache the return
1876 * value until the system is fully initialized.
1877 *
1878 * <p> The name of the built-in system class loader is {@code "app"}.
1879 * The class path used by the built-in system class loader is determined
1880 * by the system property "{@code java.class.path}" during early
1881 * initialization of the VM. If the system property is not defined,
1882 * or its value is an empty string, then there is no class path
1883 * when the initial module is a module on the application module path,
1884 * i.e. <em>a named module</em>. If the initial module is not on
1885 * the application module path then the class path defaults to
1886 * the current working directory.
1887 *
1888 * @return The system {@code ClassLoader}
1889 *
1890 * @throws SecurityException
1891 * If a security manager is present, and the caller's class loader
1902 * If the system property "{@code java.system.class.loader}"
1903 * is defined but the named class could not be loaded, the
1904 * provider class does not define the required constructor, or an
1905 * exception is thrown by that constructor when it is invoked. The
1906 * underlying cause of the error can be retrieved via the
1907 * {@link Throwable#getCause()} method.
1908 *
1909 * @revised 1.4
1910 * @revised 9
1911 * @spec JPMS
1912 */
1913 @CallerSensitive
1914 public static ClassLoader getSystemClassLoader() {
1915 switch (VM.initLevel()) {
1916 case 0:
1917 case 1:
1918 case 2:
1919 // the system class loader is the built-in app class loader during startup
1920 return getBuiltinAppClassLoader();
1921 case 3:
1922 String msg = "getSystemClassLoader cannot be called during the system class loader instantiation";
1923 throw new IllegalStateException(msg);
1924 case 4:
1925 // system fully initialized
1926 assert VM.isBooted() && scl != null;
1927 SecurityManager sm = System.getSecurityManager();
1928 if (sm != null) {
1929 checkClassLoaderPermission(scl, Reflection.getCallerClass());
1930 }
1931 return scl;
1932 default:
1933 throw new InternalError("should not reach here");
1934 }
1935 }
1936
1937 static ClassLoader getBuiltinPlatformClassLoader() {
1938 return ClassLoaders.platformClassLoader();
1939 }
1940
1941 static ClassLoader getBuiltinAppClassLoader() {
1942 return ClassLoaders.appClassLoader();
1943 }
1953 throw new InternalError("system class loader cannot be set at initLevel " +
1954 VM.initLevel());
1955 }
1956
1957 // detect recursive initialization
1958 if (scl != null) {
1959 throw new IllegalStateException("recursive invocation");
1960 }
1961
1962 ClassLoader builtinLoader = getBuiltinAppClassLoader();
1963
1964 // All are privileged frames. No need to call doPrivileged.
1965 String cn = System.getProperty("java.system.class.loader");
1966 if (cn != null) {
1967 try {
1968 // custom class loader is only supported to be loaded from unnamed module
1969 Constructor<?> ctor = Class.forName(cn, false, builtinLoader)
1970 .getDeclaredConstructor(ClassLoader.class);
1971 scl = (ClassLoader) ctor.newInstance(builtinLoader);
1972 } catch (Exception e) {
1973 Throwable cause = e;
1974 if (e instanceof InvocationTargetException) {
1975 cause = e.getCause();
1976 if (cause instanceof RuntimeException) {
1977 throw (RuntimeException) cause;
1978 }
1979 }
1980 throw new Error(cause.getMessage(), cause);
1981 }
1982 } else {
1983 scl = builtinLoader;
1984 }
1985 return scl;
1986 }
1987
1988 // Returns true if the specified class loader can be found in this class
1989 // loader's delegation chain.
1990 boolean isAncestor(ClassLoader cl) {
1991 ClassLoader acl = this;
1992 do {
1993 acl = acl.parent;
1994 if (cl == acl) {
1995 return true;
1996 }
1997 } while (acl != null);
1998 return false;
1999 }
2000
|