src/java.base/share/classes/java/net/URLClassLoader.java

Print this page
rev 10608 : 8057936: java.net.URLClassLoader.findClass uses exceptions in control flow
Reviewed-by: michaelm, mr


 339      * @return the search path of URLs for loading classes and resources.
 340      */
 341     public URL[] getURLs() {
 342         return ucp.getURLs();
 343     }
 344 
 345     /**
 346      * Finds and loads the class with the specified name from the URL search
 347      * path. Any URLs referring to JAR files are loaded and opened as needed
 348      * until the class is found.
 349      *
 350      * @param name the name of the class
 351      * @return the resulting class
 352      * @exception ClassNotFoundException if the class could not be found,
 353      *            or if the loader is closed.
 354      * @exception NullPointerException if {@code name} is {@code null}.
 355      */
 356     protected Class<?> findClass(final String name)
 357          throws ClassNotFoundException
 358     {

 359         try {
 360             return AccessController.doPrivileged(
 361                 new PrivilegedExceptionAction<Class<?>>() {
 362                     public Class<?> run() throws ClassNotFoundException {
 363                         String path = name.replace('.', '/').concat(".class");
 364                         Resource res = ucp.getResource(path, false);
 365                         if (res != null) {
 366                             try {
 367                                 return defineClass(name, res);
 368                             } catch (IOException e) {
 369                                 throw new ClassNotFoundException(name, e);
 370                             }
 371                         } else {
 372                             throw new ClassNotFoundException(name);
 373                         }
 374                     }
 375                 }, acc);
 376         } catch (java.security.PrivilegedActionException pae) {
 377             throw (ClassNotFoundException) pae.getException();
 378         }




 379     }
 380 
 381     /*
 382      * Retrieve the package using the specified package name.
 383      * If non-null, verify the package using the specified code
 384      * source and manifest.
 385      */
 386     private Package getAndVerifyPackage(String pkgname,
 387                                         Manifest man, URL url) {
 388         Package pkg = getPackage(pkgname);
 389         if (pkg != null) {
 390             // Package found, so check package sealing.
 391             if (pkg.isSealed()) {
 392                 // Verify that code source URL is the same.
 393                 if (!pkg.isSealed(url)) {
 394                     throw new SecurityException(
 395                         "sealing violation: package " + pkgname + " is sealed");
 396                 }
 397             } else {
 398                 // Make sure we are not attempting to seal the package




 339      * @return the search path of URLs for loading classes and resources.
 340      */
 341     public URL[] getURLs() {
 342         return ucp.getURLs();
 343     }
 344 
 345     /**
 346      * Finds and loads the class with the specified name from the URL search
 347      * path. Any URLs referring to JAR files are loaded and opened as needed
 348      * until the class is found.
 349      *
 350      * @param name the name of the class
 351      * @return the resulting class
 352      * @exception ClassNotFoundException if the class could not be found,
 353      *            or if the loader is closed.
 354      * @exception NullPointerException if {@code name} is {@code null}.
 355      */
 356     protected Class<?> findClass(final String name)
 357         throws ClassNotFoundException
 358     {
 359         final Class<?> result;
 360         try {
 361             result = AccessController.doPrivileged(
 362                 new PrivilegedExceptionAction<Class<?>>() {
 363                     public Class<?> run() throws ClassNotFoundException {
 364                         String path = name.replace('.', '/').concat(".class");
 365                         Resource res = ucp.getResource(path, false);
 366                         if (res != null) {
 367                             try {
 368                                 return defineClass(name, res);
 369                             } catch (IOException e) {
 370                                 throw new ClassNotFoundException(name, e);
 371                             }
 372                         } else {
 373                             return null;
 374                         }
 375                     }
 376                 }, acc);
 377         } catch (java.security.PrivilegedActionException pae) {
 378             throw (ClassNotFoundException) pae.getException();
 379         }
 380         if (result == null) {
 381             throw new ClassNotFoundException(name);
 382         }
 383         return result;
 384     }
 385 
 386     /*
 387      * Retrieve the package using the specified package name.
 388      * If non-null, verify the package using the specified code
 389      * source and manifest.
 390      */
 391     private Package getAndVerifyPackage(String pkgname,
 392                                         Manifest man, URL url) {
 393         Package pkg = getPackage(pkgname);
 394         if (pkg != null) {
 395             // Package found, so check package sealing.
 396             if (pkg.isSealed()) {
 397                 // Verify that code source URL is the same.
 398                 if (!pkg.isSealed(url)) {
 399                     throw new SecurityException(
 400                         "sealing violation: package " + pkgname + " is sealed");
 401                 }
 402             } else {
 403                 // Make sure we are not attempting to seal the package