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="#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
82 * Class#getClassLoader() reference} to the {@code ClassLoader} that defined
83 * it.
84 *
85 * <p> {@code Class} objects for array classes are not created by class
86 * loaders, but are created automatically as required by the Java runtime.
87 * The class loader for an array class, as returned by {@link
88 * Class#getClassLoader()} is the same as the class loader for its element
89 * type; if the element type is a primitive type, then the array class has no
90 * class loader.
91 *
92 * <p> Applications implement subclasses of {@code ClassLoader} in order to
93 * extend the manner in which the Java virtual machine dynamically loads
94 * classes.
95 *
96 * <p> Class loaders may typically be used by security managers to indicate
185 * it should use the method {@link #defineClass defineClass} to
186 * create a class instance. A sample implementation is:
187 *
188 * <blockquote><pre>
189 * class NetworkClassLoader extends ClassLoader {
190 * String host;
191 * int port;
192 *
193 * public Class findClass(String name) {
194 * byte[] b = loadClassData(name);
195 * return defineClass(name, b, 0, b.length);
196 * }
197 *
198 * private byte[] loadClassData(String name) {
199 * // load the class data from the connection
200 * . . .
201 * }
202 * }
203 * </pre></blockquote>
204 *
205 * <h3> <a id="name">Binary names</a> </h3>
206 *
207 * <p> Any class name provided as a {@code String} parameter to methods in
208 * {@code ClassLoader} must be a binary name as defined by
209 * <cite>The Java™ Language Specification</cite>.
210 *
211 * <p> Examples of valid class names include:
212 * <blockquote><pre>
213 * "java.lang.String"
214 * "javax.swing.JSpinner$DefaultEditor"
215 * "java.security.KeyStore$Builder$FileBuilder$1"
216 * "java.net.URLClassLoader$3$1"
217 * </pre></blockquote>
218 *
219 * <p> Any package name provided as a {@code String} parameter to methods in
220 * {@code ClassLoader} must be either the empty string (denoting an unnamed package)
221 * or a fully qualified name as defined by
222 * <cite>The Java™ Language Specification</cite>.
223 *
224 * @jls 6.7 Fully Qualified Names
225 * @jls 13.1 The Form of a Binary
463 *
464 * @return name of this class loader; or {@code null} if
465 * this class loader is not named.
466 *
467 * @since 9
468 * @spec JPMS
469 */
470 public String getName() {
471 return name;
472 }
473
474 // package-private used by StackTraceElement to avoid
475 // calling the overrideable getName method
476 final String name() {
477 return name;
478 }
479
480 // -- Class --
481
482 /**
483 * Loads the class with the specified <a href="#name">binary name</a>.
484 * This method searches for classes in the same manner as the {@link
485 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
486 * machine to resolve class references. Invoking this method is equivalent
487 * to invoking {@link #loadClass(String, boolean) loadClass(name,
488 * false)}.
489 *
490 * @param name
491 * The <a href="#name">binary name</a> of the class
492 *
493 * @return The resulting {@code Class} object
494 *
495 * @throws ClassNotFoundException
496 * If the class was not found
497 */
498 public Class<?> loadClass(String name) throws ClassNotFoundException {
499 return loadClass(name, false);
500 }
501
502 /**
503 * Loads the class with the specified <a href="#name">binary name</a>. The
504 * default implementation of this method searches for classes in the
505 * following order:
506 *
507 * <ol>
508 *
509 * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
510 * has already been loaded. </p></li>
511 *
512 * <li><p> Invoke the {@link #loadClass(String) loadClass} method
513 * on the parent class loader. If the parent is {@code null} the class
514 * loader built into the virtual machine is used, instead. </p></li>
515 *
516 * <li><p> Invoke the {@link #findClass(String)} method to find the
517 * class. </p></li>
518 *
519 * </ol>
520 *
521 * <p> If the class was found using the above steps, and the
522 * {@code resolve} flag is true, this method will then invoke the {@link
523 * #resolveClass(Class)} method on the resulting {@code Class} object.
524 *
525 * <p> Subclasses of {@code ClassLoader} are encouraged to override {@link
526 * #findClass(String)}, rather than this method. </p>
527 *
528 * <p> Unless overridden, this method synchronizes on the result of
529 * {@link #getClassLoadingLock getClassLoadingLock} method
530 * during the entire class loading process.
531 *
532 * @param name
533 * The <a href="#name">binary name</a> of the class
534 *
535 * @param resolve
536 * If {@code true} then resolve the class
537 *
538 * @return The resulting {@code Class} object
539 *
540 * @throws ClassNotFoundException
541 * If the class could not be found
542 */
543 protected Class<?> loadClass(String name, boolean resolve)
544 throws ClassNotFoundException
545 {
546 synchronized (getClassLoadingLock(name)) {
547 // First, check if the class has already been loaded
548 Class<?> c = findLoadedClass(name);
549 if (c == null) {
550 long t0 = System.nanoTime();
551 try {
552 if (parent != null) {
553 c = parent.loadClass(name, false);
562 if (c == null) {
563 // If still not found, then invoke findClass in order
564 // to find the class.
565 long t1 = System.nanoTime();
566 c = findClass(name);
567
568 // this is the defining class loader; record the stats
569 PerfCounter.getParentDelegationTime().addTime(t1 - t0);
570 PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
571 PerfCounter.getFindClasses().increment();
572 }
573 }
574 if (resolve) {
575 resolveClass(c);
576 }
577 return c;
578 }
579 }
580
581 /**
582 * Loads the class with the specified <a href="#name">binary name</a>
583 * in a module defined to this class loader. This method returns {@code null}
584 * if the class could not be found.
585 *
586 * @apiNote This method does not delegate to the parent class loader.
587 *
588 * @implSpec The default implementation of this method searches for classes
589 * in the following order:
590 *
591 * <ol>
592 * <li>Invoke {@link #findLoadedClass(String)} to check if the class
593 * has already been loaded.</li>
594 * <li>Invoke the {@link #findClass(String, String)} method to find the
595 * class in the given module.</li>
596 * </ol>
597 *
598 * @param module
599 * The module
600 * @param name
601 * The <a href="#name">binary name</a> of the class
602 *
603 * @return The resulting {@code Class} object in a module defined by
604 * this class loader, or {@code null} if the class could not be found.
605 */
606 final Class<?> loadClass(Module module, String name) {
607 synchronized (getClassLoadingLock(name)) {
608 // First, check if the class has already been loaded
609 Class<?> c = findLoadedClass(name);
610 if (c == null) {
611 c = findClass(module.getName(), name);
612 }
613 if (c != null && c.getModule() == module) {
614 return c;
615 } else {
616 return null;
617 }
618 }
619 }
620
621 /**
657 if (ReflectUtil.isNonPublicProxyClass(cls)) {
658 for (Class<?> intf: cls.getInterfaces()) {
659 checkPackageAccess(intf, pd);
660 }
661 return;
662 }
663
664 final String packageName = cls.getPackageName();
665 if (!packageName.isEmpty()) {
666 AccessController.doPrivileged(new PrivilegedAction<>() {
667 public Void run() {
668 sm.checkPackageAccess(packageName);
669 return null;
670 }
671 }, new AccessControlContext(new ProtectionDomain[] {pd}));
672 }
673 }
674 }
675
676 /**
677 * Finds the class with the specified <a href="#name">binary name</a>.
678 * This method should be overridden by class loader implementations that
679 * follow the delegation model for loading classes, and will be invoked by
680 * the {@link #loadClass loadClass} method after checking the
681 * parent class loader for the requested class.
682 *
683 * @implSpec The default implementation throws {@code ClassNotFoundException}.
684 *
685 * @param name
686 * The <a href="#name">binary name</a> of the class
687 *
688 * @return The resulting {@code Class} object
689 *
690 * @throws ClassNotFoundException
691 * If the class could not be found
692 *
693 * @since 1.2
694 */
695 protected Class<?> findClass(String name) throws ClassNotFoundException {
696 throw new ClassNotFoundException(name);
697 }
698
699 /**
700 * Finds the class with the given <a href="#name">binary name</a>
701 * in a module defined to this class loader.
702 * Class loader implementations that support the loading from modules
703 * should override this method.
704 *
705 * @apiNote This method returns {@code null} rather than throwing
706 * {@code ClassNotFoundException} if the class could not be found.
707 *
708 * @implSpec The default implementation attempts to find the class by
709 * invoking {@link #findClass(String)} when the {@code moduleName} is
710 * {@code null}. It otherwise returns {@code null}.
711 *
712 * @param moduleName
713 * The module name; or {@code null} to find the class in the
714 * {@linkplain #getUnnamedModule() unnamed module} for this
715 * class loader
716
717 * @param name
718 * The <a href="#name">binary name</a> of the class
719 *
720 * @return The resulting {@code Class} object, or {@code null}
721 * if the class could not be found.
722 *
723 * @since 9
724 * @spec JPMS
725 */
726 protected Class<?> findClass(String moduleName, String name) {
727 if (moduleName == null) {
728 try {
729 return findClass(name);
730 } catch (ClassNotFoundException ignore) { }
731 }
732 return null;
733 }
734
735
736 /**
737 * Converts an array of bytes into an instance of class {@code Class}.
738 * Before the {@code Class} can be used it must be resolved. This method
739 * is deprecated in favor of the version that takes a <a
740 * href="#name">binary name</a> as its first argument, and is more secure.
741 *
742 * @param b
743 * The bytes that make up the class data. The bytes in positions
744 * {@code off} through {@code off+len-1} should have the format
745 * of a valid class file as defined by
746 * <cite>The Java™ Virtual Machine Specification</cite>.
747 *
748 * @param off
749 * The start offset in {@code b} of the class data
750 *
751 * @param len
752 * The length of the class data
753 *
754 * @return The {@code Class} object that was created from the specified
755 * class data
756 *
757 * @throws ClassFormatError
758 * If the data did not contain a valid class
759 *
760 * @throws IndexOutOfBoundsException
787 *
788 * <p> This method assigns a default {@link java.security.ProtectionDomain
789 * ProtectionDomain} to the newly defined class. The
790 * {@code ProtectionDomain} is effectively granted the same set of
791 * permissions returned when {@link
792 * java.security.Policy#getPermissions(java.security.CodeSource)
793 * Policy.getPolicy().getPermissions(new CodeSource(null, null))}
794 * is invoked. The default protection domain is created on the first invocation
795 * of {@link #defineClass(String, byte[], int, int) defineClass},
796 * and re-used on subsequent invocations.
797 *
798 * <p> To assign a specific {@code ProtectionDomain} to the class, use
799 * the {@link #defineClass(String, byte[], int, int,
800 * java.security.ProtectionDomain) defineClass} method that takes a
801 * {@code ProtectionDomain} as one of its arguments. </p>
802 *
803 * <p>
804 * This method defines a package in this class loader corresponding to the
805 * package of the {@code Class} (if such a package has not already been defined
806 * in this class loader). The name of the defined package is derived from
807 * the <a href="#name">binary name</a> of the class specified by
808 * the byte array {@code b}.
809 * Other properties of the defined package are as specified by {@link Package}.
810 *
811 * @param name
812 * The expected <a href="#name">binary name</a> of the class, or
813 * {@code null} if not known
814 *
815 * @param b
816 * The bytes that make up the class data. The bytes in positions
817 * {@code off} through {@code off+len-1} should have the format
818 * of a valid class file as defined by
819 * <cite>The Java™ Virtual Machine Specification</cite>.
820 *
821 * @param off
822 * The start offset in {@code b} of the class data
823 *
824 * @param len
825 * The length of the class data
826 *
827 * @return The {@code Class} object that was created from the specified
828 * class data.
829 *
830 * @throws ClassFormatError
831 * If the data did not contain a valid class
832 *
906 }
907 }
908
909 /**
910 * Converts an array of bytes into an instance of class {@code Class},
911 * with a given {@code ProtectionDomain}.
912 *
913 * <p> If the given {@code ProtectionDomain} is {@code null},
914 * then a default protection domain will be assigned to the class as specified
915 * in the documentation for {@link #defineClass(String, byte[], int, int)}.
916 * Before the class can be used it must be resolved.
917 *
918 * <p> The first class defined in a package determines the exact set of
919 * certificates that all subsequent classes defined in that package must
920 * contain. The set of certificates for a class is obtained from the
921 * {@link java.security.CodeSource CodeSource} within the
922 * {@code ProtectionDomain} of the class. Any classes added to that
923 * package must contain the same set of certificates or a
924 * {@code SecurityException} will be thrown. Note that if
925 * {@code name} is {@code null}, this check is not performed.
926 * You should always pass in the <a href="#name">binary name</a> of the
927 * class you are defining as well as the bytes. This ensures that the
928 * class you are defining is indeed the class you think it is.
929 *
930 * <p> If the specified {@code name} begins with "{@code java.}", it can
931 * only be defined by the {@linkplain #getPlatformClassLoader()
932 * platform class loader} or its ancestors; otherwise {@code SecurityException}
933 * will be thrown. If {@code name} is not {@code null}, it must be equal to
934 * the <a href="#name">binary name</a> of the class
935 * specified by the byte array {@code b}, otherwise a {@link
936 * NoClassDefFoundError NoClassDefFoundError} will be thrown.
937 *
938 * <p> This method defines a package in this class loader corresponding to the
939 * package of the {@code Class} (if such a package has not already been defined
940 * in this class loader). The name of the defined package is derived from
941 * the <a href="#name">binary name</a> of the class specified by
942 * the byte array {@code b}.
943 * Other properties of the defined package are as specified by {@link Package}.
944 *
945 * @param name
946 * The expected <a href="#name">binary name</a> of the class, or
947 * {@code null} if not known
948 *
949 * @param b
950 * The bytes that make up the class data. The bytes in positions
951 * {@code off} through {@code off+len-1} should have the format
952 * of a valid class file as defined by
953 * <cite>The Java™ Virtual Machine Specification</cite>.
954 *
955 * @param off
956 * The start offset in {@code b} of the class data
957 *
958 * @param len
959 * The length of the class data
960 *
961 * @param protectionDomain
962 * The {@code ProtectionDomain} of the class
963 *
964 * @return The {@code Class} object created from the data,
965 * and {@code ProtectionDomain}.
966 *
967 * @throws ClassFormatError
968 * If the data did not contain a valid class
969 *
970 * @throws NoClassDefFoundError
971 * If {@code name} is not {@code null} and not equal to the
972 * <a href="#name">binary name</a> of the class specified by {@code b}
973 *
974 * @throws IndexOutOfBoundsException
975 * If either {@code off} or {@code len} is negative, or if
976 * {@code off+len} is greater than {@code b.length}.
977 *
978 * @throws SecurityException
979 * If an attempt is made to add this class to a package that
980 * contains classes that were signed by a different set of
981 * certificates than this class, or if {@code name} begins with
982 * "{@code java.}" and this class loader is not the platform
983 * class loader or its ancestor.
984 *
985 * @revised 9
986 * @spec JPMS
987 */
988 protected final Class<?> defineClass(String name, byte[] b, int off, int len,
989 ProtectionDomain protectionDomain)
990 throws ClassFormatError
991 {
992 protectionDomain = preDefineClass(name, protectionDomain);
1010 * are identical to those specified in the documentation for {@link
1011 * #defineClass(String, byte[], int, int, ProtectionDomain)}.
1012 *
1013 * <p> An invocation of this method of the form
1014 * <i>cl</i>{@code .defineClass(}<i>name</i>{@code ,}
1015 * <i>bBuffer</i>{@code ,} <i>pd</i>{@code )} yields exactly the same
1016 * result as the statements
1017 *
1018 *<p> <code>
1019 * ...<br>
1020 * byte[] temp = new byte[bBuffer.{@link
1021 * java.nio.ByteBuffer#remaining remaining}()];<br>
1022 * bBuffer.{@link java.nio.ByteBuffer#get(byte[])
1023 * get}(temp);<br>
1024 * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
1025 * cl.defineClass}(name, temp, 0,
1026 * temp.length, pd);<br>
1027 * </code></p>
1028 *
1029 * @param name
1030 * The expected <a href="#name">binary name</a>. of the class, or
1031 * {@code null} if not known
1032 *
1033 * @param b
1034 * The bytes that make up the class data. The bytes from positions
1035 * {@code b.position()} through {@code b.position() + b.limit() -1
1036 * } should have the format of a valid class file as defined by
1037 * <cite>The Java™ Virtual Machine Specification</cite>.
1038 *
1039 * @param protectionDomain
1040 * The {@code ProtectionDomain} of the class, or {@code null}.
1041 *
1042 * @return The {@code Class} object created from the data,
1043 * and {@code ProtectionDomain}.
1044 *
1045 * @throws ClassFormatError
1046 * If the data did not contain a valid class.
1047 *
1048 * @throws NoClassDefFoundError
1049 * If {@code name} is not {@code null} and not equal to the
1050 * <a href="#name">binary name</a> of the class specified by {@code b}
1051 *
1052 * @throws SecurityException
1053 * If an attempt is made to add this class to a package that
1054 * contains classes that were signed by a different set of
1055 * certificates than this class, or if {@code name} begins with
1056 * "{@code java.}".
1057 *
1058 * @see #defineClass(String, byte[], int, int, ProtectionDomain)
1059 *
1060 * @since 1.5
1061 * @revised 9
1062 * @spec JPMS
1063 */
1064 protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
1065 ProtectionDomain protectionDomain)
1066 throws ClassFormatError
1067 {
1068 int len = b.remaining();
1069
1070 // Use byte[] if not a direct ByteBuffer:
1181 * used by a class loader to link a class. If the class {@code c} has
1182 * already been linked, then this method simply returns. Otherwise, the
1183 * class is linked as described in the "Execution" chapter of
1184 * <cite>The Java™ Language Specification</cite>.
1185 *
1186 * @param c
1187 * The class to link
1188 *
1189 * @throws NullPointerException
1190 * If {@code c} is {@code null}.
1191 *
1192 * @see #defineClass(String, byte[], int, int)
1193 */
1194 protected final void resolveClass(Class<?> c) {
1195 if (c == null) {
1196 throw new NullPointerException();
1197 }
1198 }
1199
1200 /**
1201 * Finds a class with the specified <a href="#name">binary name</a>,
1202 * loading it if necessary.
1203 *
1204 * <p> This method loads the class through the system class loader (see
1205 * {@link #getSystemClassLoader()}). The {@code Class} object returned
1206 * might have more than one {@code ClassLoader} associated with it.
1207 * Subclasses of {@code ClassLoader} need not usually invoke this method,
1208 * because most class loaders need to override just {@link
1209 * #findClass(String)}. </p>
1210 *
1211 * @param name
1212 * The <a href="#name">binary name</a> of the class
1213 *
1214 * @return The {@code Class} object for the specified {@code name}
1215 *
1216 * @throws ClassNotFoundException
1217 * If the class could not be found
1218 *
1219 * @see #ClassLoader(ClassLoader)
1220 * @see #getParent()
1221 */
1222 protected final Class<?> findSystemClass(String name)
1223 throws ClassNotFoundException
1224 {
1225 return getSystemClassLoader().loadClass(name);
1226 }
1227
1228 /**
1229 * Returns a class loaded by the bootstrap class loader;
1230 * or return null if not found.
1231 */
1232 Class<?> findBootstrapClassOrNull(String name) {
1233 if (!checkName(name)) return null;
1234
1235 return findBootstrapClass(name);
1236 }
1237
1238 // return null if not found
1239 private native Class<?> findBootstrapClass(String name);
1240
1241 /**
1242 * Returns the class with the given <a href="#name">binary name</a> if this
1243 * loader has been recorded by the Java virtual machine as an initiating
1244 * loader of a class with that <a href="#name">binary name</a>. Otherwise
1245 * {@code null} is returned.
1246 *
1247 * @param name
1248 * The <a href="#name">binary name</a> of the class
1249 *
1250 * @return The {@code Class} object, or {@code null} if the class has
1251 * not been loaded
1252 *
1253 * @since 1.1
1254 */
1255 protected final Class<?> findLoadedClass(String name) {
1256 if (!checkName(name))
1257 return null;
1258 return findLoadedClass0(name);
1259 }
1260
1261 private final native Class<?> findLoadedClass0(String name);
1262
1263 /**
1264 * Sets the signers of a class. This should be invoked after defining a
1265 * class.
1266 *
1267 * @param c
1268 * The {@code Class} object
2070
2071 return (Package)packages.compute(name, (n, p) -> toPackage(n, p, m));
2072 }
2073
2074 /*
2075 * Returns a Package object for the named package
2076 */
2077 private Package toPackage(String name, NamedPackage p, Module m) {
2078 // define Package object if the named package is not yet defined
2079 if (p == null)
2080 return NamedPackage.toPackage(name, m);
2081
2082 // otherwise, replace the NamedPackage object with Package object
2083 if (p instanceof Package)
2084 return (Package)p;
2085
2086 return NamedPackage.toPackage(p.packageName(), p.module());
2087 }
2088
2089 /**
2090 * Defines a package by <a href="#name">name</a> in this {@code ClassLoader}.
2091 * <p>
2092 * <a href="#name">Package names</a> must be unique within a class loader and
2093 * cannot be redefined or changed once created.
2094 * <p>
2095 * If a class loader wishes to define a package with specific properties,
2096 * such as version information, then the class loader should call this
2097 * {@code definePackage} method before calling {@code defineClass}.
2098 * Otherwise, the
2099 * {@link #defineClass(String, byte[], int, int, ProtectionDomain) defineClass}
2100 * method will define a package in this class loader corresponding to the package
2101 * of the newly defined class; the properties of this defined package are
2102 * specified by {@link Package}.
2103 *
2104 * @apiNote
2105 * A class loader that wishes to define a package for classes in a JAR
2106 * typically uses the specification and implementation titles, versions, and
2107 * vendors from the JAR's manifest. If the package is specified as
2108 * {@linkplain java.util.jar.Attributes.Name#SEALED sealed} in the JAR's manifest,
2109 * the {@code URL} of the JAR file is typically used as the {@code sealBase}.
2110 * If classes of package {@code 'p'} defined by this class loader
2111 * are loaded from multiple JARs, the {@code Package} object may contain
2112 * different information depending on the first class of package {@code 'p'}
2113 * defined and which JAR's manifest is read first to explicitly define
2114 * package {@code 'p'}.
2115 *
2116 * <p> It is strongly recommended that a class loader does not call this
2117 * method to explicitly define packages in <em>named modules</em>; instead,
2118 * the package will be automatically defined when a class is {@linkplain
2119 * #defineClass(String, byte[], int, int, ProtectionDomain) being defined}.
2120 * If it is desirable to define {@code Package} explicitly, it should ensure
2121 * that all packages in a named module are defined with the properties
2122 * specified by {@link Package}. Otherwise, some {@code Package} objects
2123 * in a named module may be for example sealed with different seal base.
2124 *
2125 * @param name
2126 * The <a href="#name">package name</a>
2127 *
2128 * @param specTitle
2129 * The specification title
2130 *
2131 * @param specVersion
2132 * The specification version
2133 *
2134 * @param specVendor
2135 * The specification vendor
2136 *
2137 * @param implTitle
2138 * The implementation title
2139 *
2140 * @param implVersion
2141 * The implementation version
2142 *
2143 * @param implVendor
2144 * The implementation vendor
2145 *
2146 * @param sealBase
2168 */
2169 protected Package definePackage(String name, String specTitle,
2170 String specVersion, String specVendor,
2171 String implTitle, String implVersion,
2172 String implVendor, URL sealBase)
2173 {
2174 Objects.requireNonNull(name);
2175
2176 // definePackage is not final and may be overridden by custom class loader
2177 Package p = new Package(name, specTitle, specVersion, specVendor,
2178 implTitle, implVersion, implVendor,
2179 sealBase, this);
2180
2181 if (packages.putIfAbsent(name, p) != null)
2182 throw new IllegalArgumentException(name);
2183
2184 return p;
2185 }
2186
2187 /**
2188 * Returns a {@code Package} of the given <a href="#name">name</a> that
2189 * has been defined by this class loader.
2190 *
2191 * @param name The <a href="#name">package name</a>
2192 *
2193 * @return The {@code Package} of the given name that has been defined
2194 * by this class loader, or {@code null} if not found
2195 *
2196 * @throws NullPointerException
2197 * if {@code name} is {@code null}.
2198 *
2199 * @jvms 5.3 Run-time package
2200 *
2201 * @since 9
2202 * @spec JPMS
2203 */
2204 public final Package getDefinedPackage(String name) {
2205 Objects.requireNonNull(name, "name cannot be null");
2206
2207 NamedPackage p = packages.get(name);
2208 if (p == null)
2209 return null;
2210
2211 return definePackage(name, p.module());
2216 * this class loader. The returned array has no duplicated {@code Package}s
2217 * of the same name.
2218 *
2219 * @apiNote This method returns an array rather than a {@code Set} or {@code Stream}
2220 * for consistency with the existing {@link #getPackages} method.
2221 *
2222 * @return The array of {@code Package} objects that have been defined by
2223 * this class loader; or an zero length array if no package has been
2224 * defined by this class loader.
2225 *
2226 * @jvms 5.3 Run-time package
2227 *
2228 * @since 9
2229 * @spec JPMS
2230 */
2231 public final Package[] getDefinedPackages() {
2232 return packages().toArray(Package[]::new);
2233 }
2234
2235 /**
2236 * Finds a package by <a href="#name">name</a> in this class loader and its ancestors.
2237 * <p>
2238 * If this class loader defines a {@code Package} of the given name,
2239 * the {@code Package} is returned. Otherwise, the ancestors of
2240 * this class loader are searched recursively (parent by parent)
2241 * for a {@code Package} of the given name.
2242 *
2243 * @apiNote The {@link #getPlatformClassLoader() platform class loader}
2244 * may delegate to the application class loader but the application class
2245 * loader is not its ancestor. When invoked on the platform class loader,
2246 * this method will not find packages defined to the application
2247 * class loader.
2248 *
2249 * @param name
2250 * The <a href="#name">package name</a>
2251 *
2252 * @return The {@code Package} of the given name that has been defined by
2253 * this class loader or its ancestors, or {@code null} if not found.
2254 *
2255 * @throws NullPointerException
2256 * if {@code name} is {@code null}.
2257 *
2258 * @deprecated
2259 * If multiple class loaders delegate to each other and define classes
2260 * with the same package name, and one such loader relies on the lookup
2261 * behavior of {@code getPackage} to return a {@code Package} from
2262 * a parent loader, then the properties exposed by the {@code Package}
2263 * may not be as expected in the rest of the program.
2264 * For example, the {@code Package} will only expose annotations from the
2265 * {@code package-info.class} file defined by the parent loader, even if
2266 * annotations exist in a {@code package-info.class} file defined by
2267 * a child loader. A more robust approach is to use the
2268 * {@link ClassLoader#getDefinedPackage} method which returns
2269 * a {@code Package} for the specified class loader.
2270 *
|
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
82 * Class#getClassLoader() reference} to the {@code ClassLoader} that defined
83 * it.
84 *
85 * <p> {@code Class} objects for array classes are not created by class
86 * loaders, but are created automatically as required by the Java runtime.
87 * The class loader for an array class, as returned by {@link
88 * Class#getClassLoader()} is the same as the class loader for its element
89 * type; if the element type is a primitive type, then the array class has no
90 * class loader.
91 *
92 * <p> Applications implement subclasses of {@code ClassLoader} in order to
93 * extend the manner in which the Java virtual machine dynamically loads
94 * classes.
95 *
96 * <p> Class loaders may typically be used by security managers to indicate
185 * it should use the method {@link #defineClass defineClass} to
186 * create a class instance. A sample implementation is:
187 *
188 * <blockquote><pre>
189 * class NetworkClassLoader extends ClassLoader {
190 * String host;
191 * int port;
192 *
193 * public Class findClass(String name) {
194 * byte[] b = loadClassData(name);
195 * return defineClass(name, b, 0, b.length);
196 * }
197 *
198 * private byte[] loadClassData(String name) {
199 * // load the class data from the connection
200 * . . .
201 * }
202 * }
203 * </pre></blockquote>
204 *
205 * <h3> <a id="binary-name">Binary names</a> </h3>
206 *
207 * <p> Any class name provided as a {@code String} parameter to methods in
208 * {@code ClassLoader} must be a binary name as defined by
209 * <cite>The Java™ Language Specification</cite>.
210 *
211 * <p> Examples of valid class names include:
212 * <blockquote><pre>
213 * "java.lang.String"
214 * "javax.swing.JSpinner$DefaultEditor"
215 * "java.security.KeyStore$Builder$FileBuilder$1"
216 * "java.net.URLClassLoader$3$1"
217 * </pre></blockquote>
218 *
219 * <p> Any package name provided as a {@code String} parameter to methods in
220 * {@code ClassLoader} must be either the empty string (denoting an unnamed package)
221 * or a fully qualified name as defined by
222 * <cite>The Java™ Language Specification</cite>.
223 *
224 * @jls 6.7 Fully Qualified Names
225 * @jls 13.1 The Form of a Binary
463 *
464 * @return name of this class loader; or {@code null} if
465 * this class loader is not named.
466 *
467 * @since 9
468 * @spec JPMS
469 */
470 public String getName() {
471 return name;
472 }
473
474 // package-private used by StackTraceElement to avoid
475 // calling the overrideable getName method
476 final String name() {
477 return name;
478 }
479
480 // -- Class --
481
482 /**
483 * Loads the class with the specified <a href="#binary-name">binary name</a>.
484 * This method searches for classes in the same manner as the {@link
485 * #loadClass(String, boolean)} method. It is invoked by the Java virtual
486 * machine to resolve class references. Invoking this method is equivalent
487 * to invoking {@link #loadClass(String, boolean) loadClass(name,
488 * false)}.
489 *
490 * @param name
491 * The <a href="#binary-name">binary name</a> of the class
492 *
493 * @return The resulting {@code Class} object
494 *
495 * @throws ClassNotFoundException
496 * If the class was not found
497 */
498 public Class<?> loadClass(String name) throws ClassNotFoundException {
499 return loadClass(name, false);
500 }
501
502 /**
503 * Loads the class with the specified <a href="#binary-name">binary name</a>. The
504 * default implementation of this method searches for classes in the
505 * following order:
506 *
507 * <ol>
508 *
509 * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
510 * has already been loaded. </p></li>
511 *
512 * <li><p> Invoke the {@link #loadClass(String) loadClass} method
513 * on the parent class loader. If the parent is {@code null} the class
514 * loader built into the virtual machine is used, instead. </p></li>
515 *
516 * <li><p> Invoke the {@link #findClass(String)} method to find the
517 * class. </p></li>
518 *
519 * </ol>
520 *
521 * <p> If the class was found using the above steps, and the
522 * {@code resolve} flag is true, this method will then invoke the {@link
523 * #resolveClass(Class)} method on the resulting {@code Class} object.
524 *
525 * <p> Subclasses of {@code ClassLoader} are encouraged to override {@link
526 * #findClass(String)}, rather than this method. </p>
527 *
528 * <p> Unless overridden, this method synchronizes on the result of
529 * {@link #getClassLoadingLock getClassLoadingLock} method
530 * during the entire class loading process.
531 *
532 * @param name
533 * The <a href="#binary-name">binary name</a> of the class
534 *
535 * @param resolve
536 * If {@code true} then resolve the class
537 *
538 * @return The resulting {@code Class} object
539 *
540 * @throws ClassNotFoundException
541 * If the class could not be found
542 */
543 protected Class<?> loadClass(String name, boolean resolve)
544 throws ClassNotFoundException
545 {
546 synchronized (getClassLoadingLock(name)) {
547 // First, check if the class has already been loaded
548 Class<?> c = findLoadedClass(name);
549 if (c == null) {
550 long t0 = System.nanoTime();
551 try {
552 if (parent != null) {
553 c = parent.loadClass(name, false);
562 if (c == null) {
563 // If still not found, then invoke findClass in order
564 // to find the class.
565 long t1 = System.nanoTime();
566 c = findClass(name);
567
568 // this is the defining class loader; record the stats
569 PerfCounter.getParentDelegationTime().addTime(t1 - t0);
570 PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
571 PerfCounter.getFindClasses().increment();
572 }
573 }
574 if (resolve) {
575 resolveClass(c);
576 }
577 return c;
578 }
579 }
580
581 /**
582 * Loads the class with the specified <a href="#binary-name">binary name</a>
583 * in a module defined to this class loader. This method returns {@code null}
584 * if the class could not be found.
585 *
586 * @apiNote This method does not delegate to the parent class loader.
587 *
588 * @implSpec The default implementation of this method searches for classes
589 * in the following order:
590 *
591 * <ol>
592 * <li>Invoke {@link #findLoadedClass(String)} to check if the class
593 * has already been loaded.</li>
594 * <li>Invoke the {@link #findClass(String, String)} method to find the
595 * class in the given module.</li>
596 * </ol>
597 *
598 * @param module
599 * The module
600 * @param name
601 * The <a href="#binary-name">binary name</a> of the class
602 *
603 * @return The resulting {@code Class} object in a module defined by
604 * this class loader, or {@code null} if the class could not be found.
605 */
606 final Class<?> loadClass(Module module, String name) {
607 synchronized (getClassLoadingLock(name)) {
608 // First, check if the class has already been loaded
609 Class<?> c = findLoadedClass(name);
610 if (c == null) {
611 c = findClass(module.getName(), name);
612 }
613 if (c != null && c.getModule() == module) {
614 return c;
615 } else {
616 return null;
617 }
618 }
619 }
620
621 /**
657 if (ReflectUtil.isNonPublicProxyClass(cls)) {
658 for (Class<?> intf: cls.getInterfaces()) {
659 checkPackageAccess(intf, pd);
660 }
661 return;
662 }
663
664 final String packageName = cls.getPackageName();
665 if (!packageName.isEmpty()) {
666 AccessController.doPrivileged(new PrivilegedAction<>() {
667 public Void run() {
668 sm.checkPackageAccess(packageName);
669 return null;
670 }
671 }, new AccessControlContext(new ProtectionDomain[] {pd}));
672 }
673 }
674 }
675
676 /**
677 * Finds the class with the specified <a href="#binary-name">binary name</a>.
678 * This method should be overridden by class loader implementations that
679 * follow the delegation model for loading classes, and will be invoked by
680 * the {@link #loadClass loadClass} method after checking the
681 * parent class loader for the requested class.
682 *
683 * @implSpec The default implementation throws {@code ClassNotFoundException}.
684 *
685 * @param name
686 * The <a href="#binary-name">binary name</a> of the class
687 *
688 * @return The resulting {@code Class} object
689 *
690 * @throws ClassNotFoundException
691 * If the class could not be found
692 *
693 * @since 1.2
694 */
695 protected Class<?> findClass(String name) throws ClassNotFoundException {
696 throw new ClassNotFoundException(name);
697 }
698
699 /**
700 * Finds the class with the given <a href="#binary-name">binary name</a>
701 * in a module defined to this class loader.
702 * Class loader implementations that support the loading from modules
703 * should override this method.
704 *
705 * @apiNote This method returns {@code null} rather than throwing
706 * {@code ClassNotFoundException} if the class could not be found.
707 *
708 * @implSpec The default implementation attempts to find the class by
709 * invoking {@link #findClass(String)} when the {@code moduleName} is
710 * {@code null}. It otherwise returns {@code null}.
711 *
712 * @param moduleName
713 * The module name; or {@code null} to find the class in the
714 * {@linkplain #getUnnamedModule() unnamed module} for this
715 * class loader
716
717 * @param name
718 * The <a href="#binary-name">binary name</a> of the class
719 *
720 * @return The resulting {@code Class} object, or {@code null}
721 * if the class could not be found.
722 *
723 * @since 9
724 * @spec JPMS
725 */
726 protected Class<?> findClass(String moduleName, String name) {
727 if (moduleName == null) {
728 try {
729 return findClass(name);
730 } catch (ClassNotFoundException ignore) { }
731 }
732 return null;
733 }
734
735
736 /**
737 * Converts an array of bytes into an instance of class {@code Class}.
738 * Before the {@code Class} can be used it must be resolved. This method
739 * is deprecated in favor of the version that takes a <a
740 * href="#binary-name">binary name</a> as its first argument, and is more secure.
741 *
742 * @param b
743 * The bytes that make up the class data. The bytes in positions
744 * {@code off} through {@code off+len-1} should have the format
745 * of a valid class file as defined by
746 * <cite>The Java™ Virtual Machine Specification</cite>.
747 *
748 * @param off
749 * The start offset in {@code b} of the class data
750 *
751 * @param len
752 * The length of the class data
753 *
754 * @return The {@code Class} object that was created from the specified
755 * class data
756 *
757 * @throws ClassFormatError
758 * If the data did not contain a valid class
759 *
760 * @throws IndexOutOfBoundsException
787 *
788 * <p> This method assigns a default {@link java.security.ProtectionDomain
789 * ProtectionDomain} to the newly defined class. The
790 * {@code ProtectionDomain} is effectively granted the same set of
791 * permissions returned when {@link
792 * java.security.Policy#getPermissions(java.security.CodeSource)
793 * Policy.getPolicy().getPermissions(new CodeSource(null, null))}
794 * is invoked. The default protection domain is created on the first invocation
795 * of {@link #defineClass(String, byte[], int, int) defineClass},
796 * and re-used on subsequent invocations.
797 *
798 * <p> To assign a specific {@code ProtectionDomain} to the class, use
799 * the {@link #defineClass(String, byte[], int, int,
800 * java.security.ProtectionDomain) defineClass} method that takes a
801 * {@code ProtectionDomain} as one of its arguments. </p>
802 *
803 * <p>
804 * This method defines a package in this class loader corresponding to the
805 * package of the {@code Class} (if such a package has not already been defined
806 * in this class loader). The name of the defined package is derived from
807 * the <a href="#binary-name">binary name</a> of the class specified by
808 * the byte array {@code b}.
809 * Other properties of the defined package are as specified by {@link Package}.
810 *
811 * @param name
812 * The expected <a href="#binary-name">binary name</a> of the class, or
813 * {@code null} if not known
814 *
815 * @param b
816 * The bytes that make up the class data. The bytes in positions
817 * {@code off} through {@code off+len-1} should have the format
818 * of a valid class file as defined by
819 * <cite>The Java™ Virtual Machine Specification</cite>.
820 *
821 * @param off
822 * The start offset in {@code b} of the class data
823 *
824 * @param len
825 * The length of the class data
826 *
827 * @return The {@code Class} object that was created from the specified
828 * class data.
829 *
830 * @throws ClassFormatError
831 * If the data did not contain a valid class
832 *
906 }
907 }
908
909 /**
910 * Converts an array of bytes into an instance of class {@code Class},
911 * with a given {@code ProtectionDomain}.
912 *
913 * <p> If the given {@code ProtectionDomain} is {@code null},
914 * then a default protection domain will be assigned to the class as specified
915 * in the documentation for {@link #defineClass(String, byte[], int, int)}.
916 * Before the class can be used it must be resolved.
917 *
918 * <p> The first class defined in a package determines the exact set of
919 * certificates that all subsequent classes defined in that package must
920 * contain. The set of certificates for a class is obtained from the
921 * {@link java.security.CodeSource CodeSource} within the
922 * {@code ProtectionDomain} of the class. Any classes added to that
923 * package must contain the same set of certificates or a
924 * {@code SecurityException} will be thrown. Note that if
925 * {@code name} is {@code null}, this check is not performed.
926 * You should always pass in the <a href="#binary-name">binary name</a> of the
927 * class you are defining as well as the bytes. This ensures that the
928 * class you are defining is indeed the class you think it is.
929 *
930 * <p> If the specified {@code name} begins with "{@code java.}", it can
931 * only be defined by the {@linkplain #getPlatformClassLoader()
932 * platform class loader} or its ancestors; otherwise {@code SecurityException}
933 * will be thrown. If {@code name} is not {@code null}, it must be equal to
934 * the <a href="#binary-name">binary name</a> of the class
935 * specified by the byte array {@code b}, otherwise a {@link
936 * NoClassDefFoundError NoClassDefFoundError} will be thrown.
937 *
938 * <p> This method defines a package in this class loader corresponding to the
939 * package of the {@code Class} (if such a package has not already been defined
940 * in this class loader). The name of the defined package is derived from
941 * the <a href="#binary-name">binary name</a> of the class specified by
942 * the byte array {@code b}.
943 * Other properties of the defined package are as specified by {@link Package}.
944 *
945 * @param name
946 * The expected <a href="#binary-name">binary name</a> of the class, or
947 * {@code null} if not known
948 *
949 * @param b
950 * The bytes that make up the class data. The bytes in positions
951 * {@code off} through {@code off+len-1} should have the format
952 * of a valid class file as defined by
953 * <cite>The Java™ Virtual Machine Specification</cite>.
954 *
955 * @param off
956 * The start offset in {@code b} of the class data
957 *
958 * @param len
959 * The length of the class data
960 *
961 * @param protectionDomain
962 * The {@code ProtectionDomain} of the class
963 *
964 * @return The {@code Class} object created from the data,
965 * and {@code ProtectionDomain}.
966 *
967 * @throws ClassFormatError
968 * If the data did not contain a valid class
969 *
970 * @throws NoClassDefFoundError
971 * If {@code name} is not {@code null} and not equal to the
972 * <a href="#binary-name">binary name</a> of the class specified by {@code b}
973 *
974 * @throws IndexOutOfBoundsException
975 * If either {@code off} or {@code len} is negative, or if
976 * {@code off+len} is greater than {@code b.length}.
977 *
978 * @throws SecurityException
979 * If an attempt is made to add this class to a package that
980 * contains classes that were signed by a different set of
981 * certificates than this class, or if {@code name} begins with
982 * "{@code java.}" and this class loader is not the platform
983 * class loader or its ancestor.
984 *
985 * @revised 9
986 * @spec JPMS
987 */
988 protected final Class<?> defineClass(String name, byte[] b, int off, int len,
989 ProtectionDomain protectionDomain)
990 throws ClassFormatError
991 {
992 protectionDomain = preDefineClass(name, protectionDomain);
1010 * are identical to those specified in the documentation for {@link
1011 * #defineClass(String, byte[], int, int, ProtectionDomain)}.
1012 *
1013 * <p> An invocation of this method of the form
1014 * <i>cl</i>{@code .defineClass(}<i>name</i>{@code ,}
1015 * <i>bBuffer</i>{@code ,} <i>pd</i>{@code )} yields exactly the same
1016 * result as the statements
1017 *
1018 *<p> <code>
1019 * ...<br>
1020 * byte[] temp = new byte[bBuffer.{@link
1021 * java.nio.ByteBuffer#remaining remaining}()];<br>
1022 * bBuffer.{@link java.nio.ByteBuffer#get(byte[])
1023 * get}(temp);<br>
1024 * return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
1025 * cl.defineClass}(name, temp, 0,
1026 * temp.length, pd);<br>
1027 * </code></p>
1028 *
1029 * @param name
1030 * The expected <a href="#binary-name">binary name</a>. of the class, or
1031 * {@code null} if not known
1032 *
1033 * @param b
1034 * The bytes that make up the class data. The bytes from positions
1035 * {@code b.position()} through {@code b.position() + b.limit() -1
1036 * } should have the format of a valid class file as defined by
1037 * <cite>The Java™ Virtual Machine Specification</cite>.
1038 *
1039 * @param protectionDomain
1040 * The {@code ProtectionDomain} of the class, or {@code null}.
1041 *
1042 * @return The {@code Class} object created from the data,
1043 * and {@code ProtectionDomain}.
1044 *
1045 * @throws ClassFormatError
1046 * If the data did not contain a valid class.
1047 *
1048 * @throws NoClassDefFoundError
1049 * If {@code name} is not {@code null} and not equal to the
1050 * <a href="#binary-name">binary name</a> of the class specified by {@code b}
1051 *
1052 * @throws SecurityException
1053 * If an attempt is made to add this class to a package that
1054 * contains classes that were signed by a different set of
1055 * certificates than this class, or if {@code name} begins with
1056 * "{@code java.}".
1057 *
1058 * @see #defineClass(String, byte[], int, int, ProtectionDomain)
1059 *
1060 * @since 1.5
1061 * @revised 9
1062 * @spec JPMS
1063 */
1064 protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
1065 ProtectionDomain protectionDomain)
1066 throws ClassFormatError
1067 {
1068 int len = b.remaining();
1069
1070 // Use byte[] if not a direct ByteBuffer:
1181 * used by a class loader to link a class. If the class {@code c} has
1182 * already been linked, then this method simply returns. Otherwise, the
1183 * class is linked as described in the "Execution" chapter of
1184 * <cite>The Java™ Language Specification</cite>.
1185 *
1186 * @param c
1187 * The class to link
1188 *
1189 * @throws NullPointerException
1190 * If {@code c} is {@code null}.
1191 *
1192 * @see #defineClass(String, byte[], int, int)
1193 */
1194 protected final void resolveClass(Class<?> c) {
1195 if (c == null) {
1196 throw new NullPointerException();
1197 }
1198 }
1199
1200 /**
1201 * Finds a class with the specified <a href="#binary-name">binary name</a>,
1202 * loading it if necessary.
1203 *
1204 * <p> This method loads the class through the system class loader (see
1205 * {@link #getSystemClassLoader()}). The {@code Class} object returned
1206 * might have more than one {@code ClassLoader} associated with it.
1207 * Subclasses of {@code ClassLoader} need not usually invoke this method,
1208 * because most class loaders need to override just {@link
1209 * #findClass(String)}. </p>
1210 *
1211 * @param name
1212 * The <a href="#binary-name">binary name</a> of the class
1213 *
1214 * @return The {@code Class} object for the specified {@code name}
1215 *
1216 * @throws ClassNotFoundException
1217 * If the class could not be found
1218 *
1219 * @see #ClassLoader(ClassLoader)
1220 * @see #getParent()
1221 */
1222 protected final Class<?> findSystemClass(String name)
1223 throws ClassNotFoundException
1224 {
1225 return getSystemClassLoader().loadClass(name);
1226 }
1227
1228 /**
1229 * Returns a class loaded by the bootstrap class loader;
1230 * or return null if not found.
1231 */
1232 Class<?> findBootstrapClassOrNull(String name) {
1233 if (!checkName(name)) return null;
1234
1235 return findBootstrapClass(name);
1236 }
1237
1238 // return null if not found
1239 private native Class<?> findBootstrapClass(String name);
1240
1241 /**
1242 * Returns the class with the given <a href="#binary-name">binary name</a> if this
1243 * loader has been recorded by the Java virtual machine as an initiating
1244 * loader of a class with that <a href="#binary-name">binary name</a>. Otherwise
1245 * {@code null} is returned.
1246 *
1247 * @param name
1248 * The <a href="#binary-name">binary name</a> of the class
1249 *
1250 * @return The {@code Class} object, or {@code null} if the class has
1251 * not been loaded
1252 *
1253 * @since 1.1
1254 */
1255 protected final Class<?> findLoadedClass(String name) {
1256 if (!checkName(name))
1257 return null;
1258 return findLoadedClass0(name);
1259 }
1260
1261 private final native Class<?> findLoadedClass0(String name);
1262
1263 /**
1264 * Sets the signers of a class. This should be invoked after defining a
1265 * class.
1266 *
1267 * @param c
1268 * The {@code Class} object
2070
2071 return (Package)packages.compute(name, (n, p) -> toPackage(n, p, m));
2072 }
2073
2074 /*
2075 * Returns a Package object for the named package
2076 */
2077 private Package toPackage(String name, NamedPackage p, Module m) {
2078 // define Package object if the named package is not yet defined
2079 if (p == null)
2080 return NamedPackage.toPackage(name, m);
2081
2082 // otherwise, replace the NamedPackage object with Package object
2083 if (p instanceof Package)
2084 return (Package)p;
2085
2086 return NamedPackage.toPackage(p.packageName(), p.module());
2087 }
2088
2089 /**
2090 * Defines a package by <a href="#binary-name">name</a> in this {@code ClassLoader}.
2091 * <p>
2092 * <a href="#binary-name">Package names</a> must be unique within a class loader and
2093 * cannot be redefined or changed once created.
2094 * <p>
2095 * If a class loader wishes to define a package with specific properties,
2096 * such as version information, then the class loader should call this
2097 * {@code definePackage} method before calling {@code defineClass}.
2098 * Otherwise, the
2099 * {@link #defineClass(String, byte[], int, int, ProtectionDomain) defineClass}
2100 * method will define a package in this class loader corresponding to the package
2101 * of the newly defined class; the properties of this defined package are
2102 * specified by {@link Package}.
2103 *
2104 * @apiNote
2105 * A class loader that wishes to define a package for classes in a JAR
2106 * typically uses the specification and implementation titles, versions, and
2107 * vendors from the JAR's manifest. If the package is specified as
2108 * {@linkplain java.util.jar.Attributes.Name#SEALED sealed} in the JAR's manifest,
2109 * the {@code URL} of the JAR file is typically used as the {@code sealBase}.
2110 * If classes of package {@code 'p'} defined by this class loader
2111 * are loaded from multiple JARs, the {@code Package} object may contain
2112 * different information depending on the first class of package {@code 'p'}
2113 * defined and which JAR's manifest is read first to explicitly define
2114 * package {@code 'p'}.
2115 *
2116 * <p> It is strongly recommended that a class loader does not call this
2117 * method to explicitly define packages in <em>named modules</em>; instead,
2118 * the package will be automatically defined when a class is {@linkplain
2119 * #defineClass(String, byte[], int, int, ProtectionDomain) being defined}.
2120 * If it is desirable to define {@code Package} explicitly, it should ensure
2121 * that all packages in a named module are defined with the properties
2122 * specified by {@link Package}. Otherwise, some {@code Package} objects
2123 * in a named module may be for example sealed with different seal base.
2124 *
2125 * @param name
2126 * The <a href="#binary-name">package name</a>
2127 *
2128 * @param specTitle
2129 * The specification title
2130 *
2131 * @param specVersion
2132 * The specification version
2133 *
2134 * @param specVendor
2135 * The specification vendor
2136 *
2137 * @param implTitle
2138 * The implementation title
2139 *
2140 * @param implVersion
2141 * The implementation version
2142 *
2143 * @param implVendor
2144 * The implementation vendor
2145 *
2146 * @param sealBase
2168 */
2169 protected Package definePackage(String name, String specTitle,
2170 String specVersion, String specVendor,
2171 String implTitle, String implVersion,
2172 String implVendor, URL sealBase)
2173 {
2174 Objects.requireNonNull(name);
2175
2176 // definePackage is not final and may be overridden by custom class loader
2177 Package p = new Package(name, specTitle, specVersion, specVendor,
2178 implTitle, implVersion, implVendor,
2179 sealBase, this);
2180
2181 if (packages.putIfAbsent(name, p) != null)
2182 throw new IllegalArgumentException(name);
2183
2184 return p;
2185 }
2186
2187 /**
2188 * Returns a {@code Package} of the given <a href="#binary-name">name</a> that
2189 * has been defined by this class loader.
2190 *
2191 * @param name The <a href="#binary-name">package name</a>
2192 *
2193 * @return The {@code Package} of the given name that has been defined
2194 * by this class loader, or {@code null} if not found
2195 *
2196 * @throws NullPointerException
2197 * if {@code name} is {@code null}.
2198 *
2199 * @jvms 5.3 Run-time package
2200 *
2201 * @since 9
2202 * @spec JPMS
2203 */
2204 public final Package getDefinedPackage(String name) {
2205 Objects.requireNonNull(name, "name cannot be null");
2206
2207 NamedPackage p = packages.get(name);
2208 if (p == null)
2209 return null;
2210
2211 return definePackage(name, p.module());
2216 * this class loader. The returned array has no duplicated {@code Package}s
2217 * of the same name.
2218 *
2219 * @apiNote This method returns an array rather than a {@code Set} or {@code Stream}
2220 * for consistency with the existing {@link #getPackages} method.
2221 *
2222 * @return The array of {@code Package} objects that have been defined by
2223 * this class loader; or an zero length array if no package has been
2224 * defined by this class loader.
2225 *
2226 * @jvms 5.3 Run-time package
2227 *
2228 * @since 9
2229 * @spec JPMS
2230 */
2231 public final Package[] getDefinedPackages() {
2232 return packages().toArray(Package[]::new);
2233 }
2234
2235 /**
2236 * Finds a package by <a href="#binary-name">name</a> in this class loader and its ancestors.
2237 * <p>
2238 * If this class loader defines a {@code Package} of the given name,
2239 * the {@code Package} is returned. Otherwise, the ancestors of
2240 * this class loader are searched recursively (parent by parent)
2241 * for a {@code Package} of the given name.
2242 *
2243 * @apiNote The {@link #getPlatformClassLoader() platform class loader}
2244 * may delegate to the application class loader but the application class
2245 * loader is not its ancestor. When invoked on the platform class loader,
2246 * this method will not find packages defined to the application
2247 * class loader.
2248 *
2249 * @param name
2250 * The <a href="#binary-name">package name</a>
2251 *
2252 * @return The {@code Package} of the given name that has been defined by
2253 * this class loader or its ancestors, or {@code null} if not found.
2254 *
2255 * @throws NullPointerException
2256 * if {@code name} is {@code null}.
2257 *
2258 * @deprecated
2259 * If multiple class loaders delegate to each other and define classes
2260 * with the same package name, and one such loader relies on the lookup
2261 * behavior of {@code getPackage} to return a {@code Package} from
2262 * a parent loader, then the properties exposed by the {@code Package}
2263 * may not be as expected in the rest of the program.
2264 * For example, the {@code Package} will only expose annotations from the
2265 * {@code package-info.class} file defined by the parent loader, even if
2266 * annotations exist in a {@code package-info.class} file defined by
2267 * a child loader. A more robust approach is to use the
2268 * {@link ClassLoader#getDefinedPackage} method which returns
2269 * a {@code Package} for the specified class loader.
2270 *
|