205 * or a fully qualified name as defined by
206 * <cite>The Java™ Language Specification</cite>.
207 *
208 * @jls 6.7 Fully Qualified Names
209 * @jls 13.1 The Form of a Binary
210 * @see #resolveClass(Class)
211 * @since 1.0
212 */
213 public abstract class ClassLoader {
214
215 private static native void registerNatives();
216 static {
217 registerNatives();
218 }
219
220 // The parent class loader for delegation
221 // Note: VM hardcoded the offset of this field, thus all new fields
222 // must be added *after* it.
223 private final ClassLoader parent;
224
225 // the unnamed module for this ClassLoader
226 private final Module unnamedModule;
227
228 /**
229 * Encapsulates the set of parallel capable loader types.
230 */
231 private static class ParallelLoaders {
232 private ParallelLoaders() {}
233
234 // the set of parallel capable loader types
235 private static final Set<Class<? extends ClassLoader>> loaderTypes =
236 Collections.newSetFromMap(new WeakHashMap<>());
237 static {
238 synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
239 }
240
241 /**
242 * Registers the given class loader type as parallel capable.
243 * Returns {@code true} is successfully registered; {@code false} if
244 * loader's super class is not registered.
314 NamedPackage p = packages.get(pn);
315 if (p == null) {
316 p = new NamedPackage(pn, m);
317
318 NamedPackage value = packages.putIfAbsent(pn, p);
319 if (value != null) {
320 // Package object already be defined for the named package
321 p = value;
322 // if definePackage is called by this class loader to define
323 // a package in a named module, this will return Package
324 // object of the same name. Package object may contain
325 // unexpected information but it does not impact the runtime.
326 // this assertion may be helpful for troubleshooting
327 assert value.module() == m;
328 }
329 }
330 return p;
331 }
332
333 private static Void checkCreateClassLoader() {
334 SecurityManager security = System.getSecurityManager();
335 if (security != null) {
336 security.checkCreateClassLoader();
337 }
338 return null;
339 }
340
341 private ClassLoader(Void unused, ClassLoader parent) {
342 this.parent = parent;
343 this.unnamedModule
344 = SharedSecrets.getJavaLangReflectModuleAccess()
345 .defineUnnamedModule(this);
346 if (ParallelLoaders.isRegistered(this.getClass())) {
347 parallelLockMap = new ConcurrentHashMap<>();
348 package2certs = new ConcurrentHashMap<>();
349 assertionLock = new Object();
350 } else {
351 // no finer-grained lock; lock on the classloader instance
352 parallelLockMap = null;
353 package2certs = new Hashtable<>();
354 assertionLock = this;
355 }
356 }
357
358 /**
359 * Creates a new class loader using the specified parent class loader for
360 * delegation.
361 *
362 * <p> If there is a security manager, its {@link
363 * SecurityManager#checkCreateClassLoader()
364 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
365 * a security exception. </p>
366 *
367 * @param parent
368 * The parent class loader
369 *
370 * @throws SecurityException
371 * If a security manager exists and its
372 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
373 * of a new class loader.
374 *
375 * @since 1.2
376 */
377 protected ClassLoader(ClassLoader parent) {
378 this(checkCreateClassLoader(), parent);
379 }
380
381 /**
382 * Creates a new class loader using the <tt>ClassLoader</tt> returned by
383 * the method {@link #getSystemClassLoader()
384 * <tt>getSystemClassLoader()</tt>} as the parent class loader.
385 *
386 * <p> If there is a security manager, its {@link
387 * SecurityManager#checkCreateClassLoader()
388 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
389 * a security exception. </p>
390 *
391 * @throws SecurityException
392 * If a security manager exists and its
393 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
394 * of a new class loader.
395 */
396 protected ClassLoader() {
397 this(checkCreateClassLoader(), getSystemClassLoader());
398 }
399
400 // -- Class --
401
402 /**
403 * Loads the class with the specified <a href="#name">binary name</a>.
404 * This method searches for classes in the same manner as the {@link
405 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
406 * machine to resolve class references. Invoking this method is equivalent
407 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
408 * false)</tt>}.
409 *
410 * @param name
411 * The <a href="#name">binary name</a> of the class
412 *
413 * @return The resulting <tt>Class</tt> object
414 *
415 * @throws ClassNotFoundException
416 * If the class was not found
417 */
1611 return parent;
1612 }
1613
1614 /**
1615 * Returns the unnamed {@code Module} for this class loader.
1616 *
1617 * @return The unnamed Module for this class loader
1618 *
1619 * @see Module#isNamed()
1620 * @since 9
1621 */
1622 public final Module getUnnamedModule() {
1623 return unnamedModule;
1624 }
1625
1626 /**
1627 * Returns the platform class loader for delegation. All
1628 * <a href="#builtinLoaders">platform classes</a> are visible to
1629 * the platform class loader.
1630 *
1631 * @return The platform {@code ClassLoader}.
1632 *
1633 * @throws SecurityException
1634 * If a security manager is present, and the caller's class loader is
1635 * not {@code null}, and the caller's class loader is not the same
1636 * as or an ancestor of the platform class loader,
1637 * and the caller does not have the
1638 * {@link RuntimePermission}{@code ("getClassLoader")}
1639 *
1640 * @since 9
1641 */
1642 @CallerSensitive
1643 public static ClassLoader getPlatformClassLoader() {
1644 SecurityManager sm = System.getSecurityManager();
1645 ClassLoader loader = getBuiltinPlatformClassLoader();
1646 if (sm != null) {
1647 checkClassLoaderPermission(loader, Reflection.getCallerClass());
1648 }
1649 return loader;
1650 }
1664 * instance of this class.
1665 *
1666 * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
1667 * when this method is first invoked then the value of that property is
1668 * taken to be the name of a class that will be returned as the system
1669 * class loader. The class is loaded using the default system class loader
1670 * and must define a public constructor that takes a single parameter of
1671 * type <tt>ClassLoader</tt> which is used as the delegation parent. An
1672 * instance is then created using this constructor with the default system
1673 * class loader as the parameter. The resulting class loader is defined
1674 * to be the system class loader. During construction, the class loader
1675 * should take great care to avoid calling {@code getSystemClassLoader()}.
1676 * If circular initialization of the system class loader is detected then
1677 * an unspecified error or exception is thrown.
1678 *
1679 * @implNote The system property to override the system class loader is not
1680 * examined until the VM is almost fully initialized. Code that executes
1681 * this method during startup should take care not to cache the return
1682 * value until the system is fully initialized.
1683 *
1684 * <p> The class path used by the built-in system class loader is determined
1685 * by the system property "{@code java.class.path}" during early
1686 * initialization of the VM. If the system property is not defined,
1687 * or its value is an empty string, then there is no class path
1688 * when the initial module is a module on the application module path,
1689 * i.e. <em>a named module</em>. If the initial module is not on
1690 * the application module path then the class path defaults to
1691 * the current working directory.
1692 *
1693 * @return The system <tt>ClassLoader</tt> for delegation
1694 *
1695 * @throws SecurityException
1696 * If a security manager is present, and the caller's class loader
1697 * is not {@code null} and is not the same as or an ancestor of the
1698 * system class loader, and the caller does not have the
1699 * {@link RuntimePermission}{@code ("getClassLoader")}
1700 *
1701 * @throws IllegalStateException
1702 * If invoked recursively during the construction of the class
1703 * loader specified by the "<tt>java.system.class.loader</tt>"
1704 * property.
|
205 * or a fully qualified name as defined by
206 * <cite>The Java™ Language Specification</cite>.
207 *
208 * @jls 6.7 Fully Qualified Names
209 * @jls 13.1 The Form of a Binary
210 * @see #resolveClass(Class)
211 * @since 1.0
212 */
213 public abstract class ClassLoader {
214
215 private static native void registerNatives();
216 static {
217 registerNatives();
218 }
219
220 // The parent class loader for delegation
221 // Note: VM hardcoded the offset of this field, thus all new fields
222 // must be added *after* it.
223 private final ClassLoader parent;
224
225 // class loader name
226 private final String name;
227
228 // the unnamed module for this ClassLoader
229 private final Module unnamedModule;
230
231 /**
232 * Encapsulates the set of parallel capable loader types.
233 */
234 private static class ParallelLoaders {
235 private ParallelLoaders() {}
236
237 // the set of parallel capable loader types
238 private static final Set<Class<? extends ClassLoader>> loaderTypes =
239 Collections.newSetFromMap(new WeakHashMap<>());
240 static {
241 synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
242 }
243
244 /**
245 * Registers the given class loader type as parallel capable.
246 * Returns {@code true} is successfully registered; {@code false} if
247 * loader's super class is not registered.
317 NamedPackage p = packages.get(pn);
318 if (p == null) {
319 p = new NamedPackage(pn, m);
320
321 NamedPackage value = packages.putIfAbsent(pn, p);
322 if (value != null) {
323 // Package object already be defined for the named package
324 p = value;
325 // if definePackage is called by this class loader to define
326 // a package in a named module, this will return Package
327 // object of the same name. Package object may contain
328 // unexpected information but it does not impact the runtime.
329 // this assertion may be helpful for troubleshooting
330 assert value.module() == m;
331 }
332 }
333 return p;
334 }
335
336 private static Void checkCreateClassLoader() {
337 return checkCreateClassLoader(null);
338 }
339
340 private static Void checkCreateClassLoader(String name) {
341 if (name != null && name.isEmpty()) {
342 throw new IllegalArgumentException("name must be non-empty or null");
343 }
344
345 SecurityManager security = System.getSecurityManager();
346 if (security != null) {
347 security.checkCreateClassLoader();
348 }
349 return null;
350 }
351
352 private ClassLoader(Void unused, String name, ClassLoader parent) {
353 this.name = name;
354 this.parent = parent;
355 this.unnamedModule
356 = SharedSecrets.getJavaLangReflectModuleAccess()
357 .defineUnnamedModule(this);
358 if (ParallelLoaders.isRegistered(this.getClass())) {
359 parallelLockMap = new ConcurrentHashMap<>();
360 package2certs = new ConcurrentHashMap<>();
361 assertionLock = new Object();
362 } else {
363 // no finer-grained lock; lock on the classloader instance
364 parallelLockMap = null;
365 package2certs = new Hashtable<>();
366 assertionLock = this;
367 }
368 }
369
370 /**
371 * Creates a new class loader of the specified name and using the
372 * specified parent class loader for delegation.
373 *
374 * @param name class loader name; or {@code null} if not named
375 * @param parent the parent class loader
376 *
377 * @throws IllegalArgumentException if the given name is empty.
378 *
379 * @throws SecurityException
380 * If a security manager exists and its
381 * {@link SecurityManager#checkCreateClassLoader()}
382 * method doesn't allow creation of a new class loader.
383 *
384 * @since 9
385 */
386 protected ClassLoader(String name, ClassLoader parent) {
387 this(checkCreateClassLoader(name), name, parent);
388 }
389
390
391 /**
392 * Creates a new class loader using the specified parent class loader for
393 * delegation.
394 *
395 * <p> If there is a security manager, its {@link
396 * SecurityManager#checkCreateClassLoader()
397 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
398 * a security exception. </p>
399 *
400 * @param parent
401 * The parent class loader
402 *
403 * @throws SecurityException
404 * If a security manager exists and its
405 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
406 * of a new class loader.
407 *
408 * @since 1.2
409 */
410 protected ClassLoader(ClassLoader parent) {
411 this(checkCreateClassLoader(), null, parent);
412 }
413
414
415 /**
416 * Creates a new class loader using the <tt>ClassLoader</tt> returned by
417 * the method {@link #getSystemClassLoader()
418 * <tt>getSystemClassLoader()</tt>} as the parent class loader.
419 *
420 * <p> If there is a security manager, its {@link
421 * SecurityManager#checkCreateClassLoader()
422 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
423 * a security exception. </p>
424 *
425 * @throws SecurityException
426 * If a security manager exists and its
427 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
428 * of a new class loader.
429 */
430 protected ClassLoader() {
431 this(checkCreateClassLoader(), null, getSystemClassLoader());
432 }
433
434
435 /**
436 * Returns the name of this class loader or {@code null} if
437 * this class loader is not named.
438 *
439 * @apiNote This method is non-final for compatibility. If this
440 * method is overridden, this method must return the same name
441 * as specified when this class loader was instantiated.
442 *
443 * @return name of this class loader; or {@code null} if
444 * this class loader is not named.
445 *
446 * @since 9
447 */
448 public String getName() {
449 return name;
450 }
451
452 // package-private used by StackTraceElement to avoid
453 // calling the overrideable getName method
454 final String name() {
455 return name;
456 }
457
458 // -- Class --
459
460 /**
461 * Loads the class with the specified <a href="#name">binary name</a>.
462 * This method searches for classes in the same manner as the {@link
463 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
464 * machine to resolve class references. Invoking this method is equivalent
465 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
466 * false)</tt>}.
467 *
468 * @param name
469 * The <a href="#name">binary name</a> of the class
470 *
471 * @return The resulting <tt>Class</tt> object
472 *
473 * @throws ClassNotFoundException
474 * If the class was not found
475 */
1669 return parent;
1670 }
1671
1672 /**
1673 * Returns the unnamed {@code Module} for this class loader.
1674 *
1675 * @return The unnamed Module for this class loader
1676 *
1677 * @see Module#isNamed()
1678 * @since 9
1679 */
1680 public final Module getUnnamedModule() {
1681 return unnamedModule;
1682 }
1683
1684 /**
1685 * Returns the platform class loader for delegation. All
1686 * <a href="#builtinLoaders">platform classes</a> are visible to
1687 * the platform class loader.
1688 *
1689 * @implNote The name of the builtin platform class loader is
1690 * {@code "platform"}.
1691 *
1692 * @return The platform {@code ClassLoader}.
1693 *
1694 * @throws SecurityException
1695 * If a security manager is present, and the caller's class loader is
1696 * not {@code null}, and the caller's class loader is not the same
1697 * as or an ancestor of the platform class loader,
1698 * and the caller does not have the
1699 * {@link RuntimePermission}{@code ("getClassLoader")}
1700 *
1701 * @since 9
1702 */
1703 @CallerSensitive
1704 public static ClassLoader getPlatformClassLoader() {
1705 SecurityManager sm = System.getSecurityManager();
1706 ClassLoader loader = getBuiltinPlatformClassLoader();
1707 if (sm != null) {
1708 checkClassLoaderPermission(loader, Reflection.getCallerClass());
1709 }
1710 return loader;
1711 }
1725 * instance of this class.
1726 *
1727 * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
1728 * when this method is first invoked then the value of that property is
1729 * taken to be the name of a class that will be returned as the system
1730 * class loader. The class is loaded using the default system class loader
1731 * and must define a public constructor that takes a single parameter of
1732 * type <tt>ClassLoader</tt> which is used as the delegation parent. An
1733 * instance is then created using this constructor with the default system
1734 * class loader as the parameter. The resulting class loader is defined
1735 * to be the system class loader. During construction, the class loader
1736 * should take great care to avoid calling {@code getSystemClassLoader()}.
1737 * If circular initialization of the system class loader is detected then
1738 * an unspecified error or exception is thrown.
1739 *
1740 * @implNote The system property to override the system class loader is not
1741 * examined until the VM is almost fully initialized. Code that executes
1742 * this method during startup should take care not to cache the return
1743 * value until the system is fully initialized.
1744 *
1745 * <p> The name of the built-in system class loader is {@code "app"}.
1746 * The class path used by the built-in system class loader is determined
1747 * by the system property "{@code java.class.path}" during early
1748 * initialization of the VM. If the system property is not defined,
1749 * or its value is an empty string, then there is no class path
1750 * when the initial module is a module on the application module path,
1751 * i.e. <em>a named module</em>. If the initial module is not on
1752 * the application module path then the class path defaults to
1753 * the current working directory.
1754 *
1755 * @return The system <tt>ClassLoader</tt> for delegation
1756 *
1757 * @throws SecurityException
1758 * If a security manager is present, and the caller's class loader
1759 * is not {@code null} and is not the same as or an ancestor of the
1760 * system class loader, and the caller does not have the
1761 * {@link RuntimePermission}{@code ("getClassLoader")}
1762 *
1763 * @throws IllegalStateException
1764 * If invoked recursively during the construction of the class
1765 * loader specified by the "<tt>java.system.class.loader</tt>"
1766 * property.
|