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

Print this page




  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


2189     private ClassLoader parent;
2190 
2191     SystemClassLoaderAction(ClassLoader parent) {
2192         this.parent = parent;
2193     }
2194 
2195     public ClassLoader run() throws Exception {
2196         String cls = System.getProperty("java.system.class.loader");
2197         if (cls == null) {
2198             return parent;
2199         }
2200 
2201         Constructor<?> ctor = Class.forName(cls, true, parent)
2202             .getDeclaredConstructor(new Class<?>[] { ClassLoader.class });
2203         ClassLoader sys = (ClassLoader) ctor.newInstance(
2204             new Object[] { parent });
2205         Thread.currentThread().setContextClassLoader(sys);
2206         return sys;
2207     }
2208 }



































  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.NoSuchElementException;
  49 import java.util.Vector;
  50 import java.util.Hashtable;
  51 import java.util.WeakHashMap;
  52 import java.util.concurrent.ConcurrentHashMap;

  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


2189     private ClassLoader parent;
2190 
2191     SystemClassLoaderAction(ClassLoader parent) {
2192         this.parent = parent;
2193     }
2194 
2195     public ClassLoader run() throws Exception {
2196         String cls = System.getProperty("java.system.class.loader");
2197         if (cls == null) {
2198             return parent;
2199         }
2200 
2201         Constructor<?> ctor = Class.forName(cls, true, parent)
2202             .getDeclaredConstructor(new Class<?>[] { ClassLoader.class });
2203         ClassLoader sys = (ClassLoader) ctor.newInstance(
2204             new Object[] { parent });
2205         Thread.currentThread().setContextClassLoader(sys);
2206         return sys;
2207     }
2208 }
2209 
2210 /*
2211  * A utility class that will enumerate over an array of enumerations.
2212  */
2213 final class CompoundEnumeration<E> implements Enumeration<E> {
2214     private final Enumeration<E>[] enums;
2215     private int index;
2216 
2217     public CompoundEnumeration(Enumeration<E>[] enums) {
2218         this.enums = enums;
2219     }
2220 
2221     private boolean next() {
2222         while (index < enums.length) {
2223             if (enums[index] != null && enums[index].hasMoreElements()) {
2224                 return true;
2225             }
2226             index++;
2227         }
2228         return false;
2229     }
2230 
2231     public boolean hasMoreElements() {
2232         return next();
2233     }
2234 
2235     public E nextElement() {
2236         if (!next()) {
2237             throw new NoSuchElementException();
2238         }
2239         return enums[index].nextElement();
2240     }
2241 }