< prev index next >

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

Print this page
rev 60127 : 8249205: Remove unnecessary trademark symbols


 190  *     class NetworkClassLoader extends ClassLoader {
 191  *         String host;
 192  *         int port;
 193  *
 194  *         public Class findClass(String name) {
 195  *             byte[] b = loadClassData(name);
 196  *             return defineClass(name, b, 0, b.length);
 197  *         }
 198  *
 199  *         private byte[] loadClassData(String name) {
 200  *             // load the class data from the connection
 201  *             &nbsp;.&nbsp;.&nbsp;.
 202  *         }
 203  *     }
 204  * </pre></blockquote>
 205  *
 206  * <h3> <a id="binary-name">Binary names</a> </h3>
 207  *
 208  * <p> Any class name provided as a {@code String} parameter to methods in
 209  * {@code ClassLoader} must be a binary name as defined by
 210  * <cite>The Java&trade; Language Specification</cite>.
 211  *
 212  * <p> Examples of valid class names include:
 213  * <blockquote><pre>
 214  *   "java.lang.String"
 215  *   "javax.swing.JSpinner$DefaultEditor"
 216  *   "java.security.KeyStore$Builder$FileBuilder$1"
 217  *   "java.net.URLClassLoader$3$1"
 218  * </pre></blockquote>
 219  *
 220  * <p> Any package name provided as a {@code String} parameter to methods in
 221  * {@code ClassLoader} must be either the empty string (denoting an unnamed package)
 222  * or a fully qualified name as defined by
 223  * <cite>The Java&trade; Language Specification</cite>.
 224  *
 225  * @jls 6.7 Fully Qualified Names
 226  * @jls 13.1 The Form of a Binary
 227  * @see      #resolveClass(Class)
 228  * @since 1.0
 229  * @revised 9
 230  * @spec JPMS
 231  */
 232 public abstract class ClassLoader {
 233 
 234     private static native void registerNatives();
 235     static {
 236         registerNatives();
 237     }
 238 
 239     // The parent class loader for delegation
 240     // Note: VM hardcoded the offset of this field, thus all new fields
 241     // must be added *after* it.
 242     private final ClassLoader parent;
 243 


 748     protected Class<?> findClass(String moduleName, String name) {
 749         if (moduleName == null) {
 750             try {
 751                 return findClass(name);
 752             } catch (ClassNotFoundException ignore) { }
 753         }
 754         return null;
 755     }
 756 
 757 
 758     /**
 759      * Converts an array of bytes into an instance of class {@code Class}.
 760      * Before the {@code Class} can be used it must be resolved.  This method
 761      * is deprecated in favor of the version that takes a <a
 762      * href="#binary-name">binary name</a> as its first argument, and is more secure.
 763      *
 764      * @param  b
 765      *         The bytes that make up the class data.  The bytes in positions
 766      *         {@code off} through {@code off+len-1} should have the format
 767      *         of a valid class file as defined by
 768      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 769      *
 770      * @param  off
 771      *         The start offset in {@code b} of the class data
 772      *
 773      * @param  len
 774      *         The length of the class data
 775      *
 776      * @return  The {@code Class} object that was created from the specified
 777      *          class data
 778      *
 779      * @throws  ClassFormatError
 780      *          If the data did not contain a valid class
 781      *
 782      * @throws  IndexOutOfBoundsException
 783      *          If either {@code off} or {@code len} is negative, or if
 784      *          {@code off+len} is greater than {@code b.length}.
 785      *
 786      * @throws  SecurityException
 787      *          If an attempt is made to add this class to a package that
 788      *          contains classes that were signed by a different set of


 821      * the {@link #defineClass(String, byte[], int, int,
 822      * java.security.ProtectionDomain) defineClass} method that takes a
 823      * {@code ProtectionDomain} as one of its arguments.  </p>
 824      *
 825      * <p>
 826      * This method defines a package in this class loader corresponding to the
 827      * package of the {@code Class} (if such a package has not already been defined
 828      * in this class loader). The name of the defined package is derived from
 829      * the <a href="#binary-name">binary name</a> of the class specified by
 830      * the byte array {@code b}.
 831      * Other properties of the defined package are as specified by {@link Package}.
 832      *
 833      * @param  name
 834      *         The expected <a href="#binary-name">binary name</a> of the class, or
 835      *         {@code null} if not known
 836      *
 837      * @param  b
 838      *         The bytes that make up the class data.  The bytes in positions
 839      *         {@code off} through {@code off+len-1} should have the format
 840      *         of a valid class file as defined by
 841      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 842      *
 843      * @param  off
 844      *         The start offset in {@code b} of the class data
 845      *
 846      * @param  len
 847      *         The length of the class data
 848      *
 849      * @return  The {@code Class} object that was created from the specified
 850      *          class data.
 851      *
 852      * @throws  ClassFormatError
 853      *          If the data did not contain a valid class
 854      *
 855      * @throws  IndexOutOfBoundsException
 856      *          If either {@code off} or {@code len} is negative, or if
 857      *          {@code off+len} is greater than {@code b.length}.
 858      *
 859      * @throws  SecurityException
 860      *          If an attempt is made to add this class to a package that
 861      *          contains classes that were signed by a different set of


 955      * will be thrown.  If {@code name} is not {@code null}, it must be equal to
 956      * the <a href="#binary-name">binary name</a> of the class
 957      * specified by the byte array {@code b}, otherwise a {@link
 958      * NoClassDefFoundError NoClassDefFoundError} will be thrown.
 959      *
 960      * <p> This method defines a package in this class loader corresponding to the
 961      * package of the {@code Class} (if such a package has not already been defined
 962      * in this class loader). The name of the defined package is derived from
 963      * the <a href="#binary-name">binary name</a> of the class specified by
 964      * the byte array {@code b}.
 965      * Other properties of the defined package are as specified by {@link Package}.
 966      *
 967      * @param  name
 968      *         The expected <a href="#binary-name">binary name</a> of the class, or
 969      *         {@code null} if not known
 970      *
 971      * @param  b
 972      *         The bytes that make up the class data. The bytes in positions
 973      *         {@code off} through {@code off+len-1} should have the format
 974      *         of a valid class file as defined by
 975      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 976      *
 977      * @param  off
 978      *         The start offset in {@code b} of the class data
 979      *
 980      * @param  len
 981      *         The length of the class data
 982      *
 983      * @param  protectionDomain
 984      *         The {@code ProtectionDomain} of the class
 985      *
 986      * @return  The {@code Class} object created from the data,
 987      *          and {@code ProtectionDomain}.
 988      *
 989      * @throws  ClassFormatError
 990      *          If the data did not contain a valid class
 991      *
 992      * @throws  NoClassDefFoundError
 993      *          If {@code name} is not {@code null} and not equal to the
 994      *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
 995      *


1039      *
1040      *<p> <code>
1041      * ...<br>
1042      * byte[] temp = new byte[bBuffer.{@link
1043      * java.nio.ByteBuffer#remaining remaining}()];<br>
1044      *     bBuffer.{@link java.nio.ByteBuffer#get(byte[])
1045      * get}(temp);<br>
1046      *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
1047      * cl.defineClass}(name, temp, 0,
1048      * temp.length, pd);<br>
1049      * </code></p>
1050      *
1051      * @param  name
1052      *         The expected <a href="#binary-name">binary name</a>. of the class, or
1053      *         {@code null} if not known
1054      *
1055      * @param  b
1056      *         The bytes that make up the class data. The bytes from positions
1057      *         {@code b.position()} through {@code b.position() + b.limit() -1
1058      *         } should have the format of a valid class file as defined by
1059      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
1060      *
1061      * @param  protectionDomain
1062      *         The {@code ProtectionDomain} of the class, or {@code null}.
1063      *
1064      * @return  The {@code Class} object created from the data,
1065      *          and {@code ProtectionDomain}.
1066      *
1067      * @throws  ClassFormatError
1068      *          If the data did not contain a valid class.
1069      *
1070      * @throws  NoClassDefFoundError
1071      *          If {@code name} is not {@code null} and not equal to the
1072      *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
1073      *
1074      * @throws  SecurityException
1075      *          If an attempt is made to add this class to a package that
1076      *          contains classes that were signed by a different set of
1077      *          certificates than this class, or if {@code name} begins with
1078      *          "{@code java.}".
1079      *


1196         // now do the same for pcerts
1197         for (Certificate pcert : pcerts) {
1198             match = false;
1199             for (Certificate cert : certs) {
1200                 if (pcert.equals(cert)) {
1201                     match = true;
1202                     break;
1203                 }
1204             }
1205             if (!match) return false;
1206         }
1207 
1208         return true;
1209     }
1210 
1211     /**
1212      * Links the specified class.  This (misleadingly named) method may be
1213      * used by a class loader to link a class.  If the class {@code c} has
1214      * already been linked, then this method simply returns. Otherwise, the
1215      * class is linked as described in the "Execution" chapter of
1216      * <cite>The Java&trade; Language Specification</cite>.
1217      *
1218      * @param  c
1219      *         The class to link
1220      *
1221      * @throws  NullPointerException
1222      *          If {@code c} is {@code null}.
1223      *
1224      * @see  #defineClass(String, byte[], int, int)
1225      */
1226     protected final void resolveClass(Class<?> c) {
1227         if (c == null) {
1228             throw new NullPointerException();
1229         }
1230     }
1231 
1232     /**
1233      * Finds a class with the specified <a href="#binary-name">binary name</a>,
1234      * loading it if necessary.
1235      *
1236      * <p> This method loads the class through the system class loader (see


2515      * with "{@code p.}".  For example, {@code javax.swing.text} is a
2516      * subpackage of {@code javax.swing}, and both {@code java.util} and
2517      * {@code java.lang.reflect} are subpackages of {@code java}.
2518      *
2519      * <p> In the event that multiple package defaults apply to a given class,
2520      * the package default pertaining to the most specific package takes
2521      * precedence over the others.  For example, if {@code javax.lang} and
2522      * {@code javax.lang.reflect} both have package defaults associated with
2523      * them, the latter package default applies to classes in
2524      * {@code javax.lang.reflect}.
2525      *
2526      * <p> Package defaults take precedence over the class loader's default
2527      * assertion status, and may be overridden on a per-class basis by invoking
2528      * {@link #setClassAssertionStatus(String, boolean)}.  </p>
2529      *
2530      * @param  packageName
2531      *         The name of the package whose package default assertion status
2532      *         is to be set. A {@code null} value indicates the unnamed
2533      *         package that is "current"
2534      *         (see section 7.4.2 of
2535      *         <cite>The Java&trade; Language Specification</cite>.)
2536      *
2537      * @param  enabled
2538      *         {@code true} if classes loaded by this classloader and
2539      *         belonging to the named package or any of its subpackages will
2540      *         have assertions enabled by default, {@code false} if they will
2541      *         have assertions disabled by default.
2542      *
2543      * @since  1.4
2544      */
2545     public void setPackageAssertionStatus(String packageName,
2546                                           boolean enabled) {
2547         synchronized (assertionLock) {
2548             if (packageAssertionStatus == null)
2549                 initializeJavaAssertionMaps();
2550 
2551             packageAssertionStatus.put(packageName, enabled);
2552         }
2553     }
2554 
2555     /**




 190  *     class NetworkClassLoader extends ClassLoader {
 191  *         String host;
 192  *         int port;
 193  *
 194  *         public Class findClass(String name) {
 195  *             byte[] b = loadClassData(name);
 196  *             return defineClass(name, b, 0, b.length);
 197  *         }
 198  *
 199  *         private byte[] loadClassData(String name) {
 200  *             // load the class data from the connection
 201  *             &nbsp;.&nbsp;.&nbsp;.
 202  *         }
 203  *     }
 204  * </pre></blockquote>
 205  *
 206  * <h3> <a id="binary-name">Binary names</a> </h3>
 207  *
 208  * <p> Any class name provided as a {@code String} parameter to methods in
 209  * {@code ClassLoader} must be a binary name as defined by
 210  * <cite>The Java Language Specification</cite>.
 211  *
 212  * <p> Examples of valid class names include:
 213  * <blockquote><pre>
 214  *   "java.lang.String"
 215  *   "javax.swing.JSpinner$DefaultEditor"
 216  *   "java.security.KeyStore$Builder$FileBuilder$1"
 217  *   "java.net.URLClassLoader$3$1"
 218  * </pre></blockquote>
 219  *
 220  * <p> Any package name provided as a {@code String} parameter to methods in
 221  * {@code ClassLoader} must be either the empty string (denoting an unnamed package)
 222  * or a fully qualified name as defined by
 223  * <cite>The Java Language Specification</cite>.
 224  *
 225  * @jls 6.7 Fully Qualified Names
 226  * @jls 13.1 The Form of a Binary
 227  * @see      #resolveClass(Class)
 228  * @since 1.0
 229  * @revised 9
 230  * @spec JPMS
 231  */
 232 public abstract class ClassLoader {
 233 
 234     private static native void registerNatives();
 235     static {
 236         registerNatives();
 237     }
 238 
 239     // The parent class loader for delegation
 240     // Note: VM hardcoded the offset of this field, thus all new fields
 241     // must be added *after* it.
 242     private final ClassLoader parent;
 243 


 748     protected Class<?> findClass(String moduleName, String name) {
 749         if (moduleName == null) {
 750             try {
 751                 return findClass(name);
 752             } catch (ClassNotFoundException ignore) { }
 753         }
 754         return null;
 755     }
 756 
 757 
 758     /**
 759      * Converts an array of bytes into an instance of class {@code Class}.
 760      * Before the {@code Class} can be used it must be resolved.  This method
 761      * is deprecated in favor of the version that takes a <a
 762      * href="#binary-name">binary name</a> as its first argument, and is more secure.
 763      *
 764      * @param  b
 765      *         The bytes that make up the class data.  The bytes in positions
 766      *         {@code off} through {@code off+len-1} should have the format
 767      *         of a valid class file as defined by
 768      *         <cite>The Java Virtual Machine Specification</cite>.
 769      *
 770      * @param  off
 771      *         The start offset in {@code b} of the class data
 772      *
 773      * @param  len
 774      *         The length of the class data
 775      *
 776      * @return  The {@code Class} object that was created from the specified
 777      *          class data
 778      *
 779      * @throws  ClassFormatError
 780      *          If the data did not contain a valid class
 781      *
 782      * @throws  IndexOutOfBoundsException
 783      *          If either {@code off} or {@code len} is negative, or if
 784      *          {@code off+len} is greater than {@code b.length}.
 785      *
 786      * @throws  SecurityException
 787      *          If an attempt is made to add this class to a package that
 788      *          contains classes that were signed by a different set of


 821      * the {@link #defineClass(String, byte[], int, int,
 822      * java.security.ProtectionDomain) defineClass} method that takes a
 823      * {@code ProtectionDomain} as one of its arguments.  </p>
 824      *
 825      * <p>
 826      * This method defines a package in this class loader corresponding to the
 827      * package of the {@code Class} (if such a package has not already been defined
 828      * in this class loader). The name of the defined package is derived from
 829      * the <a href="#binary-name">binary name</a> of the class specified by
 830      * the byte array {@code b}.
 831      * Other properties of the defined package are as specified by {@link Package}.
 832      *
 833      * @param  name
 834      *         The expected <a href="#binary-name">binary name</a> of the class, or
 835      *         {@code null} if not known
 836      *
 837      * @param  b
 838      *         The bytes that make up the class data.  The bytes in positions
 839      *         {@code off} through {@code off+len-1} should have the format
 840      *         of a valid class file as defined by
 841      *         <cite>The Java Virtual Machine Specification</cite>.
 842      *
 843      * @param  off
 844      *         The start offset in {@code b} of the class data
 845      *
 846      * @param  len
 847      *         The length of the class data
 848      *
 849      * @return  The {@code Class} object that was created from the specified
 850      *          class data.
 851      *
 852      * @throws  ClassFormatError
 853      *          If the data did not contain a valid class
 854      *
 855      * @throws  IndexOutOfBoundsException
 856      *          If either {@code off} or {@code len} is negative, or if
 857      *          {@code off+len} is greater than {@code b.length}.
 858      *
 859      * @throws  SecurityException
 860      *          If an attempt is made to add this class to a package that
 861      *          contains classes that were signed by a different set of


 955      * will be thrown.  If {@code name} is not {@code null}, it must be equal to
 956      * the <a href="#binary-name">binary name</a> of the class
 957      * specified by the byte array {@code b}, otherwise a {@link
 958      * NoClassDefFoundError NoClassDefFoundError} will be thrown.
 959      *
 960      * <p> This method defines a package in this class loader corresponding to the
 961      * package of the {@code Class} (if such a package has not already been defined
 962      * in this class loader). The name of the defined package is derived from
 963      * the <a href="#binary-name">binary name</a> of the class specified by
 964      * the byte array {@code b}.
 965      * Other properties of the defined package are as specified by {@link Package}.
 966      *
 967      * @param  name
 968      *         The expected <a href="#binary-name">binary name</a> of the class, or
 969      *         {@code null} if not known
 970      *
 971      * @param  b
 972      *         The bytes that make up the class data. The bytes in positions
 973      *         {@code off} through {@code off+len-1} should have the format
 974      *         of a valid class file as defined by
 975      *         <cite>The Java Virtual Machine Specification</cite>.
 976      *
 977      * @param  off
 978      *         The start offset in {@code b} of the class data
 979      *
 980      * @param  len
 981      *         The length of the class data
 982      *
 983      * @param  protectionDomain
 984      *         The {@code ProtectionDomain} of the class
 985      *
 986      * @return  The {@code Class} object created from the data,
 987      *          and {@code ProtectionDomain}.
 988      *
 989      * @throws  ClassFormatError
 990      *          If the data did not contain a valid class
 991      *
 992      * @throws  NoClassDefFoundError
 993      *          If {@code name} is not {@code null} and not equal to the
 994      *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
 995      *


1039      *
1040      *<p> <code>
1041      * ...<br>
1042      * byte[] temp = new byte[bBuffer.{@link
1043      * java.nio.ByteBuffer#remaining remaining}()];<br>
1044      *     bBuffer.{@link java.nio.ByteBuffer#get(byte[])
1045      * get}(temp);<br>
1046      *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
1047      * cl.defineClass}(name, temp, 0,
1048      * temp.length, pd);<br>
1049      * </code></p>
1050      *
1051      * @param  name
1052      *         The expected <a href="#binary-name">binary name</a>. of the class, or
1053      *         {@code null} if not known
1054      *
1055      * @param  b
1056      *         The bytes that make up the class data. The bytes from positions
1057      *         {@code b.position()} through {@code b.position() + b.limit() -1
1058      *         } should have the format of a valid class file as defined by
1059      *         <cite>The Java Virtual Machine Specification</cite>.
1060      *
1061      * @param  protectionDomain
1062      *         The {@code ProtectionDomain} of the class, or {@code null}.
1063      *
1064      * @return  The {@code Class} object created from the data,
1065      *          and {@code ProtectionDomain}.
1066      *
1067      * @throws  ClassFormatError
1068      *          If the data did not contain a valid class.
1069      *
1070      * @throws  NoClassDefFoundError
1071      *          If {@code name} is not {@code null} and not equal to the
1072      *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
1073      *
1074      * @throws  SecurityException
1075      *          If an attempt is made to add this class to a package that
1076      *          contains classes that were signed by a different set of
1077      *          certificates than this class, or if {@code name} begins with
1078      *          "{@code java.}".
1079      *


1196         // now do the same for pcerts
1197         for (Certificate pcert : pcerts) {
1198             match = false;
1199             for (Certificate cert : certs) {
1200                 if (pcert.equals(cert)) {
1201                     match = true;
1202                     break;
1203                 }
1204             }
1205             if (!match) return false;
1206         }
1207 
1208         return true;
1209     }
1210 
1211     /**
1212      * Links the specified class.  This (misleadingly named) method may be
1213      * used by a class loader to link a class.  If the class {@code c} has
1214      * already been linked, then this method simply returns. Otherwise, the
1215      * class is linked as described in the "Execution" chapter of
1216      * <cite>The Java Language Specification</cite>.
1217      *
1218      * @param  c
1219      *         The class to link
1220      *
1221      * @throws  NullPointerException
1222      *          If {@code c} is {@code null}.
1223      *
1224      * @see  #defineClass(String, byte[], int, int)
1225      */
1226     protected final void resolveClass(Class<?> c) {
1227         if (c == null) {
1228             throw new NullPointerException();
1229         }
1230     }
1231 
1232     /**
1233      * Finds a class with the specified <a href="#binary-name">binary name</a>,
1234      * loading it if necessary.
1235      *
1236      * <p> This method loads the class through the system class loader (see


2515      * with "{@code p.}".  For example, {@code javax.swing.text} is a
2516      * subpackage of {@code javax.swing}, and both {@code java.util} and
2517      * {@code java.lang.reflect} are subpackages of {@code java}.
2518      *
2519      * <p> In the event that multiple package defaults apply to a given class,
2520      * the package default pertaining to the most specific package takes
2521      * precedence over the others.  For example, if {@code javax.lang} and
2522      * {@code javax.lang.reflect} both have package defaults associated with
2523      * them, the latter package default applies to classes in
2524      * {@code javax.lang.reflect}.
2525      *
2526      * <p> Package defaults take precedence over the class loader's default
2527      * assertion status, and may be overridden on a per-class basis by invoking
2528      * {@link #setClassAssertionStatus(String, boolean)}.  </p>
2529      *
2530      * @param  packageName
2531      *         The name of the package whose package default assertion status
2532      *         is to be set. A {@code null} value indicates the unnamed
2533      *         package that is "current"
2534      *         (see section 7.4.2 of
2535      *         <cite>The Java Language Specification</cite>.)
2536      *
2537      * @param  enabled
2538      *         {@code true} if classes loaded by this classloader and
2539      *         belonging to the named package or any of its subpackages will
2540      *         have assertions enabled by default, {@code false} if they will
2541      *         have assertions disabled by default.
2542      *
2543      * @since  1.4
2544      */
2545     public void setPackageAssertionStatus(String packageName,
2546                                           boolean enabled) {
2547         synchronized (assertionLock) {
2548             if (packageAssertionStatus == null)
2549                 initializeJavaAssertionMaps();
2550 
2551             packageAssertionStatus.put(packageName, enabled);
2552         }
2553     }
2554 
2555     /**


< prev index next >