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

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import java.io.InputStream;
  28 import java.io.IOException;
  29 import java.io.File;
  30 import java.lang.reflect.Constructor;
  31 import java.lang.reflect.InvocationTargetException;
  32 import java.net.MalformedURLException;
  33 import java.net.URL;
  34 import java.security.AccessController;
  35 import java.security.AccessControlContext;
  36 import java.security.CodeSource;
  37 import java.security.Policy;
  38 import java.security.PrivilegedAction;
  39 import java.security.PrivilegedActionException;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.ProtectionDomain;
  42 import java.security.cert.Certificate;
  43 import java.util.Collections;
  44 import java.util.Enumeration;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.Set;
  48 import java.util.Stack;
  49 import java.util.Map;
  50 import java.util.Vector;
  51 import java.util.Hashtable;
  52 import java.util.WeakHashMap;
  53 import java.util.concurrent.ConcurrentHashMap;
  54 import sun.misc.CompoundEnumeration;
  55 import sun.misc.Resource;
  56 import sun.misc.URLClassPath;
  57 import sun.misc.VM;
  58 import sun.reflect.CallerSensitive;
  59 import sun.reflect.Reflection;
  60 import sun.reflect.misc.ReflectUtil;
  61 import sun.security.util.SecurityConstants;
  62 
  63 /**
  64  * A class loader is an object that is responsible for loading classes. The
  65  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  66  * href="#name">binary name</a> of a class, a class loader should attempt to
  67  * locate or generate data that constitutes a definition for the class.  A
  68  * typical strategy is to transform the name into a file name and then read a
  69  * "class file" of that name from a file system.
  70  *
  71  * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
  72  * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
  73  * it.
  74  *
  75  * <p> <tt>Class</tt> objects for array classes are not created by class
  76  * loaders, but are created automatically as required by the Java runtime.
  77  * The class loader for an array class, as returned by {@link


 339 
 340     // -- Class --
 341 
 342     /**
 343      * Loads the class with the specified <a href="#name">binary name</a>.
 344      * This method searches for classes in the same manner as the {@link
 345      * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
 346      * machine to resolve class references.  Invoking this method is equivalent
 347      * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
 348      * false)</tt>}.
 349      *
 350      * @param  name
 351      *         The <a href="#name">binary name</a> of the class
 352      *
 353      * @return  The resulting <tt>Class</tt> object
 354      *
 355      * @throws  ClassNotFoundException
 356      *          If the class was not found
 357      */
 358     public Class<?> loadClass(String name) throws ClassNotFoundException {

 359         return loadClass(name, false);









 360     }
 361 
 362     /**
 363      * Loads the class with the specified <a href="#name">binary name</a>.  The
 364      * default implementation of this method searches for classes in the
 365      * following order:
 366      *
 367      * <ol>
 368      *
 369      *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
 370      *   has already been loaded.  </p></li>
 371      *
 372      *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
 373      *   on the parent class loader.  If the parent is <tt>null</tt> the class
 374      *   loader built-in to the virtual machine is used, instead.  </p></li>
 375      *
 376      *   <li><p> Invoke the {@link #findClass(String)} method to find the
 377      *   class.  </p></li>
 378      *
 379      * </ol>


 512 
 513     /**
 514      * Finds the class with the specified <a href="#name">binary name</a>.
 515      * This method should be overridden by class loader implementations that
 516      * follow the delegation model for loading classes, and will be invoked by
 517      * the {@link #loadClass <tt>loadClass</tt>} method after checking the
 518      * parent class loader for the requested class.  The default implementation
 519      * throws a <tt>ClassNotFoundException</tt>.
 520      *
 521      * @param  name
 522      *         The <a href="#name">binary name</a> of the class
 523      *
 524      * @return  The resulting <tt>Class</tt> object
 525      *
 526      * @throws  ClassNotFoundException
 527      *          If the class could not be found
 528      *
 529      * @since  1.2
 530      */
 531     protected Class<?> findClass(String name) throws ClassNotFoundException {
 532         throw new ClassNotFoundException(name);




 533     }
 534 
 535     /**
 536      * Converts an array of bytes into an instance of class <tt>Class</tt>.
 537      * Before the <tt>Class</tt> can be used it must be resolved.  This method
 538      * is deprecated in favor of the version that takes a <a
 539      * href="#name">binary name</a> as its first argument, and is more secure.
 540      *
 541      * @param  b
 542      *         The bytes that make up the class data.  The bytes in positions
 543      *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
 544      *         of a valid class file as defined by
 545      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 546      *
 547      * @param  off
 548      *         The start offset in <tt>b</tt> of the class data
 549      *
 550      * @param  len
 551      *         The length of the class data
 552      *




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import java.io.InputStream;
  28 import java.io.IOException;
  29 import java.io.File;
  30 import java.lang.reflect.Constructor;
  31 import java.lang.reflect.InvocationTargetException;

  32 import java.net.URL;
  33 import java.security.AccessController;
  34 import java.security.AccessControlContext;
  35 import java.security.CodeSource;

  36 import java.security.PrivilegedAction;
  37 import java.security.PrivilegedActionException;
  38 import java.security.PrivilegedExceptionAction;
  39 import java.security.ProtectionDomain;
  40 import java.security.cert.Certificate;
  41 import java.util.Collections;
  42 import java.util.Enumeration;
  43 import java.util.HashMap;
  44 import java.util.HashSet;
  45 import java.util.Set;
  46 import java.util.Stack;
  47 import java.util.Map;
  48 import java.util.Vector;
  49 import java.util.Hashtable;
  50 import java.util.WeakHashMap;
  51 import java.util.concurrent.ConcurrentHashMap;
  52 import sun.misc.CompoundEnumeration;
  53 import sun.misc.Resource;
  54 import sun.misc.URLClassPath;

  55 import sun.reflect.CallerSensitive;
  56 import sun.reflect.Reflection;
  57 import sun.reflect.misc.ReflectUtil;
  58 import sun.security.util.SecurityConstants;
  59 
  60 /**
  61  * A class loader is an object that is responsible for loading classes. The
  62  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  63  * href="#name">binary name</a> of a class, a class loader should attempt to
  64  * locate or generate data that constitutes a definition for the class.  A
  65  * typical strategy is to transform the name into a file name and then read a
  66  * "class file" of that name from a file system.
  67  *
  68  * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
  69  * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
  70  * it.
  71  *
  72  * <p> <tt>Class</tt> objects for array classes are not created by class
  73  * loaders, but are created automatically as required by the Java runtime.
  74  * The class loader for an array class, as returned by {@link


 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         try {
 357             return loadClass(name, false);
 358         } catch (ClassNotFoundException e) {
 359             if (e.getStackTraceDepth() > 0) {
 360                 // we've got an exception with stack trace - rethrow it
 361                 throw e;
 362             } else {
 363                 // we've got a stack-less exception - replace it
 364                 throw new ClassNotFoundException(e.getMessage(), e.getException());
 365             }
 366         }
 367     }
 368 
 369     /**
 370      * Loads the class with the specified <a href="#name">binary name</a>.  The
 371      * default implementation of this method searches for classes in the
 372      * following order:
 373      *
 374      * <ol>
 375      *
 376      *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
 377      *   has already been loaded.  </p></li>
 378      *
 379      *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
 380      *   on the parent class loader.  If the parent is <tt>null</tt> the class
 381      *   loader built-in to the virtual machine is used, instead.  </p></li>
 382      *
 383      *   <li><p> Invoke the {@link #findClass(String)} method to find the
 384      *   class.  </p></li>
 385      *
 386      * </ol>


 519 
 520     /**
 521      * Finds the class with the specified <a href="#name">binary name</a>.
 522      * This method should be overridden by class loader implementations that
 523      * follow the delegation model for loading classes, and will be invoked by
 524      * the {@link #loadClass <tt>loadClass</tt>} method after checking the
 525      * parent class loader for the requested class.  The default implementation
 526      * throws a <tt>ClassNotFoundException</tt>.
 527      *
 528      * @param  name
 529      *         The <a href="#name">binary name</a> of the class
 530      *
 531      * @return  The resulting <tt>Class</tt> object
 532      *
 533      * @throws  ClassNotFoundException
 534      *          If the class could not be found
 535      *
 536      * @since  1.2
 537      */
 538     protected Class<?> findClass(String name) throws ClassNotFoundException {
 539         throw new ClassNotFoundException(
 540             name + " (thrown by: " + getClass().getName() + ")",
 541             null,
 542             false
 543         );
 544     }
 545 
 546     /**
 547      * Converts an array of bytes into an instance of class <tt>Class</tt>.
 548      * Before the <tt>Class</tt> can be used it must be resolved.  This method
 549      * is deprecated in favor of the version that takes a <a
 550      * href="#name">binary name</a> as its first argument, and is more secure.
 551      *
 552      * @param  b
 553      *         The bytes that make up the class data.  The bytes in positions
 554      *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
 555      *         of a valid class file as defined by
 556      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
 557      *
 558      * @param  off
 559      *         The start offset in <tt>b</tt> of the class data
 560      *
 561      * @param  len
 562      *         The length of the class data
 563      *