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.
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.
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 SecurityManager security = System.getSecurityManager();
338 if (security != null) {
339 security.checkCreateClassLoader();
340 }
341 return null;
342 }
343
344 private ClassLoader(Void unused, String name, ClassLoader parent) {
345 if (name != null && name.isEmpty()) {
346 throw new IllegalArgumentException("name must be non-empty or null");
347 }
348 this.name = name;
349 this.parent = parent;
350 this.unnamedModule
351 = SharedSecrets.getJavaLangReflectModuleAccess()
352 .defineUnnamedModule(this);
353 if (ParallelLoaders.isRegistered(this.getClass())) {
354 parallelLockMap = new ConcurrentHashMap<>();
355 package2certs = new ConcurrentHashMap<>();
356 assertionLock = new Object();
357 } else {
358 // no finer-grained lock; lock on the classloader instance
359 parallelLockMap = null;
360 package2certs = new Hashtable<>();
361 assertionLock = this;
362 }
363 }
364
365 /**
366 * Creates a new class loader of the specified name and using the
367 * specified parent class loader for delegation.
368 *
369 * @param name class loader name; or {@code null} if not named
370 * @param parent the parent class loader
371 *
372 * @throws IllegalArgumentException if the given name is empty.
373 *
374 * @throws SecurityException
375 * If a security manager exists and its
376 * {@link SecurityManager#checkCreateClassLoader()}
377 * method doesn't allow creation of a new class loader.
378 *
379 * @since 9
380 */
381 protected ClassLoader(String name, ClassLoader parent) {
382 this(checkCreateClassLoader(), name, parent);
383 }
384
385
386 /**
387 * Creates a new class loader using the specified parent class loader for
388 * delegation.
389 *
390 * <p> If there is a security manager, its {@link
391 * SecurityManager#checkCreateClassLoader()
392 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
393 * a security exception. </p>
394 *
395 * @param parent
396 * The parent class loader
397 *
398 * @throws SecurityException
399 * If a security manager exists and its
400 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
401 * of a new class loader.
402 *
403 * @since 1.2
404 */
405 protected ClassLoader(ClassLoader parent) {
406 this(checkCreateClassLoader(), null, parent);
407 }
408
409
410 /**
411 * Creates a new class loader using the <tt>ClassLoader</tt> returned by
412 * the method {@link #getSystemClassLoader()
413 * <tt>getSystemClassLoader()</tt>} as the parent class loader.
414 *
415 * <p> If there is a security manager, its {@link
416 * SecurityManager#checkCreateClassLoader()
417 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
418 * a security exception. </p>
419 *
420 * @throws SecurityException
421 * If a security manager exists and its
422 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
423 * of a new class loader.
424 */
425 protected ClassLoader() {
426 this(checkCreateClassLoader(), null, getSystemClassLoader());
427 }
428
429
430 /**
431 * Returns the name of this class loader or {@code null} if
432 * this class loader is not named.
433 *
434 * @apiNote This method is non-final for compatibility. If this
435 * method is overridden, this method must return the same name
436 * as specified when this class loader was instantiated.
437 *
438 * @return name of this class loader; or {@code null} if
439 * this class loader is not named.
440 *
441 * @since 9
442 */
443 public String getName() {
444 return name;
445 }
446
447 // package-private used by StackTraceElement to avoid
448 // calling the overrideable getName method
449 final String name() {
450 return name;
451 }
452
453 // -- Class --
454
455 /**
456 * Loads the class with the specified <a href="#name">binary name</a>.
457 * This method searches for classes in the same manner as the {@link
458 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
459 * machine to resolve class references. Invoking this method is equivalent
460 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
461 * false)</tt>}.
462 *
463 * @param name
464 * The <a href="#name">binary name</a> of the class
465 *
466 * @return The resulting <tt>Class</tt> object
467 *
468 * @throws ClassNotFoundException
469 * If the class was not found
470 */
1664 return parent;
1665 }
1666
1667 /**
1668 * Returns the unnamed {@code Module} for this class loader.
1669 *
1670 * @return The unnamed Module for this class loader
1671 *
1672 * @see Module#isNamed()
1673 * @since 9
1674 */
1675 public final Module getUnnamedModule() {
1676 return unnamedModule;
1677 }
1678
1679 /**
1680 * Returns the platform class loader for delegation. All
1681 * <a href="#builtinLoaders">platform classes</a> are visible to
1682 * the platform class loader.
1683 *
1684 * @implNote The name of the builtin platform class loader is
1685 * {@code "platform"}.
1686 *
1687 * @return The platform {@code ClassLoader}.
1688 *
1689 * @throws SecurityException
1690 * If a security manager is present, and the caller's class loader is
1691 * not {@code null}, and the caller's class loader is not the same
1692 * as or an ancestor of the platform class loader,
1693 * and the caller does not have the
1694 * {@link RuntimePermission}{@code ("getClassLoader")}
1695 *
1696 * @since 9
1697 */
1698 @CallerSensitive
1699 public static ClassLoader getPlatformClassLoader() {
1700 SecurityManager sm = System.getSecurityManager();
1701 ClassLoader loader = getBuiltinPlatformClassLoader();
1702 if (sm != null) {
1703 checkClassLoaderPermission(loader, Reflection.getCallerClass());
1704 }
1705 return loader;
1706 }
1720 * instance of this class.
1721 *
1722 * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
1723 * when this method is first invoked then the value of that property is
1724 * taken to be the name of a class that will be returned as the system
1725 * class loader. The class is loaded using the default system class loader
1726 * and must define a public constructor that takes a single parameter of
1727 * type <tt>ClassLoader</tt> which is used as the delegation parent. An
1728 * instance is then created using this constructor with the default system
1729 * class loader as the parameter. The resulting class loader is defined
1730 * to be the system class loader. During construction, the class loader
1731 * should take great care to avoid calling {@code getSystemClassLoader()}.
1732 * If circular initialization of the system class loader is detected then
1733 * an unspecified error or exception is thrown.
1734 *
1735 * @implNote The system property to override the system class loader is not
1736 * examined until the VM is almost fully initialized. Code that executes
1737 * this method during startup should take care not to cache the return
1738 * value until the system is fully initialized.
1739 *
1740 * <p> The name of the built-in system class loader is {@code "app"}.
1741 * The class path used by the built-in system class loader is determined
1742 * by the system property "{@code java.class.path}" during early
1743 * initialization of the VM. If the system property is not defined,
1744 * or its value is an empty string, then there is no class path
1745 * when the initial module is a module on the application module path,
1746 * i.e. <em>a named module</em>. If the initial module is not on
1747 * the application module path then the class path defaults to
1748 * the current working directory.
1749 *
1750 * @return The system <tt>ClassLoader</tt> for delegation
1751 *
1752 * @throws SecurityException
1753 * If a security manager is present, and the caller's class loader
1754 * is not {@code null} and is not the same as or an ancestor of the
1755 * system class loader, and the caller does not have the
1756 * {@link RuntimePermission}{@code ("getClassLoader")}
1757 *
1758 * @throws IllegalStateException
1759 * If invoked recursively during the construction of the class
1760 * loader specified by the "<tt>java.system.class.loader</tt>"
1761 * property.
|