< prev index next >

jdk/src/java.base/share/classes/jdk/internal/loader/ClassLoaders.java

Print this page




 101     /**
 102      * Returns the platform class loader.
 103      */
 104     public static ClassLoader platformClassLoader() {
 105         return PLATFORM_LOADER;
 106     }
 107 
 108     /**
 109      * Returns the application class loader.
 110      */
 111     public static ClassLoader appClassLoader() {
 112         return APP_LOADER;
 113     }
 114 
 115     /**
 116      * The class loader that is used to find resources in modules defined to
 117      * the boot class loader. It is not used for class loading.
 118      */
 119     private static class BootClassLoader extends BuiltinClassLoader {
 120         BootClassLoader(URLClassPath bcp) {
 121             super(null, bcp);
 122         }
 123 
 124         @Override
 125         protected Class<?> loadClassOrNull(String cn) {
 126             return JLA.findBootstrapClassOrNull(this, cn);
 127         }
 128     };
 129 
 130     /**
 131      * The platform class loader, a unique type to make it easier to distinguish
 132      * from the application class loader.
 133      */
 134     private static class PlatformClassLoader extends BuiltinClassLoader {
 135         static {
 136             if (!ClassLoader.registerAsParallelCapable())
 137                 throw new InternalError();
 138         }
 139 
 140         PlatformClassLoader(BootClassLoader parent) {
 141             super(parent, null);
 142         }
 143 
 144         /**
 145          * Called by the VM to support define package for AppCDS.
 146          *
 147          * Shared classes are returned in ClassLoader::findLoadedClass
 148          * that bypass the defineClass call.
 149          */
 150         private Package definePackage(String pn, Module module) {
 151             return JLA.definePackage(this, pn, module);
 152         }
 153     }
 154 
 155     /**
 156      * The application class loader that is a {@code BuiltinClassLoader} with
 157      * customizations to be compatible with long standing behavior.
 158      */
 159     private static class AppClassLoader extends BuiltinClassLoader {
 160         static {
 161             if (!ClassLoader.registerAsParallelCapable())
 162                 throw new InternalError();
 163         }
 164 
 165         final URLClassPath ucp;
 166 
 167         AppClassLoader(PlatformClassLoader parent, URLClassPath ucp) {
 168             super(parent, ucp);
 169             this.ucp = ucp;
 170         }
 171 
 172         @Override
 173         protected Class<?> loadClass(String cn, boolean resolve)
 174             throws ClassNotFoundException
 175         {
 176             // for compatibility reasons, say where restricted package list has
 177             // been updated to list API packages in the unnamed module.
 178             SecurityManager sm = System.getSecurityManager();
 179             if (sm != null) {
 180                 int i = cn.lastIndexOf('.');
 181                 if (i != -1) {
 182                     sm.checkPackageAccess(cn.substring(0, i));
 183                 }
 184             }
 185 
 186             return super.loadClass(cn, resolve);
 187         }
 188 




 101     /**
 102      * Returns the platform class loader.
 103      */
 104     public static ClassLoader platformClassLoader() {
 105         return PLATFORM_LOADER;
 106     }
 107 
 108     /**
 109      * Returns the application class loader.
 110      */
 111     public static ClassLoader appClassLoader() {
 112         return APP_LOADER;
 113     }
 114 
 115     /**
 116      * The class loader that is used to find resources in modules defined to
 117      * the boot class loader. It is not used for class loading.
 118      */
 119     private static class BootClassLoader extends BuiltinClassLoader {
 120         BootClassLoader(URLClassPath bcp) {
 121             super(null, null, bcp);
 122         }
 123 
 124         @Override
 125         protected Class<?> loadClassOrNull(String cn) {
 126             return JLA.findBootstrapClassOrNull(this, cn);
 127         }
 128     };
 129 
 130     /**
 131      * The platform class loader, a unique type to make it easier to distinguish
 132      * from the application class loader.
 133      */
 134     private static class PlatformClassLoader extends BuiltinClassLoader {
 135         static {
 136             if (!ClassLoader.registerAsParallelCapable())
 137                 throw new InternalError();
 138         }
 139 
 140         PlatformClassLoader(BootClassLoader parent) {
 141             super("platform", parent, null);
 142         }
 143 
 144         /**
 145          * Called by the VM to support define package for AppCDS.
 146          *
 147          * Shared classes are returned in ClassLoader::findLoadedClass
 148          * that bypass the defineClass call.
 149          */
 150         private Package definePackage(String pn, Module module) {
 151             return JLA.definePackage(this, pn, module);
 152         }
 153     }
 154 
 155     /**
 156      * The application class loader that is a {@code BuiltinClassLoader} with
 157      * customizations to be compatible with long standing behavior.
 158      */
 159     private static class AppClassLoader extends BuiltinClassLoader {
 160         static {
 161             if (!ClassLoader.registerAsParallelCapable())
 162                 throw new InternalError();
 163         }
 164 
 165         final URLClassPath ucp;
 166 
 167         AppClassLoader(PlatformClassLoader parent, URLClassPath ucp) {
 168             super("app", parent, ucp);
 169             this.ucp = ucp;
 170         }
 171 
 172         @Override
 173         protected Class<?> loadClass(String cn, boolean resolve)
 174             throws ClassNotFoundException
 175         {
 176             // for compatibility reasons, say where restricted package list has
 177             // been updated to list API packages in the unnamed module.
 178             SecurityManager sm = System.getSecurityManager();
 179             if (sm != null) {
 180                 int i = cn.lastIndexOf('.');
 181                 if (i != -1) {
 182                     sm.checkPackageAccess(cn.substring(0, i));
 183                 }
 184             }
 185 
 186             return super.loadClass(cn, resolve);
 187         }
 188 


< prev index next >