< prev index next >

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

Print this page




  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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;

  30 import java.io.File;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.Module;
  34 import java.net.URL;
  35 import java.security.AccessController;
  36 import java.security.AccessControlContext;
  37 import java.security.CodeSource;
  38 import java.security.PrivilegedAction;
  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.Hashtable;
  46 import java.util.Map;
  47 import java.util.Objects;
  48 import java.util.Set;


  49 import java.util.Stack;
  50 import java.util.NoSuchElementException;
  51 import java.util.Vector;
  52 import java.util.WeakHashMap;
  53 import java.util.concurrent.ConcurrentHashMap;

  54 import java.util.stream.Stream;

  55 
  56 import jdk.internal.perf.PerfCounter;
  57 import jdk.internal.module.ServicesCatalog;
  58 import jdk.internal.loader.BootLoader;
  59 import jdk.internal.loader.ClassLoaders;
  60 import jdk.internal.misc.SharedSecrets;
  61 import jdk.internal.misc.Unsafe;
  62 import jdk.internal.misc.VM;
  63 import jdk.internal.reflect.CallerSensitive;
  64 import jdk.internal.reflect.Reflection;
  65 import sun.reflect.misc.ReflectUtil;
  66 import sun.security.util.SecurityConstants;
  67 
  68 /**
  69  * A class loader is an object that is responsible for loading classes. The
  70  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  71  * href="#name">binary name</a> of a class, a class loader should attempt to
  72  * locate or generate data that constitutes a definition for the class.  A
  73  * typical strategy is to transform the name into a file name and then read a
  74  * "class file" of that name from a file system.


1335      *
1336      * @throws  IOException
1337      *          If I/O errors occur
1338      *
1339      * @see  #findResources(String)
1340      *
1341      * @since  1.2
1342      */
1343     public Enumeration<URL> getResources(String name) throws IOException {
1344         @SuppressWarnings("unchecked")
1345         Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
1346         if (parent != null) {
1347             tmp[0] = parent.getResources(name);
1348         } else {
1349             tmp[0] = BootLoader.findResources(name);
1350         }
1351         tmp[1] = findResources(name);
1352 
1353         return new CompoundEnumeration<>(tmp);
1354     }



















































