< prev index next >

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

Print this page
rev 15908 : 8165271: Fix use of reflection to gain access to private fields
Reviewed-by:
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


  34 import java.security.AccessController;
  35 import java.security.CodeSigner;
  36 import java.security.CodeSource;
  37 import java.security.Permission;
  38 import java.security.PermissionCollection;
  39 import java.security.PrivilegedAction;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.SecureClassLoader;
  42 import java.util.Enumeration;
  43 import java.util.List;
  44 import java.util.NoSuchElementException;
  45 import java.util.Set;
  46 import java.util.WeakHashMap;
  47 import java.util.jar.Attributes;
  48 import java.util.jar.Attributes.Name;
  49 import java.util.jar.JarFile;
  50 import java.util.jar.Manifest;
  51 
  52 import jdk.internal.loader.Resource;
  53 import jdk.internal.loader.URLClassPath;


  54 import jdk.internal.perf.PerfCounter;
  55 import sun.net.www.ParseUtil;
  56 import sun.security.util.SecurityConstants;
  57 
  58 /**
  59  * This class loader is used to load classes and resources from a search
  60  * path of URLs referring to both JAR files and directories. Any {@code jar:}
  61  * scheme URL (see {@link java.net.JarURLConnection}) is assumed to refer to a
  62  * JAR file.  Any {@code file:} scheme URL that ends with a '/' is assumed to
  63  * refer to a directory. Otherwise, the URL is assumed to refer to a JAR file
  64  * which will be opened as needed.
  65  * <p>
  66  * The AccessControlContext of the thread that created the instance of
  67  * URLClassLoader will be used when subsequently loading classes and
  68  * resources.
  69  * <p>
  70  * The classes that are loaded are by default granted permission only to
  71  * access the URLs specified when the URLClassLoader was created.
  72  *
  73  * @author  David Connelly


 748      * loading the class.
 749      *
 750      * @param urls the URLs to search for classes and resources
 751      * @exception  NullPointerException if {@code urls} is {@code null}.
 752      * @return the resulting class loader
 753      */
 754     public static URLClassLoader newInstance(final URL[] urls) {
 755         // Save the caller's context
 756         final AccessControlContext acc = AccessController.getContext();
 757         // Need a privileged block to create the class loader
 758         URLClassLoader ucl = AccessController.doPrivileged(
 759             new PrivilegedAction<>() {
 760                 public URLClassLoader run() {
 761                     return new FactoryURLClassLoader(urls, acc);
 762                 }
 763             });
 764         return ucl;
 765     }
 766 
 767     static {








 768         ClassLoader.registerAsParallelCapable();
 769     }
 770 }
 771 
 772 final class FactoryURLClassLoader extends URLClassLoader {
 773 
 774     static {
 775         ClassLoader.registerAsParallelCapable();
 776     }
 777 
 778     FactoryURLClassLoader(URL[] urls, ClassLoader parent,
 779                           AccessControlContext acc) {
 780         super(urls, parent, acc);
 781     }
 782 
 783     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
 784         super(urls, acc);
 785     }
 786 
 787     public final Class<?> loadClass(String name, boolean resolve)
   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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


  34 import java.security.AccessController;
  35 import java.security.CodeSigner;
  36 import java.security.CodeSource;
  37 import java.security.Permission;
  38 import java.security.PermissionCollection;
  39 import java.security.PrivilegedAction;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.SecureClassLoader;
  42 import java.util.Enumeration;
  43 import java.util.List;
  44 import java.util.NoSuchElementException;
  45 import java.util.Set;
  46 import java.util.WeakHashMap;
  47 import java.util.jar.Attributes;
  48 import java.util.jar.Attributes.Name;
  49 import java.util.jar.JarFile;
  50 import java.util.jar.Manifest;
  51 
  52 import jdk.internal.loader.Resource;
  53 import jdk.internal.loader.URLClassPath;
  54 import jdk.internal.misc.JavaNetUrlClassLoaderAccess;
  55 import jdk.internal.misc.SharedSecrets;
  56 import jdk.internal.perf.PerfCounter;
  57 import sun.net.www.ParseUtil;
  58 import sun.security.util.SecurityConstants;
  59 
  60 /**
  61  * This class loader is used to load classes and resources from a search
  62  * path of URLs referring to both JAR files and directories. Any {@code jar:}
  63  * scheme URL (see {@link java.net.JarURLConnection}) is assumed to refer to a
  64  * JAR file.  Any {@code file:} scheme URL that ends with a '/' is assumed to
  65  * refer to a directory. Otherwise, the URL is assumed to refer to a JAR file
  66  * which will be opened as needed.
  67  * <p>
  68  * The AccessControlContext of the thread that created the instance of
  69  * URLClassLoader will be used when subsequently loading classes and
  70  * resources.
  71  * <p>
  72  * The classes that are loaded are by default granted permission only to
  73  * access the URLs specified when the URLClassLoader was created.
  74  *
  75  * @author  David Connelly


 750      * loading the class.
 751      *
 752      * @param urls the URLs to search for classes and resources
 753      * @exception  NullPointerException if {@code urls} is {@code null}.
 754      * @return the resulting class loader
 755      */
 756     public static URLClassLoader newInstance(final URL[] urls) {
 757         // Save the caller's context
 758         final AccessControlContext acc = AccessController.getContext();
 759         // Need a privileged block to create the class loader
 760         URLClassLoader ucl = AccessController.doPrivileged(
 761             new PrivilegedAction<>() {
 762                 public URLClassLoader run() {
 763                     return new FactoryURLClassLoader(urls, acc);
 764                 }
 765             });
 766         return ucl;
 767     }
 768 
 769     static {
 770         SharedSecrets.setJavaNetUrlClassLoaderAccess(
 771             new JavaNetUrlClassLoaderAccess() {
 772                 @Override
 773                 public AccessControlContext getAccessControlContext(URLClassLoader u) {
 774                     return u.acc;
 775                 }
 776             }
 777         );
 778         ClassLoader.registerAsParallelCapable();
 779     }
 780 }
 781 
 782 final class FactoryURLClassLoader extends URLClassLoader {
 783 
 784     static {
 785         ClassLoader.registerAsParallelCapable();
 786     }
 787 
 788     FactoryURLClassLoader(URL[] urls, ClassLoader parent,
 789                           AccessControlContext acc) {
 790         super(urls, parent, acc);
 791     }
 792 
 793     FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {
 794         super(urls, acc);
 795     }
 796 
 797     public final Class<?> loadClass(String name, boolean resolve)
< prev index next >