< prev index next >

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

Print this page




  42 import java.util.Arrays;
  43 import java.util.Collections;
  44 import java.util.Deque;
  45 import java.util.Enumeration;
  46 import java.util.HashMap;
  47 import java.util.HashSet;
  48 import java.util.Hashtable;
  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;
  54 import java.util.Spliterators;
  55 import java.util.Vector;
  56 import java.util.WeakHashMap;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Supplier;
  59 import java.util.stream.Stream;
  60 import java.util.stream.StreamSupport;
  61 

  62 import jdk.internal.perf.PerfCounter;
  63 import jdk.internal.loader.BootLoader;
  64 import jdk.internal.loader.ClassLoaders;
  65 import jdk.internal.misc.Unsafe;
  66 import jdk.internal.misc.VM;
  67 import jdk.internal.ref.CleanerFactory;
  68 import jdk.internal.reflect.CallerSensitive;
  69 import jdk.internal.reflect.Reflection;
  70 import sun.reflect.misc.ReflectUtil;
  71 import sun.security.util.SecurityConstants;
  72 
  73 /**
  74  * A class loader is an object that is responsible for loading classes. The
  75  * class {@code ClassLoader} is an abstract class.  Given the <a
  76  * href="#binary-name">binary name</a> of a class, a class loader should attempt to
  77  * locate or generate data that constitutes a definition for the class.  A
  78  * typical strategy is to transform the name into a file name and then read a
  79  * "class file" of that name from a file system.
  80  *
  81  * <p> Every {@link java.lang.Class Class} object contains a {@link


 229  * @spec JPMS
 230  */
 231 public abstract class ClassLoader {
 232 
 233     private static native void registerNatives();
 234     static {
 235         registerNatives();
 236     }
 237 
 238     // The parent class loader for delegation
 239     // Note: VM hardcoded the offset of this field, thus all new fields
 240     // must be added *after* it.
 241     private final ClassLoader parent;
 242 
 243     // class loader name
 244     private final String name;
 245 
 246     // the unnamed module for this ClassLoader
 247     private final Module unnamedModule;
 248 



 249     /**
 250      * Encapsulates the set of parallel capable loader types.
 251      */
 252     private static class ParallelLoaders {
 253         private ParallelLoaders() {}
 254 
 255         // the set of parallel capable loader types
 256         private static final Set<Class<? extends ClassLoader>> loaderTypes =
 257             Collections.newSetFromMap(new WeakHashMap<>());
 258         static {
 259             synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
 260         }
 261 
 262         /**
 263          * Registers the given class loader type as parallel capable.
 264          * Returns {@code true} is successfully registered; {@code false} if
 265          * loader's super class is not registered.
 266          */
 267         static boolean register(Class<? extends ClassLoader> c) {
 268             synchronized (loaderTypes) {


 364         if (security != null) {
 365             security.checkCreateClassLoader();
 366         }
 367         return null;
 368     }
 369 
 370     private ClassLoader(Void unused, String name, ClassLoader parent) {
 371         this.name = name;
 372         this.parent = parent;
 373         this.unnamedModule = new Module(this);
 374         if (ParallelLoaders.isRegistered(this.getClass())) {
 375             parallelLockMap = new ConcurrentHashMap<>();
 376             package2certs = new ConcurrentHashMap<>();
 377             assertionLock = new Object();
 378         } else {
 379             // no finer-grained lock; lock on the classloader instance
 380             parallelLockMap = null;
 381             package2certs = new Hashtable<>();
 382             assertionLock = this;
 383         }


















 384     }
 385 
 386     /**
 387      * Creates a new class loader of the specified name and using the
 388      * specified parent class loader for delegation.
 389      *
 390      * @apiNote If the parent is specified as {@code null} (for the
 391      * bootstrap class loader) then there is no guarantee that all platform
 392      * classes are visible.
 393      *
 394      * @param  name   class loader name; or {@code null} if not named
 395      * @param  parent the parent class loader
 396      *
 397      * @throws IllegalArgumentException if the given name is empty.
 398      *
 399      * @throws SecurityException
 400      *         If a security manager exists and its
 401      *         {@link SecurityManager#checkCreateClassLoader()}
 402      *         method doesn't allow creation of a new class loader.
 403      *




  42 import java.util.Arrays;
  43 import java.util.Collections;
  44 import java.util.Deque;
  45 import java.util.Enumeration;
  46 import java.util.HashMap;
  47 import java.util.HashSet;
  48 import java.util.Hashtable;
  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;
  54 import java.util.Spliterators;
  55 import java.util.Vector;
  56 import java.util.WeakHashMap;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import java.util.function.Supplier;
  59 import java.util.stream.Stream;
  60 import java.util.stream.StreamSupport;
  61 
  62 import jdk.internal.loader.BuiltinClassLoader;
  63 import jdk.internal.perf.PerfCounter;
  64 import jdk.internal.loader.BootLoader;
  65 import jdk.internal.loader.ClassLoaders;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.misc.VM;
  68 import jdk.internal.ref.CleanerFactory;
  69 import jdk.internal.reflect.CallerSensitive;
  70 import jdk.internal.reflect.Reflection;
  71 import sun.reflect.misc.ReflectUtil;
  72 import sun.security.util.SecurityConstants;
  73 
  74 /**
  75  * A class loader is an object that is responsible for loading classes. The
  76  * class {@code ClassLoader} is an abstract class.  Given the <a
  77  * href="#binary-name">binary name</a> of a class, a class loader should attempt to
  78  * locate or generate data that constitutes a definition for the class.  A
  79  * typical strategy is to transform the name into a file name and then read a
  80  * "class file" of that name from a file system.
  81  *
  82  * <p> Every {@link java.lang.Class Class} object contains a {@link


 230  * @spec JPMS
 231  */
 232 public abstract class ClassLoader {
 233 
 234     private static native void registerNatives();
 235     static {
 236         registerNatives();
 237     }
 238 
 239     // The parent class loader for delegation
 240     // Note: VM hardcoded the offset of this field, thus all new fields
 241     // must be added *after* it.
 242     private final ClassLoader parent;
 243 
 244     // class loader name
 245     private final String name;
 246 
 247     // the unnamed module for this ClassLoader
 248     private final Module unnamedModule;
 249 
 250     // a string for exception message printing
 251     private final String nameAndId;
 252 
 253     /**
 254      * Encapsulates the set of parallel capable loader types.
 255      */
 256     private static class ParallelLoaders {
 257         private ParallelLoaders() {}
 258 
 259         // the set of parallel capable loader types
 260         private static final Set<Class<? extends ClassLoader>> loaderTypes =
 261             Collections.newSetFromMap(new WeakHashMap<>());
 262         static {
 263             synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
 264         }
 265 
 266         /**
 267          * Registers the given class loader type as parallel capable.
 268          * Returns {@code true} is successfully registered; {@code false} if
 269          * loader's super class is not registered.
 270          */
 271         static boolean register(Class<? extends ClassLoader> c) {
 272             synchronized (loaderTypes) {


 368         if (security != null) {
 369             security.checkCreateClassLoader();
 370         }
 371         return null;
 372     }
 373 
 374     private ClassLoader(Void unused, String name, ClassLoader parent) {
 375         this.name = name;
 376         this.parent = parent;
 377         this.unnamedModule = new Module(this);
 378         if (ParallelLoaders.isRegistered(this.getClass())) {
 379             parallelLockMap = new ConcurrentHashMap<>();
 380             package2certs = new ConcurrentHashMap<>();
 381             assertionLock = new Object();
 382         } else {
 383             // no finer-grained lock; lock on the classloader instance
 384             parallelLockMap = null;
 385             package2certs = new Hashtable<>();
 386             assertionLock = this;
 387         }
 388         this.nameAndId = nameAndId(this);
 389     }
 390 
 391     /**
 392      * If the defining loader has a name explicitly set then
 393      *       '<loader-name>' @<id>
 394      * If the defining loader has no name then
 395      *       <qualified-class-name> @<id>
 396      * If it's built-in loader then omit `@<id>` as there is only one instance.
 397      */
 398     private static String nameAndId(ClassLoader ld) {
 399         String nid = ld.getName() != null ? "\'" + ld.getName() + "\'"
 400                                           : ld.getClass().getName();
 401         if (!(ld instanceof BuiltinClassLoader)) {
 402             String id = Integer.toHexString(System.identityHashCode(ld));
 403             nid = nid + " @" + id;
 404         }
 405         return nid;
 406     }
 407 
 408     /**
 409      * Creates a new class loader of the specified name and using the
 410      * specified parent class loader for delegation.
 411      *
 412      * @apiNote If the parent is specified as {@code null} (for the
 413      * bootstrap class loader) then there is no guarantee that all platform
 414      * classes are visible.
 415      *
 416      * @param  name   class loader name; or {@code null} if not named
 417      * @param  parent the parent class loader
 418      *
 419      * @throws IllegalArgumentException if the given name is empty.
 420      *
 421      * @throws SecurityException
 422      *         If a security manager exists and its
 423      *         {@link SecurityManager#checkCreateClassLoader()}
 424      *         method doesn't allow creation of a new class loader.
 425      *


< prev index next >