140 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
141 * create a class instance. A sample implementation is:
142 *
143 * <blockquote><pre>
144 * class NetworkClassLoader extends ClassLoader {
145 * String host;
146 * int port;
147 *
148 * public Class findClass(String name) {
149 * byte[] b = loadClassData(name);
150 * return defineClass(name, b, 0, b.length);
151 * }
152 *
153 * private byte[] loadClassData(String name) {
154 * // load the class data from the connection
155 * . . .
156 * }
157 * }
158 * </pre></blockquote>
159 *
160 * <h4> <a name="name">Binary names</a> </h4>
161 *
162 * <p> Any class name provided as a {@link String} parameter to methods in
163 * <tt>ClassLoader</tt> must be a binary name as defined by
164 * <cite>The Java™ Language Specification</cite>.
165 *
166 * <p> Examples of valid class names include:
167 * <blockquote><pre>
168 * "java.lang.String"
169 * "javax.swing.JSpinner$DefaultEditor"
170 * "java.security.KeyStore$Builder$FileBuilder$1"
171 * "java.net.URLClassLoader$3$1"
172 * </pre></blockquote>
173 *
174 * @see #resolveClass(Class)
175 * @since 1.0
176 */
177 public abstract class ClassLoader {
178
179 private static native void registerNatives();
180 static {
325 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
326 * a security exception. </p>
327 *
328 * @throws SecurityException
329 * If a security manager exists and its
330 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
331 * of a new class loader.
332 */
333 protected ClassLoader() {
334 this(checkCreateClassLoader(), getSystemClassLoader());
335 }
336
337 // -- Class --
338
339 /**
340 * Loads the class with the specified <a href="#name">binary name</a>.
341 * This method searches for classes in the same manner as the {@link
342 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
343 * machine to resolve class references. Invoking this method is equivalent
344 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
345 * false)</tt>}. </p>
346 *
347 * @param name
348 * The <a href="#name">binary name</a> of the class
349 *
350 * @return The resulting <tt>Class</tt> object
351 *
352 * @throws ClassNotFoundException
353 * If the class was not found
354 */
355 public Class<?> loadClass(String name) throws ClassNotFoundException {
356 return loadClass(name, false);
357 }
358
359 /**
360 * Loads the class with the specified <a href="#name">binary name</a>. The
361 * default implementation of this method searches for classes in the
362 * following order:
363 *
364 * <p><ol>
365 *
424
425 // this is the defining class loader; record the stats
426 sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
427 sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
428 sun.misc.PerfCounter.getFindClasses().increment();
429 }
430 }
431 if (resolve) {
432 resolveClass(c);
433 }
434 return c;
435 }
436 }
437
438 /**
439 * Returns the lock object for class loading operations.
440 * For backward compatibility, the default implementation of this method
441 * behaves as follows. If this ClassLoader object is registered as
442 * parallel capable, the method returns a dedicated object associated
443 * with the specified class name. Otherwise, the method returns this
444 * ClassLoader object. </p>
445 *
446 * @param className
447 * The name of the to-be-loaded class
448 *
449 * @return the lock for class loading operations
450 *
451 * @throws NullPointerException
452 * If registered as parallel capable and <tt>className</tt> is null
453 *
454 * @see #loadClass(String, boolean)
455 *
456 * @since 1.7
457 */
458 protected Object getClassLoadingLock(String className) {
459 Object lock = this;
460 if (parallelLockMap != null) {
461 Object newLock = new Object();
462 lock = parallelLockMap.putIfAbsent(className, newLock);
463 if (lock == null) {
464 lock = newLock;
489 final String name = cls.getName();
490 final int i = name.lastIndexOf('.');
491 if (i != -1) {
492 AccessController.doPrivileged(new PrivilegedAction<Void>() {
493 public Void run() {
494 sm.checkPackageAccess(name.substring(0, i));
495 return null;
496 }
497 }, new AccessControlContext(new ProtectionDomain[] {pd}));
498 }
499 }
500 domains.add(pd);
501 }
502
503 /**
504 * Finds the class with the specified <a href="#name">binary name</a>.
505 * This method should be overridden by class loader implementations that
506 * follow the delegation model for loading classes, and will be invoked by
507 * the {@link #loadClass <tt>loadClass</tt>} method after checking the
508 * parent class loader for the requested class. The default implementation
509 * throws a <tt>ClassNotFoundException</tt>. </p>
510 *
511 * @param name
512 * The <a href="#name">binary name</a> of the class
513 *
514 * @return The resulting <tt>Class</tt> object
515 *
516 * @throws ClassNotFoundException
517 * If the class could not be found
518 *
519 * @since 1.2
520 */
521 protected Class<?> findClass(String name) throws ClassNotFoundException {
522 throw new ClassNotFoundException(name);
523 }
524
525 /**
526 * Converts an array of bytes into an instance of class <tt>Class</tt>.
527 * Before the <tt>Class</tt> can be used it must be resolved. This method
528 * is deprecated in favor of the version that takes a <a
529 * href="#name">binary name</a> as its first argument, and is more secure.
755 }
756
757 /**
758 * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
759 * into an instance of class <tt>Class</tt>,
760 * with an optional <tt>ProtectionDomain</tt>. If the domain is
761 * <tt>null</tt>, then a default domain will be assigned to the class as
762 * specified in the documentation for {@link #defineClass(String, byte[],
763 * int, int)}. Before the class can be used it must be resolved.
764 *
765 * <p>The rules about the first class defined in a package determining the
766 * set of certificates for the package, and the restrictions on class names
767 * are identical to those specified in the documentation for {@link
768 * #defineClass(String, byte[], int, int, ProtectionDomain)}.
769 *
770 * <p> An invocation of this method of the form
771 * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
772 * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
773 * result as the statements
774 *
775 * <blockquote><tt>
776 * ...<br>
777 * byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link
778 * java.nio.ByteBuffer#remaining remaining}()];<br>
779 * </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[])
780 * get}(temp);<br>
781 * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
782 * </tt><i>cl</i><tt>.defineClass}(</tt><i>name</i><tt>, temp, 0,
783 * temp.length, </tt><i>pd</i><tt>);<br>
784 * </tt></blockquote>
785 *
786 * @param name
787 * The expected <a href="#name">binary name</a>. of the class, or
788 * <tt>null</tt> if not known
789 *
790 * @param b
791 * The bytes that make up the class data. The bytes from positions
792 * <tt>b.position()</tt> through <tt>b.position() + b.limit() -1
793 * </tt> should have the format of a valid class file as defined by
794 * <cite>The Java™ Virtual Machine Specification</cite>.
795 *
796 * @param protectionDomain
797 * The ProtectionDomain of the class, or <tt>null</tt>.
798 *
799 * @return The <tt>Class</tt> object created from the data,
800 * and optional <tt>ProtectionDomain</tt>.
801 *
802 * @throws ClassFormatError
803 * If the data did not contain a valid class.
804 *
923 for (int i = 0; i < pcerts.length; i++) {
924 match = false;
925 for (int j = 0; j < certs.length; j++) {
926 if (pcerts[i].equals(certs[j])) {
927 match = true;
928 break;
929 }
930 }
931 if (!match) return false;
932 }
933
934 return true;
935 }
936
937 /**
938 * Links the specified class. This (misleadingly named) method may be
939 * used by a class loader to link a class. If the class <tt>c</tt> has
940 * already been linked, then this method simply returns. Otherwise, the
941 * class is linked as described in the "Execution" chapter of
942 * <cite>The Java™ Language Specification</cite>.
943 * </p>
944 *
945 * @param c
946 * The class to link
947 *
948 * @throws NullPointerException
949 * If <tt>c</tt> is <tt>null</tt>.
950 *
951 * @see #defineClass(String, byte[], int, int)
952 */
953 protected final void resolveClass(Class<?> c) {
954 resolveClass0(c);
955 }
956
957 private native void resolveClass0(Class<?> c);
958
959 /**
960 * Finds a class with the specified <a href="#name">binary name</a>,
961 * loading it if necessary.
962 *
963 * <p> This method loads the class through the system class loader (see
995 }
996
997 /**
998 * Returns a class loaded by the bootstrap class loader;
999 * or return null if not found.
1000 */
1001 private Class<?> findBootstrapClassOrNull(String name)
1002 {
1003 if (!checkName(name)) return null;
1004
1005 return findBootstrapClass(name);
1006 }
1007
1008 // return null if not found
1009 private native Class<?> findBootstrapClass(String name);
1010
1011 /**
1012 * Returns the class with the given <a href="#name">binary name</a> if this
1013 * loader has been recorded by the Java virtual machine as an initiating
1014 * loader of a class with that <a href="#name">binary name</a>. Otherwise
1015 * <tt>null</tt> is returned. </p>
1016 *
1017 * @param name
1018 * The <a href="#name">binary name</a> of the class
1019 *
1020 * @return The <tt>Class</tt> object, or <tt>null</tt> if the class has
1021 * not been loaded
1022 *
1023 * @since 1.1
1024 */
1025 protected final Class<?> findLoadedClass(String name) {
1026 if (!checkName(name))
1027 return null;
1028 return findLoadedClass0(name);
1029 }
1030
1031 private native final Class<?> findLoadedClass0(String name);
1032
1033 /**
1034 * Sets the signers of a class. This should be invoked after defining a
1035 * class. </p>
1036 *
1037 * @param c
1038 * The <tt>Class</tt> object
1039 *
1040 * @param signers
1041 * The signers for the class
1042 *
1043 * @since 1.1
1044 */
1045 protected final void setSigners(Class<?> c, Object[] signers) {
1046 c.setSigners(signers);
1047 }
1048
1049
1050 // -- Resource --
1051
1052 /**
1053 * Finds the resource with the given name. A resource is some data
1054 * (images, audio, text, etc) that can be accessed by class code in a way
1055 * that is independent of the location of the code.
1108 *
1109 * @see #findResources(String)
1110 *
1111 * @since 1.2
1112 */
1113 public Enumeration<URL> getResources(String name) throws IOException {
1114 @SuppressWarnings("unchecked")
1115 Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
1116 if (parent != null) {
1117 tmp[0] = parent.getResources(name);
1118 } else {
1119 tmp[0] = getBootstrapResources(name);
1120 }
1121 tmp[1] = findResources(name);
1122
1123 return new CompoundEnumeration<>(tmp);
1124 }
1125
1126 /**
1127 * Finds the resource with the given name. Class loader implementations
1128 * should override this method to specify where to find resources. </p>
1129 *
1130 * @param name
1131 * The resource name
1132 *
1133 * @return A <tt>URL</tt> object for reading the resource, or
1134 * <tt>null</tt> if the resource could not be found
1135 *
1136 * @since 1.2
1137 */
1138 protected URL findResource(String name) {
1139 return null;
1140 }
1141
1142 /**
1143 * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
1144 * representing all the resources with the given name. Class loader
1145 * implementations should override this method to specify where to load
1146 * resources from. </p>
1147 *
1148 * @param name
1149 * The resource name
1150 *
1151 * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
1152 * the resources
1153 *
1154 * @throws IOException
1155 * If I/O errors occur
1156 *
1157 * @since 1.2
1158 */
1159 protected Enumeration<URL> findResources(String name) throws IOException {
1160 return java.util.Collections.emptyEnumeration();
1161 }
1162
1163 /**
1164 * Registers the caller as parallel capable.</p>
1165 * The registration succeeds if and only if all of the following
1166 * conditions are met: <br>
1167 * 1. no instance of the caller has been created</p>
1168 * 2. all of the super classes (except class Object) of the caller are
1169 * registered as parallel capable</p>
1170 * Note that once a class loader is registered as parallel capable, there
1171 * is no way to change it back. </p>
1172 *
1173 * @return true if the caller is successfully registered as
1174 * parallel capable and false if otherwise.
1175 *
1176 * @since 1.7
1177 */
1178 @CallerSensitive
1179 protected static boolean registerAsParallelCapable() {
1180 Class<? extends ClassLoader> callerClass =
1181 Reflection.getCallerClass().asSubclass(ClassLoader.class);
1182 return ParallelLoaders.register(callerClass);
1183 }
1184
1185 /**
1186 * Find a resource of the specified name from the search path used to load
1187 * classes. This method locates the resource through the system class
1188 * loader (see {@link #getSystemClassLoader()}). </p>
1189 *
1190 * @param name
1191 * The resource name
1192 *
1193 * @return A {@link java.net.URL <tt>URL</tt>} object for reading the
1194 * resource, or <tt>null</tt> if the resource could not be found
1195 *
1196 * @since 1.1
1197 */
1198 public static URL getSystemResource(String name) {
1199 ClassLoader system = getSystemClassLoader();
1200 if (system == null) {
1201 return getBootstrapResource(name);
1202 }
1203 return system.getResource(name);
1204 }
1205
1206 /**
1207 * Finds all resources of the specified name from the search path used to
1208 * load classes. The resources thus found are returned as an
1275 * @param name
1276 * The resource name
1277 *
1278 * @return An input stream for reading the resource, or <tt>null</tt>
1279 * if the resource could not be found
1280 *
1281 * @since 1.1
1282 */
1283 public InputStream getResourceAsStream(String name) {
1284 URL url = getResource(name);
1285 try {
1286 return url != null ? url.openStream() : null;
1287 } catch (IOException e) {
1288 return null;
1289 }
1290 }
1291
1292 /**
1293 * Open for reading, a resource of the specified name from the search path
1294 * used to load classes. This method locates the resource through the
1295 * system class loader (see {@link #getSystemClassLoader()}). </p>
1296 *
1297 * @param name
1298 * The resource name
1299 *
1300 * @return An input stream for reading the resource, or <tt>null</tt>
1301 * if the resource could not be found
1302 *
1303 * @since 1.1
1304 */
1305 public static InputStream getSystemResourceAsStream(String name) {
1306 URL url = getSystemResource(name);
1307 try {
1308 return url != null ? url.openStream() : null;
1309 } catch (IOException e) {
1310 return null;
1311 }
1312 }
1313
1314
1315 // -- Hierarchy --
1498 }
1499 }
1500 }
1501
1502 // The class loader for the system
1503 // @GuardedBy("ClassLoader.class")
1504 private static ClassLoader scl;
1505
1506 // Set to true once the system class loader has been set
1507 // @GuardedBy("ClassLoader.class")
1508 private static boolean sclSet;
1509
1510
1511 // -- Package --
1512
1513 /**
1514 * Defines a package by name in this <tt>ClassLoader</tt>. This allows
1515 * class loaders to define the packages for their classes. Packages must
1516 * be created before the class is defined, and package names must be
1517 * unique within a class loader and cannot be redefined or changed once
1518 * created. </p>
1519 *
1520 * @param name
1521 * The package name
1522 *
1523 * @param specTitle
1524 * The specification title
1525 *
1526 * @param specVersion
1527 * The specification version
1528 *
1529 * @param specVendor
1530 * The specification vendor
1531 *
1532 * @param implTitle
1533 * The implementation title
1534 *
1535 * @param implVersion
1536 * The implementation version
1537 *
1538 * @param implVendor
1555 String specVersion, String specVendor,
1556 String implTitle, String implVersion,
1557 String implVendor, URL sealBase)
1558 throws IllegalArgumentException
1559 {
1560 synchronized (packages) {
1561 Package pkg = getPackage(name);
1562 if (pkg != null) {
1563 throw new IllegalArgumentException(name);
1564 }
1565 pkg = new Package(name, specTitle, specVersion, specVendor,
1566 implTitle, implVersion, implVendor,
1567 sealBase, this);
1568 packages.put(name, pkg);
1569 return pkg;
1570 }
1571 }
1572
1573 /**
1574 * Returns a <tt>Package</tt> that has been defined by this class loader
1575 * or any of its ancestors. </p>
1576 *
1577 * @param name
1578 * The package name
1579 *
1580 * @return The <tt>Package</tt> corresponding to the given name, or
1581 * <tt>null</tt> if not found
1582 *
1583 * @since 1.2
1584 */
1585 protected Package getPackage(String name) {
1586 Package pkg;
1587 synchronized (packages) {
1588 pkg = packages.get(name);
1589 }
1590 if (pkg == null) {
1591 if (parent != null) {
1592 pkg = parent.getPackage(name);
1593 } else {
1594 pkg = Package.getSystemPackage(name);
1595 }
1596 if (pkg != null) {
1597 synchronized (packages) {
1598 Package pkg2 = packages.get(name);
1599 if (pkg2 == null) {
1600 packages.put(name, pkg);
1601 } else {
1602 pkg = pkg2;
1603 }
1604 }
1605 }
1606 }
1607 return pkg;
1608 }
1609
1610 /**
1611 * Returns all of the <tt>Packages</tt> defined by this class loader and
1612 * its ancestors. </p>
1613 *
1614 * @return The array of <tt>Package</tt> objects defined by this
1615 * <tt>ClassLoader</tt>
1616 *
1617 * @since 1.2
1618 */
1619 protected Package[] getPackages() {
1620 Map<String, Package> map;
1621 synchronized (packages) {
1622 map = new HashMap<>(packages);
1623 }
1624 Package[] pkgs;
1625 if (parent != null) {
1626 pkgs = parent.getPackages();
1627 } else {
1628 pkgs = Package.getSystemPackages();
1629 }
1630 if (pkgs != null) {
1631 for (int i = 0; i < pkgs.length; i++) {
1632 String pkgName = pkgs[i].getName();
1633 if (map.get(pkgName) == null) {
1634 map.put(pkgName, pkgs[i]);
1635 }
1636 }
1637 }
1638 return map.values().toArray(new Package[map.size()]);
1639 }
1640
1641
1642 // -- Native library access --
1643
1644 /**
1645 * Returns the absolute path name of a native library. The VM invokes this
1646 * method to locate the native libraries that belong to classes loaded with
1647 * this class loader. If this method returns <tt>null</tt>, the VM
1648 * searches the library along the path specified as the
1649 * "<tt>java.library.path</tt>" property. </p>
1650 *
1651 * @param libname
1652 * The library name
1653 *
1654 * @return The absolute path of the native library
1655 *
1656 * @see System#loadLibrary(String)
1657 * @see System#mapLibraryName(String)
1658 *
1659 * @since 1.2
1660 */
1661 protected String findLibrary(String libname) {
1662 return null;
1663 }
1664
1665 /**
1666 * The inner class NativeLibrary denotes a loaded native library instance.
1667 * Every classloader contains a vector of loaded native libraries in the
1668 * private field <tt>nativeLibraries</tt>. The native libraries loaded
1669 * into the system are entered into the <tt>systemNativeLibraries</tt>
1949 // that the default package is placed under a null map key. If this field
1950 // is null then we are delegating assertion status queries to the VM, i.e.,
1951 // none of this ClassLoader's assertion status modification methods have
1952 // been invoked.
1953 // @GuardedBy("assertionLock")
1954 private Map<String, Boolean> packageAssertionStatus = null;
1955
1956 // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
1957 // field is null then we are delegating assertion status queries to the VM,
1958 // i.e., none of this ClassLoader's assertion status modification methods
1959 // have been invoked.
1960 // @GuardedBy("assertionLock")
1961 Map<String, Boolean> classAssertionStatus = null;
1962
1963 /**
1964 * Sets the default assertion status for this class loader. This setting
1965 * determines whether classes loaded by this class loader and initialized
1966 * in the future will have assertions enabled or disabled by default.
1967 * This setting may be overridden on a per-package or per-class basis by
1968 * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
1969 * #setClassAssertionStatus(String, boolean)}. </p>
1970 *
1971 * @param enabled
1972 * <tt>true</tt> if classes loaded by this class loader will
1973 * henceforth have assertions enabled by default, <tt>false</tt>
1974 * if they will have assertions disabled by default.
1975 *
1976 * @since 1.4
1977 */
1978 public void setDefaultAssertionStatus(boolean enabled) {
1979 synchronized (assertionLock) {
1980 if (classAssertionStatus == null)
1981 initializeJavaAssertionMaps();
1982
1983 defaultAssertionStatus = enabled;
1984 }
1985 }
1986
1987 /**
1988 * Sets the package default assertion status for the named package. The
1989 * package default assertion status determines the assertion status for
2051 * enabled when (and if) it is initialized, <tt>false</tt> if the
2052 * class is to have assertions disabled.
2053 *
2054 * @since 1.4
2055 */
2056 public void setClassAssertionStatus(String className, boolean enabled) {
2057 synchronized (assertionLock) {
2058 if (classAssertionStatus == null)
2059 initializeJavaAssertionMaps();
2060
2061 classAssertionStatus.put(className, enabled);
2062 }
2063 }
2064
2065 /**
2066 * Sets the default assertion status for this class loader to
2067 * <tt>false</tt> and discards any package defaults or class assertion
2068 * status settings associated with the class loader. This method is
2069 * provided so that class loaders can be made to ignore any command line or
2070 * persistent assertion status settings and "start with a clean slate."
2071 * </p>
2072 *
2073 * @since 1.4
2074 */
2075 public void clearAssertionStatus() {
2076 /*
2077 * Whether or not "Java assertion maps" are initialized, set
2078 * them to empty maps, effectively ignoring any present settings.
2079 */
2080 synchronized (assertionLock) {
2081 classAssertionStatus = new HashMap<>();
2082 packageAssertionStatus = new HashMap<>();
2083 defaultAssertionStatus = false;
2084 }
2085 }
2086
2087 /**
2088 * Returns the assertion status that would be assigned to the specified
2089 * class if it were to be initialized at the time this method is invoked.
2090 * If the named class has had its assertion status set, the most recent
2091 * setting will be returned; otherwise, if any package default assertion
|
140 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
141 * create a class instance. A sample implementation is:
142 *
143 * <blockquote><pre>
144 * class NetworkClassLoader extends ClassLoader {
145 * String host;
146 * int port;
147 *
148 * public Class findClass(String name) {
149 * byte[] b = loadClassData(name);
150 * return defineClass(name, b, 0, b.length);
151 * }
152 *
153 * private byte[] loadClassData(String name) {
154 * // load the class data from the connection
155 * . . .
156 * }
157 * }
158 * </pre></blockquote>
159 *
160 * <h3> <a name="name">Binary names</a> </h3>
161 *
162 * <p> Any class name provided as a {@link String} parameter to methods in
163 * <tt>ClassLoader</tt> must be a binary name as defined by
164 * <cite>The Java™ Language Specification</cite>.
165 *
166 * <p> Examples of valid class names include:
167 * <blockquote><pre>
168 * "java.lang.String"
169 * "javax.swing.JSpinner$DefaultEditor"
170 * "java.security.KeyStore$Builder$FileBuilder$1"
171 * "java.net.URLClassLoader$3$1"
172 * </pre></blockquote>
173 *
174 * @see #resolveClass(Class)
175 * @since 1.0
176 */
177 public abstract class ClassLoader {
178
179 private static native void registerNatives();
180 static {
325 * <tt>checkCreateClassLoader</tt>} method is invoked. This may result in
326 * a security exception. </p>
327 *
328 * @throws SecurityException
329 * If a security manager exists and its
330 * <tt>checkCreateClassLoader</tt> method doesn't allow creation
331 * of a new class loader.
332 */
333 protected ClassLoader() {
334 this(checkCreateClassLoader(), getSystemClassLoader());
335 }
336
337 // -- Class --
338
339 /**
340 * Loads the class with the specified <a href="#name">binary name</a>.
341 * This method searches for classes in the same manner as the {@link
342 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
343 * machine to resolve class references. Invoking this method is equivalent
344 * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
345 * false)</tt>}.
346 *
347 * @param name
348 * The <a href="#name">binary name</a> of the class
349 *
350 * @return The resulting <tt>Class</tt> object
351 *
352 * @throws ClassNotFoundException
353 * If the class was not found
354 */
355 public Class<?> loadClass(String name) throws ClassNotFoundException {
356 return loadClass(name, false);
357 }
358
359 /**
360 * Loads the class with the specified <a href="#name">binary name</a>. The
361 * default implementation of this method searches for classes in the
362 * following order:
363 *
364 * <p><ol>
365 *
424
425 // this is the defining class loader; record the stats
426 sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
427 sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
428 sun.misc.PerfCounter.getFindClasses().increment();
429 }
430 }
431 if (resolve) {
432 resolveClass(c);
433 }
434 return c;
435 }
436 }
437
438 /**
439 * Returns the lock object for class loading operations.
440 * For backward compatibility, the default implementation of this method
441 * behaves as follows. If this ClassLoader object is registered as
442 * parallel capable, the method returns a dedicated object associated
443 * with the specified class name. Otherwise, the method returns this
444 * ClassLoader object.
445 *
446 * @param className
447 * The name of the to-be-loaded class
448 *
449 * @return the lock for class loading operations
450 *
451 * @throws NullPointerException
452 * If registered as parallel capable and <tt>className</tt> is null
453 *
454 * @see #loadClass(String, boolean)
455 *
456 * @since 1.7
457 */
458 protected Object getClassLoadingLock(String className) {
459 Object lock = this;
460 if (parallelLockMap != null) {
461 Object newLock = new Object();
462 lock = parallelLockMap.putIfAbsent(className, newLock);
463 if (lock == null) {
464 lock = newLock;
489 final String name = cls.getName();
490 final int i = name.lastIndexOf('.');
491 if (i != -1) {
492 AccessController.doPrivileged(new PrivilegedAction<Void>() {
493 public Void run() {
494 sm.checkPackageAccess(name.substring(0, i));
495 return null;
496 }
497 }, new AccessControlContext(new ProtectionDomain[] {pd}));
498 }
499 }
500 domains.add(pd);
501 }
502
503 /**
504 * Finds the class with the specified <a href="#name">binary name</a>.
505 * This method should be overridden by class loader implementations that
506 * follow the delegation model for loading classes, and will be invoked by
507 * the {@link #loadClass <tt>loadClass</tt>} method after checking the
508 * parent class loader for the requested class. The default implementation
509 * throws a <tt>ClassNotFoundException</tt>.
510 *
511 * @param name
512 * The <a href="#name">binary name</a> of the class
513 *
514 * @return The resulting <tt>Class</tt> object
515 *
516 * @throws ClassNotFoundException
517 * If the class could not be found
518 *
519 * @since 1.2
520 */
521 protected Class<?> findClass(String name) throws ClassNotFoundException {
522 throw new ClassNotFoundException(name);
523 }
524
525 /**
526 * Converts an array of bytes into an instance of class <tt>Class</tt>.
527 * Before the <tt>Class</tt> can be used it must be resolved. This method
528 * is deprecated in favor of the version that takes a <a
529 * href="#name">binary name</a> as its first argument, and is more secure.
755 }
756
757 /**
758 * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
759 * into an instance of class <tt>Class</tt>,
760 * with an optional <tt>ProtectionDomain</tt>. If the domain is
761 * <tt>null</tt>, then a default domain will be assigned to the class as
762 * specified in the documentation for {@link #defineClass(String, byte[],
763 * int, int)}. Before the class can be used it must be resolved.
764 *
765 * <p>The rules about the first class defined in a package determining the
766 * set of certificates for the package, and the restrictions on class names
767 * are identical to those specified in the documentation for {@link
768 * #defineClass(String, byte[], int, int, ProtectionDomain)}.
769 *
770 * <p> An invocation of this method of the form
771 * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
772 * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
773 * result as the statements
774 *
775 *<p> <tt>
776 * ...<br>
777 * byte[] temp = new byte[bBuffer.{@link
778 * java.nio.ByteBuffer#remaining remaining}()];<br>
779 * bBuffer.{@link java.nio.ByteBuffer#get(byte[])
780 * get}(temp);<br>
781 * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
782 * cl.defineClass}(name, temp, 0,
783 * temp.length, pd);<br>
784 * </tt></p>
785 *
786 * @param name
787 * The expected <a href="#name">binary name</a>. of the class, or
788 * <tt>null</tt> if not known
789 *
790 * @param b
791 * The bytes that make up the class data. The bytes from positions
792 * <tt>b.position()</tt> through <tt>b.position() + b.limit() -1
793 * </tt> should have the format of a valid class file as defined by
794 * <cite>The Java™ Virtual Machine Specification</cite>.
795 *
796 * @param protectionDomain
797 * The ProtectionDomain of the class, or <tt>null</tt>.
798 *
799 * @return The <tt>Class</tt> object created from the data,
800 * and optional <tt>ProtectionDomain</tt>.
801 *
802 * @throws ClassFormatError
803 * If the data did not contain a valid class.
804 *
923 for (int i = 0; i < pcerts.length; i++) {
924 match = false;
925 for (int j = 0; j < certs.length; j++) {
926 if (pcerts[i].equals(certs[j])) {
927 match = true;
928 break;
929 }
930 }
931 if (!match) return false;
932 }
933
934 return true;
935 }
936
937 /**
938 * Links the specified class. This (misleadingly named) method may be
939 * used by a class loader to link a class. If the class <tt>c</tt> has
940 * already been linked, then this method simply returns. Otherwise, the
941 * class is linked as described in the "Execution" chapter of
942 * <cite>The Java™ Language Specification</cite>.
943 *
944 * @param c
945 * The class to link
946 *
947 * @throws NullPointerException
948 * If <tt>c</tt> is <tt>null</tt>.
949 *
950 * @see #defineClass(String, byte[], int, int)
951 */
952 protected final void resolveClass(Class<?> c) {
953 resolveClass0(c);
954 }
955
956 private native void resolveClass0(Class<?> c);
957
958 /**
959 * Finds a class with the specified <a href="#name">binary name</a>,
960 * loading it if necessary.
961 *
962 * <p> This method loads the class through the system class loader (see
994 }
995
996 /**
997 * Returns a class loaded by the bootstrap class loader;
998 * or return null if not found.
999 */
1000 private Class<?> findBootstrapClassOrNull(String name)
1001 {
1002 if (!checkName(name)) return null;
1003
1004 return findBootstrapClass(name);
1005 }
1006
1007 // return null if not found
1008 private native Class<?> findBootstrapClass(String name);
1009
1010 /**
1011 * Returns the class with the given <a href="#name">binary name</a> if this
1012 * loader has been recorded by the Java virtual machine as an initiating
1013 * loader of a class with that <a href="#name">binary name</a>. Otherwise
1014 * <tt>null</tt> is returned.
1015 *
1016 * @param name
1017 * The <a href="#name">binary name</a> of the class
1018 *
1019 * @return The <tt>Class</tt> object, or <tt>null</tt> if the class has
1020 * not been loaded
1021 *
1022 * @since 1.1
1023 */
1024 protected final Class<?> findLoadedClass(String name) {
1025 if (!checkName(name))
1026 return null;
1027 return findLoadedClass0(name);
1028 }
1029
1030 private native final Class<?> findLoadedClass0(String name);
1031
1032 /**
1033 * Sets the signers of a class. This should be invoked after defining a
1034 * class.
1035 *
1036 * @param c
1037 * The <tt>Class</tt> object
1038 *
1039 * @param signers
1040 * The signers for the class
1041 *
1042 * @since 1.1
1043 */
1044 protected final void setSigners(Class<?> c, Object[] signers) {
1045 c.setSigners(signers);
1046 }
1047
1048
1049 // -- Resource --
1050
1051 /**
1052 * Finds the resource with the given name. A resource is some data
1053 * (images, audio, text, etc) that can be accessed by class code in a way
1054 * that is independent of the location of the code.
1107 *
1108 * @see #findResources(String)
1109 *
1110 * @since 1.2
1111 */
1112 public Enumeration<URL> getResources(String name) throws IOException {
1113 @SuppressWarnings("unchecked")
1114 Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
1115 if (parent != null) {
1116 tmp[0] = parent.getResources(name);
1117 } else {
1118 tmp[0] = getBootstrapResources(name);
1119 }
1120 tmp[1] = findResources(name);
1121
1122 return new CompoundEnumeration<>(tmp);
1123 }
1124
1125 /**
1126 * Finds the resource with the given name. Class loader implementations
1127 * should override this method to specify where to find resources.
1128 *
1129 * @param name
1130 * The resource name
1131 *
1132 * @return A <tt>URL</tt> object for reading the resource, or
1133 * <tt>null</tt> if the resource could not be found
1134 *
1135 * @since 1.2
1136 */
1137 protected URL findResource(String name) {
1138 return null;
1139 }
1140
1141 /**
1142 * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
1143 * representing all the resources with the given name. Class loader
1144 * implementations should override this method to specify where to load
1145 * resources from.
1146 *
1147 * @param name
1148 * The resource name
1149 *
1150 * @return An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
1151 * the resources
1152 *
1153 * @throws IOException
1154 * If I/O errors occur
1155 *
1156 * @since 1.2
1157 */
1158 protected Enumeration<URL> findResources(String name) throws IOException {
1159 return java.util.Collections.emptyEnumeration();
1160 }
1161
1162 /**
1163 * Registers the caller as parallel capable.
1164 * The registration succeeds if and only if all of the following
1165 * conditions are met:
1166 * <ol>
1167 * <li> no instance of the caller has been created</li>
1168 * <li> all of the super classes (except class Object) of the caller are
1169 * registered as parallel capable</li>
1170 * </ol>
1171 * <p>Note that once a class loader is registered as parallel capable, there
1172 * is no way to change it back.</p>
1173 *
1174 * @return true if the caller is successfully registered as
1175 * parallel capable and false if otherwise.
1176 *
1177 * @since 1.7
1178 */
1179 @CallerSensitive
1180 protected static boolean registerAsParallelCapable() {
1181 Class<? extends ClassLoader> callerClass =
1182 Reflection.getCallerClass().asSubclass(ClassLoader.class);
1183 return ParallelLoaders.register(callerClass);
1184 }
1185
1186 /**
1187 * Find a resource of the specified name from the search path used to load
1188 * classes. This method locates the resource through the system class
1189 * loader (see {@link #getSystemClassLoader()}).
1190 *
1191 * @param name
1192 * The resource name
1193 *
1194 * @return A {@link java.net.URL <tt>URL</tt>} object for reading the
1195 * resource, or <tt>null</tt> if the resource could not be found
1196 *
1197 * @since 1.1
1198 */
1199 public static URL getSystemResource(String name) {
1200 ClassLoader system = getSystemClassLoader();
1201 if (system == null) {
1202 return getBootstrapResource(name);
1203 }
1204 return system.getResource(name);
1205 }
1206
1207 /**
1208 * Finds all resources of the specified name from the search path used to
1209 * load classes. The resources thus found are returned as an
1276 * @param name
1277 * The resource name
1278 *
1279 * @return An input stream for reading the resource, or <tt>null</tt>
1280 * if the resource could not be found
1281 *
1282 * @since 1.1
1283 */
1284 public InputStream getResourceAsStream(String name) {
1285 URL url = getResource(name);
1286 try {
1287 return url != null ? url.openStream() : null;
1288 } catch (IOException e) {
1289 return null;
1290 }
1291 }
1292
1293 /**
1294 * Open for reading, a resource of the specified name from the search path
1295 * used to load classes. This method locates the resource through the
1296 * system class loader (see {@link #getSystemClassLoader()}).
1297 *
1298 * @param name
1299 * The resource name
1300 *
1301 * @return An input stream for reading the resource, or <tt>null</tt>
1302 * if the resource could not be found
1303 *
1304 * @since 1.1
1305 */
1306 public static InputStream getSystemResourceAsStream(String name) {
1307 URL url = getSystemResource(name);
1308 try {
1309 return url != null ? url.openStream() : null;
1310 } catch (IOException e) {
1311 return null;
1312 }
1313 }
1314
1315
1316 // -- Hierarchy --
1499 }
1500 }
1501 }
1502
1503 // The class loader for the system
1504 // @GuardedBy("ClassLoader.class")
1505 private static ClassLoader scl;
1506
1507 // Set to true once the system class loader has been set
1508 // @GuardedBy("ClassLoader.class")
1509 private static boolean sclSet;
1510
1511
1512 // -- Package --
1513
1514 /**
1515 * Defines a package by name in this <tt>ClassLoader</tt>. This allows
1516 * class loaders to define the packages for their classes. Packages must
1517 * be created before the class is defined, and package names must be
1518 * unique within a class loader and cannot be redefined or changed once
1519 * created.
1520 *
1521 * @param name
1522 * The package name
1523 *
1524 * @param specTitle
1525 * The specification title
1526 *
1527 * @param specVersion
1528 * The specification version
1529 *
1530 * @param specVendor
1531 * The specification vendor
1532 *
1533 * @param implTitle
1534 * The implementation title
1535 *
1536 * @param implVersion
1537 * The implementation version
1538 *
1539 * @param implVendor
1556 String specVersion, String specVendor,
1557 String implTitle, String implVersion,
1558 String implVendor, URL sealBase)
1559 throws IllegalArgumentException
1560 {
1561 synchronized (packages) {
1562 Package pkg = getPackage(name);
1563 if (pkg != null) {
1564 throw new IllegalArgumentException(name);
1565 }
1566 pkg = new Package(name, specTitle, specVersion, specVendor,
1567 implTitle, implVersion, implVendor,
1568 sealBase, this);
1569 packages.put(name, pkg);
1570 return pkg;
1571 }
1572 }
1573
1574 /**
1575 * Returns a <tt>Package</tt> that has been defined by this class loader
1576 * or any of its ancestors.
1577 *
1578 * @param name
1579 * The package name
1580 *
1581 * @return The <tt>Package</tt> corresponding to the given name, or
1582 * <tt>null</tt> if not found
1583 *
1584 * @since 1.2
1585 */
1586 protected Package getPackage(String name) {
1587 Package pkg;
1588 synchronized (packages) {
1589 pkg = packages.get(name);
1590 }
1591 if (pkg == null) {
1592 if (parent != null) {
1593 pkg = parent.getPackage(name);
1594 } else {
1595 pkg = Package.getSystemPackage(name);
1596 }
1597 if (pkg != null) {
1598 synchronized (packages) {
1599 Package pkg2 = packages.get(name);
1600 if (pkg2 == null) {
1601 packages.put(name, pkg);
1602 } else {
1603 pkg = pkg2;
1604 }
1605 }
1606 }
1607 }
1608 return pkg;
1609 }
1610
1611 /**
1612 * Returns all of the <tt>Packages</tt> defined by this class loader and
1613 * its ancestors.
1614 *
1615 * @return The array of <tt>Package</tt> objects defined by this
1616 * <tt>ClassLoader</tt>
1617 *
1618 * @since 1.2
1619 */
1620 protected Package[] getPackages() {
1621 Map<String, Package> map;
1622 synchronized (packages) {
1623 map = new HashMap<>(packages);
1624 }
1625 Package[] pkgs;
1626 if (parent != null) {
1627 pkgs = parent.getPackages();
1628 } else {
1629 pkgs = Package.getSystemPackages();
1630 }
1631 if (pkgs != null) {
1632 for (int i = 0; i < pkgs.length; i++) {
1633 String pkgName = pkgs[i].getName();
1634 if (map.get(pkgName) == null) {
1635 map.put(pkgName, pkgs[i]);
1636 }
1637 }
1638 }
1639 return map.values().toArray(new Package[map.size()]);
1640 }
1641
1642
1643 // -- Native library access --
1644
1645 /**
1646 * Returns the absolute path name of a native library. The VM invokes this
1647 * method to locate the native libraries that belong to classes loaded with
1648 * this class loader. If this method returns <tt>null</tt>, the VM
1649 * searches the library along the path specified as the
1650 * "<tt>java.library.path</tt>" property.
1651 *
1652 * @param libname
1653 * The library name
1654 *
1655 * @return The absolute path of the native library
1656 *
1657 * @see System#loadLibrary(String)
1658 * @see System#mapLibraryName(String)
1659 *
1660 * @since 1.2
1661 */
1662 protected String findLibrary(String libname) {
1663 return null;
1664 }
1665
1666 /**
1667 * The inner class NativeLibrary denotes a loaded native library instance.
1668 * Every classloader contains a vector of loaded native libraries in the
1669 * private field <tt>nativeLibraries</tt>. The native libraries loaded
1670 * into the system are entered into the <tt>systemNativeLibraries</tt>
1950 // that the default package is placed under a null map key. If this field
1951 // is null then we are delegating assertion status queries to the VM, i.e.,
1952 // none of this ClassLoader's assertion status modification methods have
1953 // been invoked.
1954 // @GuardedBy("assertionLock")
1955 private Map<String, Boolean> packageAssertionStatus = null;
1956
1957 // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
1958 // field is null then we are delegating assertion status queries to the VM,
1959 // i.e., none of this ClassLoader's assertion status modification methods
1960 // have been invoked.
1961 // @GuardedBy("assertionLock")
1962 Map<String, Boolean> classAssertionStatus = null;
1963
1964 /**
1965 * Sets the default assertion status for this class loader. This setting
1966 * determines whether classes loaded by this class loader and initialized
1967 * in the future will have assertions enabled or disabled by default.
1968 * This setting may be overridden on a per-package or per-class basis by
1969 * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
1970 * #setClassAssertionStatus(String, boolean)}.
1971 *
1972 * @param enabled
1973 * <tt>true</tt> if classes loaded by this class loader will
1974 * henceforth have assertions enabled by default, <tt>false</tt>
1975 * if they will have assertions disabled by default.
1976 *
1977 * @since 1.4
1978 */
1979 public void setDefaultAssertionStatus(boolean enabled) {
1980 synchronized (assertionLock) {
1981 if (classAssertionStatus == null)
1982 initializeJavaAssertionMaps();
1983
1984 defaultAssertionStatus = enabled;
1985 }
1986 }
1987
1988 /**
1989 * Sets the package default assertion status for the named package. The
1990 * package default assertion status determines the assertion status for
2052 * enabled when (and if) it is initialized, <tt>false</tt> if the
2053 * class is to have assertions disabled.
2054 *
2055 * @since 1.4
2056 */
2057 public void setClassAssertionStatus(String className, boolean enabled) {
2058 synchronized (assertionLock) {
2059 if (classAssertionStatus == null)
2060 initializeJavaAssertionMaps();
2061
2062 classAssertionStatus.put(className, enabled);
2063 }
2064 }
2065
2066 /**
2067 * Sets the default assertion status for this class loader to
2068 * <tt>false</tt> and discards any package defaults or class assertion
2069 * status settings associated with the class loader. This method is
2070 * provided so that class loaders can be made to ignore any command line or
2071 * persistent assertion status settings and "start with a clean slate."
2072 *
2073 * @since 1.4
2074 */
2075 public void clearAssertionStatus() {
2076 /*
2077 * Whether or not "Java assertion maps" are initialized, set
2078 * them to empty maps, effectively ignoring any present settings.
2079 */
2080 synchronized (assertionLock) {
2081 classAssertionStatus = new HashMap<>();
2082 packageAssertionStatus = new HashMap<>();
2083 defaultAssertionStatus = false;
2084 }
2085 }
2086
2087 /**
2088 * Returns the assertion status that would be assigned to the specified
2089 * class if it were to be initialized at the time this method is invoked.
2090 * If the named class has had its assertion status set, the most recent
2091 * setting will be returned; otherwise, if any package default assertion
|