< prev index next >

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

Print this page
8198810: URLClassLoader does not specify behavior when URL array contains null
Reviewed-by: alanb, darcy, dholmes


  88     private final AccessControlContext acc;
  89 
  90     /**
  91      * Constructs a new URLClassLoader for the given URLs. The URLs will be
  92      * searched in the order specified for classes and resources after first
  93      * searching in the specified parent class loader.  Any {@code jar:}
  94      * scheme URL is assumed to refer to a JAR file.  Any {@code file:} scheme
  95      * URL that ends with a '/' is assumed to refer to a directory.  Otherwise,
  96      * the URL is assumed to refer to a JAR file which will be downloaded and
  97      * opened as needed.
  98      *
  99      * <p>If there is a security manager, this method first
 100      * calls the security manager's {@code checkCreateClassLoader} method
 101      * to ensure creation of a class loader is allowed.
 102      *
 103      * @param urls the URLs from which to load classes and resources
 104      * @param parent the parent class loader for delegation
 105      * @exception  SecurityException  if a security manager exists and its
 106      *             {@code checkCreateClassLoader} method doesn't allow
 107      *             creation of a class loader.
 108      * @exception  NullPointerException if {@code urls} is {@code null}.

 109      * @see SecurityManager#checkCreateClassLoader
 110      */
 111     public URLClassLoader(URL[] urls, ClassLoader parent) {
 112         super(parent);
 113         // this is to make the stack depth consistent with 1.1
 114         SecurityManager security = System.getSecurityManager();
 115         if (security != null) {
 116             security.checkCreateClassLoader();
 117         }
 118         this.acc = AccessController.getContext();
 119         this.ucp = new URLClassPath(urls, acc);
 120     }
 121 
 122     URLClassLoader(String name, URL[] urls, ClassLoader parent,
 123                    AccessControlContext acc) {
 124         super(name, parent);
 125         // this is to make the stack depth consistent with 1.1
 126         SecurityManager security = System.getSecurityManager();
 127         if (security != null) {
 128             security.checkCreateClassLoader();


 132     }
 133 
 134     /**
 135      * Constructs a new URLClassLoader for the specified URLs using the
 136      * default delegation parent {@code ClassLoader}. The URLs will
 137      * be searched in the order specified for classes and resources after
 138      * first searching in the parent class loader. Any URL that ends with
 139      * a '/' is assumed to refer to a directory. Otherwise, the URL is
 140      * assumed to refer to a JAR file which will be downloaded and opened
 141      * as needed.
 142      *
 143      * <p>If there is a security manager, this method first
 144      * calls the security manager's {@code checkCreateClassLoader} method
 145      * to ensure creation of a class loader is allowed.
 146      *
 147      * @param urls the URLs from which to load classes and resources
 148      *
 149      * @exception  SecurityException  if a security manager exists and its
 150      *             {@code checkCreateClassLoader} method doesn't allow
 151      *             creation of a class loader.
 152      * @exception  NullPointerException if {@code urls} is {@code null}.

 153      * @see SecurityManager#checkCreateClassLoader
 154      */
 155     public URLClassLoader(URL[] urls) {
 156         super();
 157         // this is to make the stack depth consistent with 1.1
 158         SecurityManager security = System.getSecurityManager();
 159         if (security != null) {
 160             security.checkCreateClassLoader();
 161         }
 162         this.acc = AccessController.getContext();
 163         this.ucp = new URLClassPath(urls, acc);
 164     }
 165 
 166     URLClassLoader(URL[] urls, AccessControlContext acc) {
 167         super();
 168         // this is to make the stack depth consistent with 1.1
 169         SecurityManager security = System.getSecurityManager();
 170         if (security != null) {
 171             security.checkCreateClassLoader();
 172         }


 175     }
 176 
 177     /**
 178      * Constructs a new URLClassLoader for the specified URLs, parent
 179      * class loader, and URLStreamHandlerFactory. The parent argument
 180      * will be used as the parent class loader for delegation. The
 181      * factory argument will be used as the stream handler factory to
 182      * obtain protocol handlers when creating new jar URLs.
 183      *
 184      * <p>If there is a security manager, this method first
 185      * calls the security manager's {@code checkCreateClassLoader} method
 186      * to ensure creation of a class loader is allowed.
 187      *
 188      * @param urls the URLs from which to load classes and resources
 189      * @param parent the parent class loader for delegation
 190      * @param factory the URLStreamHandlerFactory to use when creating URLs
 191      *
 192      * @exception  SecurityException  if a security manager exists and its
 193      *             {@code checkCreateClassLoader} method doesn't allow
 194      *             creation of a class loader.
 195      * @exception  NullPointerException if {@code urls} is {@code null}.

 196      * @see SecurityManager#checkCreateClassLoader
 197      */
 198     public URLClassLoader(URL[] urls, ClassLoader parent,
 199                           URLStreamHandlerFactory factory) {
 200         super(parent);
 201         // this is to make the stack depth consistent with 1.1
 202         SecurityManager security = System.getSecurityManager();
 203         if (security != null) {
 204             security.checkCreateClassLoader();
 205         }
 206         this.acc = AccessController.getContext();
 207         this.ucp = new URLClassPath(urls, factory, acc);
 208     }
 209 
 210 
 211     /**
 212      * Constructs a new named {@code URLClassLoader} for the specified URLs.
 213      * The URLs will be searched in the order specified for classes
 214      * and resources after first searching in the specified parent class loader.
 215      * Any URL that ends with a '/' is assumed to refer to a directory.
 216      * Otherwise, the URL is assumed to refer to a JAR file which will be
 217      * downloaded and opened as needed.
 218      *
 219      * @param  name class loader name; or {@code null} if not named
 220      * @param  urls the URLs from which to load classes and resources
 221      * @param  parent the parent class loader for delegation
 222      *
 223      * @throws IllegalArgumentException if the given name is empty.
 224      * @throws NullPointerException if {@code urls} is {@code null}.

 225      *
 226      * @throws SecurityException if a security manager exists and its
 227      *         {@link SecurityManager#checkCreateClassLoader()} method doesn't
 228      *         allow creation of a class loader.
 229      *
 230      * @since 9
 231      * @spec JPMS
 232      */
 233     public URLClassLoader(String name,
 234                           URL[] urls,
 235                           ClassLoader parent) {
 236         super(name, parent);
 237         // this is to make the stack depth consistent with 1.1
 238         SecurityManager security = System.getSecurityManager();
 239         if (security != null) {
 240             security.checkCreateClassLoader();
 241         }
 242         this.acc = AccessController.getContext();
 243         this.ucp = new URLClassPath(urls, acc);
 244     }
 245 
 246     /**
 247      * Constructs a new named {@code URLClassLoader} for the specified URLs,
 248      * parent class loader, and URLStreamHandlerFactory.
 249      * The parent argument will be used as the parent class loader for delegation.
 250      * The factory argument will be used as the stream handler factory to
 251      * obtain protocol handlers when creating new jar URLs.
 252      *
 253      * @param  name class loader name; or {@code null} if not named
 254      * @param  urls the URLs from which to load classes and resources
 255      * @param  parent the parent class loader for delegation
 256      * @param  factory the URLStreamHandlerFactory to use when creating URLs
 257      *
 258      * @throws IllegalArgumentException if the given name is empty.
 259      * @throws NullPointerException if {@code urls} is {@code null}.

 260      *
 261      * @throws SecurityException if a security manager exists and its
 262      *         {@code checkCreateClassLoader} method doesn't allow
 263      *         creation of a class loader.
 264      *
 265      * @since 9
 266      * @spec JPMS
 267      */
 268     public URLClassLoader(String name, URL[] urls, ClassLoader parent,
 269                           URLStreamHandlerFactory factory) {
 270         super(name, parent);
 271         // this is to make the stack depth consistent with 1.1
 272         SecurityManager security = System.getSecurityManager();
 273         if (security != null) {
 274             security.checkCreateClassLoader();
 275         }
 276         this.acc = AccessController.getContext();
 277         this.ucp = new URLClassPath(urls, factory, acc);
 278     }
 279 


 788                         sm.checkPermission(fp);
 789                         return null;
 790                     }
 791                 }, acc);
 792             }
 793             perms.add(p);
 794         }
 795         return perms;
 796     }
 797 
 798     /**
 799      * Creates a new instance of URLClassLoader for the specified
 800      * URLs and parent class loader. If a security manager is
 801      * installed, the {@code loadClass} method of the URLClassLoader
 802      * returned by this method will invoke the
 803      * {@code SecurityManager.checkPackageAccess} method before
 804      * loading the class.
 805      *
 806      * @param urls the URLs to search for classes and resources
 807      * @param parent the parent class loader for delegation
 808      * @exception  NullPointerException if {@code urls} is {@code null}.

 809      * @return the resulting class loader
 810      */
 811     public static URLClassLoader newInstance(final URL[] urls,
 812                                              final ClassLoader parent) {
 813         // Save the caller's context
 814         final AccessControlContext acc = AccessController.getContext();
 815         // Need a privileged block to create the class loader
 816         URLClassLoader ucl = AccessController.doPrivileged(
 817             new PrivilegedAction<>() {
 818                 public URLClassLoader run() {
 819                     return new FactoryURLClassLoader(null, urls, parent, acc);
 820                 }
 821             });
 822         return ucl;
 823     }
 824 
 825     /**
 826      * Creates a new instance of URLClassLoader for the specified
 827      * URLs and default parent class loader. If a security manager is
 828      * installed, the {@code loadClass} method of the URLClassLoader
 829      * returned by this method will invoke the
 830      * {@code SecurityManager.checkPackageAccess} before
 831      * loading the class.
 832      *
 833      * @param urls the URLs to search for classes and resources
 834      * @exception  NullPointerException if {@code urls} is {@code null}.

 835      * @return the resulting class loader
 836      */
 837     public static URLClassLoader newInstance(final URL[] urls) {
 838         // Save the caller's context
 839         final AccessControlContext acc = AccessController.getContext();
 840         // Need a privileged block to create the class loader
 841         URLClassLoader ucl = AccessController.doPrivileged(
 842             new PrivilegedAction<>() {
 843                 public URLClassLoader run() {
 844                     return new FactoryURLClassLoader(urls, acc);
 845                 }
 846             });
 847         return ucl;
 848     }
 849 
 850     static {
 851         SharedSecrets.setJavaNetURLClassLoaderAccess(
 852             new JavaNetURLClassLoaderAccess() {
 853                 @Override
 854                 public AccessControlContext getAccessControlContext(URLClassLoader u) {




  88     private final AccessControlContext acc;
  89 
  90     /**
  91      * Constructs a new URLClassLoader for the given URLs. The URLs will be
  92      * searched in the order specified for classes and resources after first
  93      * searching in the specified parent class loader.  Any {@code jar:}
  94      * scheme URL is assumed to refer to a JAR file.  Any {@code file:} scheme
  95      * URL that ends with a '/' is assumed to refer to a directory.  Otherwise,
  96      * the URL is assumed to refer to a JAR file which will be downloaded and
  97      * opened as needed.
  98      *
  99      * <p>If there is a security manager, this method first
 100      * calls the security manager's {@code checkCreateClassLoader} method
 101      * to ensure creation of a class loader is allowed.
 102      *
 103      * @param urls the URLs from which to load classes and resources
 104      * @param parent the parent class loader for delegation
 105      * @exception  SecurityException  if a security manager exists and its
 106      *             {@code checkCreateClassLoader} method doesn't allow
 107      *             creation of a class loader.
 108      * @exception  NullPointerException if {@code urls} or any of its
 109      *             elements is {@code null}.
 110      * @see SecurityManager#checkCreateClassLoader
 111      */
 112     public URLClassLoader(URL[] urls, ClassLoader parent) {
 113         super(parent);
 114         // this is to make the stack depth consistent with 1.1
 115         SecurityManager security = System.getSecurityManager();
 116         if (security != null) {
 117             security.checkCreateClassLoader();
 118         }
 119         this.acc = AccessController.getContext();
 120         this.ucp = new URLClassPath(urls, acc);
 121     }
 122 
 123     URLClassLoader(String name, URL[] urls, ClassLoader parent,
 124                    AccessControlContext acc) {
 125         super(name, parent);
 126         // this is to make the stack depth consistent with 1.1
 127         SecurityManager security = System.getSecurityManager();
 128         if (security != null) {
 129             security.checkCreateClassLoader();


 133     }
 134 
 135     /**
 136      * Constructs a new URLClassLoader for the specified URLs using the
 137      * default delegation parent {@code ClassLoader}. The URLs will
 138      * be searched in the order specified for classes and resources after
 139      * first searching in the parent class loader. Any URL that ends with
 140      * a '/' is assumed to refer to a directory. Otherwise, the URL is
 141      * assumed to refer to a JAR file which will be downloaded and opened
 142      * as needed.
 143      *
 144      * <p>If there is a security manager, this method first
 145      * calls the security manager's {@code checkCreateClassLoader} method
 146      * to ensure creation of a class loader is allowed.
 147      *
 148      * @param urls the URLs from which to load classes and resources
 149      *
 150      * @exception  SecurityException  if a security manager exists and its
 151      *             {@code checkCreateClassLoader} method doesn't allow
 152      *             creation of a class loader.
 153      * @exception  NullPointerException if {@code urls} or any of its
 154      *             elements is {@code null}.
 155      * @see SecurityManager#checkCreateClassLoader
 156      */
 157     public URLClassLoader(URL[] urls) {
 158         super();
 159         // this is to make the stack depth consistent with 1.1
 160         SecurityManager security = System.getSecurityManager();
 161         if (security != null) {
 162             security.checkCreateClassLoader();
 163         }
 164         this.acc = AccessController.getContext();
 165         this.ucp = new URLClassPath(urls, acc);
 166     }
 167 
 168     URLClassLoader(URL[] urls, AccessControlContext acc) {
 169         super();
 170         // this is to make the stack depth consistent with 1.1
 171         SecurityManager security = System.getSecurityManager();
 172         if (security != null) {
 173             security.checkCreateClassLoader();
 174         }


 177     }
 178 
 179     /**
 180      * Constructs a new URLClassLoader for the specified URLs, parent
 181      * class loader, and URLStreamHandlerFactory. The parent argument
 182      * will be used as the parent class loader for delegation. The
 183      * factory argument will be used as the stream handler factory to
 184      * obtain protocol handlers when creating new jar URLs.
 185      *
 186      * <p>If there is a security manager, this method first
 187      * calls the security manager's {@code checkCreateClassLoader} method
 188      * to ensure creation of a class loader is allowed.
 189      *
 190      * @param urls the URLs from which to load classes and resources
 191      * @param parent the parent class loader for delegation
 192      * @param factory the URLStreamHandlerFactory to use when creating URLs
 193      *
 194      * @exception  SecurityException  if a security manager exists and its
 195      *             {@code checkCreateClassLoader} method doesn't allow
 196      *             creation of a class loader.
 197      * @exception  NullPointerException if {@code urls} or any of its
 198      *             elements is {@code null}.
 199      * @see SecurityManager#checkCreateClassLoader
 200      */
 201     public URLClassLoader(URL[] urls, ClassLoader parent,
 202                           URLStreamHandlerFactory factory) {
 203         super(parent);
 204         // this is to make the stack depth consistent with 1.1
 205         SecurityManager security = System.getSecurityManager();
 206         if (security != null) {
 207             security.checkCreateClassLoader();
 208         }
 209         this.acc = AccessController.getContext();
 210         this.ucp = new URLClassPath(urls, factory, acc);
 211     }
 212 
 213 
 214     /**
 215      * Constructs a new named {@code URLClassLoader} for the specified URLs.
 216      * The URLs will be searched in the order specified for classes
 217      * and resources after first searching in the specified parent class loader.
 218      * Any URL that ends with a '/' is assumed to refer to a directory.
 219      * Otherwise, the URL is assumed to refer to a JAR file which will be
 220      * downloaded and opened as needed.
 221      *
 222      * @param  name class loader name; or {@code null} if not named
 223      * @param  urls the URLs from which to load classes and resources
 224      * @param  parent the parent class loader for delegation
 225      *
 226      * @throws IllegalArgumentException if the given name is empty.
 227      * @throws NullPointerException if {@code urls} or any of its
 228      *         elements is {@code null}.
 229      *
 230      * @throws SecurityException if a security manager exists and its
 231      *         {@link SecurityManager#checkCreateClassLoader()} method doesn't
 232      *         allow creation of a class loader.
 233      *
 234      * @since 9
 235      * @spec JPMS
 236      */
 237     public URLClassLoader(String name,
 238                           URL[] urls,
 239                           ClassLoader parent) {
 240         super(name, parent);
 241         // this is to make the stack depth consistent with 1.1
 242         SecurityManager security = System.getSecurityManager();
 243         if (security != null) {
 244             security.checkCreateClassLoader();
 245         }
 246         this.acc = AccessController.getContext();
 247         this.ucp = new URLClassPath(urls, acc);
 248     }
 249 
 250     /**
 251      * Constructs a new named {@code URLClassLoader} for the specified URLs,
 252      * parent class loader, and URLStreamHandlerFactory.
 253      * The parent argument will be used as the parent class loader for delegation.
 254      * The factory argument will be used as the stream handler factory to
 255      * obtain protocol handlers when creating new jar URLs.
 256      *
 257      * @param  name class loader name; or {@code null} if not named
 258      * @param  urls the URLs from which to load classes and resources
 259      * @param  parent the parent class loader for delegation
 260      * @param  factory the URLStreamHandlerFactory to use when creating URLs
 261      *
 262      * @throws IllegalArgumentException if the given name is empty.
 263      * @throws NullPointerException if {@code urls} or any of its
 264      *         elements is {@code null}.
 265      *
 266      * @throws SecurityException if a security manager exists and its
 267      *         {@code checkCreateClassLoader} method doesn't allow
 268      *         creation of a class loader.
 269      *
 270      * @since 9
 271      * @spec JPMS
 272      */
 273     public URLClassLoader(String name, URL[] urls, ClassLoader parent,
 274                           URLStreamHandlerFactory factory) {
 275         super(name, parent);
 276         // this is to make the stack depth consistent with 1.1
 277         SecurityManager security = System.getSecurityManager();
 278         if (security != null) {
 279             security.checkCreateClassLoader();
 280         }
 281         this.acc = AccessController.getContext();
 282         this.ucp = new URLClassPath(urls, factory, acc);
 283     }
 284 


 793                         sm.checkPermission(fp);
 794                         return null;
 795                     }
 796                 }, acc);
 797             }
 798             perms.add(p);
 799         }
 800         return perms;
 801     }
 802 
 803     /**
 804      * Creates a new instance of URLClassLoader for the specified
 805      * URLs and parent class loader. If a security manager is
 806      * installed, the {@code loadClass} method of the URLClassLoader
 807      * returned by this method will invoke the
 808      * {@code SecurityManager.checkPackageAccess} method before
 809      * loading the class.
 810      *
 811      * @param urls the URLs to search for classes and resources
 812      * @param parent the parent class loader for delegation
 813      * @exception  NullPointerException if {@code urls} or any of its
 814      *             elements is {@code null}.
 815      * @return the resulting class loader
 816      */
 817     public static URLClassLoader newInstance(final URL[] urls,
 818                                              final ClassLoader parent) {
 819         // Save the caller's context
 820         final AccessControlContext acc = AccessController.getContext();
 821         // Need a privileged block to create the class loader
 822         URLClassLoader ucl = AccessController.doPrivileged(
 823             new PrivilegedAction<>() {
 824                 public URLClassLoader run() {
 825                     return new FactoryURLClassLoader(null, urls, parent, acc);
 826                 }
 827             });
 828         return ucl;
 829     }
 830 
 831     /**
 832      * Creates a new instance of URLClassLoader for the specified
 833      * URLs and default parent class loader. If a security manager is
 834      * installed, the {@code loadClass} method of the URLClassLoader
 835      * returned by this method will invoke the
 836      * {@code SecurityManager.checkPackageAccess} before
 837      * loading the class.
 838      *
 839      * @param urls the URLs to search for classes and resources
 840      * @exception  NullPointerException if {@code urls} or any of its
 841      *             elements is {@code null}.
 842      * @return the resulting class loader
 843      */
 844     public static URLClassLoader newInstance(final URL[] urls) {
 845         // Save the caller's context
 846         final AccessControlContext acc = AccessController.getContext();
 847         // Need a privileged block to create the class loader
 848         URLClassLoader ucl = AccessController.doPrivileged(
 849             new PrivilegedAction<>() {
 850                 public URLClassLoader run() {
 851                     return new FactoryURLClassLoader(urls, acc);
 852                 }
 853             });
 854         return ucl;
 855     }
 856 
 857     static {
 858         SharedSecrets.setJavaNetURLClassLoaderAccess(
 859             new JavaNetURLClassLoaderAccess() {
 860                 @Override
 861                 public AccessControlContext getAccessControlContext(URLClassLoader u) {


< prev index next >