1355 
1356     /**
1357      * Finds the resource with the given name. Class loader implementations
1358      * should override this method to specify where to find resources.
1359      *
1360      * Resources in a named module are private to that module. This method does
1361      * not find resources in named modules defined to this class loader.
1362      *
1363      * @param  name
1364      *         The resource name
1365      *
1366      * @return  A <tt>URL</tt> object for reading the resource, or
1367      *          <tt>null</tt> if the resource could not be found
1368      *
1369      * @since  1.2
1370      */
1371     protected URL findResource(String name) {
1372         return null;
1373     }
1374 




  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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.UncheckedIOException;
  31 import java.io.File;
  32 import java.lang.reflect.Constructor;
  33 import java.lang.reflect.Field;
  34 import java.lang.reflect.Module;
  35 import java.net.URL;
  36 import java.security.AccessController;
  37 import java.security.AccessControlContext;
  38 import java.security.CodeSource;
  39 import java.security.PrivilegedAction;
  40 import java.security.ProtectionDomain;
  41 import java.security.cert.Certificate;
  42 import java.util.Collections;
  43 import java.util.Enumeration;
  44 import java.util.HashMap;
  45 import java.util.HashSet;
  46 import java.util.Hashtable;
  47 import java.util.Map;
  48 import java.util.Objects;
  49 import java.util.Set;
  50 import java.util.Spliterator;
  51 import java.util.Spliterators;
  52 import java.util.Stack;
  53 import java.util.NoSuchElementException;
  54 import java.util.Vector;
  55 import java.util.WeakHashMap;
  56 import java.util.concurrent.ConcurrentHashMap;
  57 import java.util.function.Supplier;
  58 import java.util.stream.Stream;
  59 import java.util.stream.StreamSupport;
  60 
  61 import jdk.internal.perf.PerfCounter;
  62 import jdk.internal.module.ServicesCatalog;
  63 import jdk.internal.loader.BootLoader;
  64 import jdk.internal.loader.ClassLoaders;
  65 import jdk.internal.misc.SharedSecrets;
  66 import jdk.internal.misc.Unsafe;
  67 import jdk.internal.misc.VM;
  68 import jdk.internal.reflect.CallerSensitive;
  69 import jdk.internal.reflect.Reflection;
  70 import sun.reflect.misc.ReflectUtil;
  71 import sun.security.util.SecurityConstants;
  72 
  73 /**
  74  * A class loader is an object that is responsible for loading classes. The
  75  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  76  * href="#name">binary name</a> of a class, a class loader should attempt to
  77  * locate or generate data that constitutes a definition for the class.  A
  78  * typical strategy is to transform the name into a file name and then read a
  79  * "class file" of that name from a file system.


1340      *
1341      * @throws  IOException
1342      *          If I/O errors occur
1343      *
1344      * @see  #findResources(String)
1345      *
1346      * @since  1.2
1347      */
1348     public Enumeration<URL> getResources(String name) throws IOException {
1349         @SuppressWarnings("unchecked")
1350         Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
1351         if (parent != null) {
1352             tmp[0] = parent.getResources(name);
1353         } else {
1354             tmp[0] = BootLoader.findResources(name);
1355         }
1356         tmp[1] = findResources(name);
1357 
1358         return new CompoundEnumeration<>(tmp);
1359     }
1360 
1361     /**
1362      * Returns a stream that loads the resources with the given name. A 
1363      * resource is some data (images, audio, text, etc) that can be accessed by
1364      * class code in a way that is independent of the location of the code.
1365      *
1366      * Resources in a named module are private to that module. This method does
1367      * not find resources in named modules.
1368      *
1369      * <p> The name of a resource is a {@code /}-separated path name that
1370      * identifies the resource.
1371      *
1372      * <p> The search order is described in the documentation for {@link
1373      * #getResource(String)}.
1374      *
1375      * <p> The loading of resources will occur when the returned stream is 
1376      * evaluated. If the loading of resources results in an {@code IOException} 
1377      * then the I/O exception is wrapped in an {@link UncheckedIOException}
1378      * that is then thrown.
1379      *
1380      * @apiNote When overriding this method it is recommended that an
1381      * implementation ensures that any delegation is consistent with the {@link
1382      * #getResource(java.lang.String) getResource(String)} method. This should
1383      * ensure that the first element returned by the stream is the same 
1384      * resource that the {@code getResource(String)} method would return.
1385      *
1386      * @param  name
1387      *         The resource name
1388      *
1389      * @return  A stream of resource {@link java.net.URL URL} objects. If no
1390      *          resources could  be found, the stream will be empty.  Resources
1391      *          that the class loader doesn't have access to will not be in the
1392      *          stream.
1393      *
1394      * @see  #findResources(String)
1395      *
1396      * @since  9
1397      */
1398     public Stream<URL> resources(String name) {
1399         Supplier<Spliterator<URL>> si = () -> {
1400             try {
1401                 return Spliterators.spliteratorUnknownSize(
1402                     getResources(name).asIterator(), RESOURCE_CHARACTERISTICS);
1403             } catch (IOException e) {
1404                 throw new UncheckedIOException(e);
1405             }
1406         };
1407         return StreamSupport.stream(si, RESOURCE_CHARACTERISTICS, false);
1408     }
1409 
1410     private static final int RESOURCE_CHARACTERISTICS = Spliterator.NONNULL | Spliterator.IMMUTABLE;
1411 
1412     /**
1413      * Finds the resource with the given name. Class loader implementations
1414      * should override this method to specify where to find resources.
1415      *
1416      * Resources in a named module are private to that module. This method does
1417      * not find resources in named modules defined to this class loader.
1418      *
1419      * @param  name
1420      *         The resource name
1421      *
1422      * @return  A <tt>URL</tt> object for reading the resource, or
1423      *          <tt>null</tt> if the resource could not be found
1424      *
1425      * @since  1.2
1426      */
1427     protected URL findResource(String name) {
1428         return null;
1429     }
1430 


< prev index next >