< prev index next >

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

Print this page

        

@@ -220,10 +220,13 @@
     // The parent class loader for delegation
     // Note: VM hardcoded the offset of this field, thus all new fields
     // must be added *after* it.
     private final ClassLoader parent;
 
+    // class loader name
+    private final String name;
+
     // the unnamed module for this ClassLoader
     private final Module unnamedModule;
 
     /**
      * Encapsulates the set of parallel capable loader types.

@@ -336,11 +339,12 @@
             security.checkCreateClassLoader();
         }
         return null;
     }
 
-    private ClassLoader(Void unused, ClassLoader parent) {
+    private ClassLoader(Void unused, String name, ClassLoader parent) {
+        this.name = name;
         this.parent = parent;
         this.unnamedModule
             = SharedSecrets.getJavaLangReflectModuleAccess()
                            .defineUnnamedModule(this);
         if (ParallelLoaders.isRegistered(this.getClass())) {

@@ -354,10 +358,29 @@
             assertionLock = this;
         }
     }
 
     /**
+     * Creates a new class loader of the specified name and using the
+     * specified parent class loader for delegation.
+     *
+     * @param  name   class loader name; or {@code null} if not named
+     * @param  parent the parent class loader
+     *
+     * @throws SecurityException
+     *         If a security manager exists and its
+     *         {@link SecurityManager#checkCreateClassLoader()}
+     *         method doesn't allow creation of a new class loader.
+     *
+     * @since  9
+     */
+    protected ClassLoader(String name, ClassLoader parent) {
+        this(checkCreateClassLoader(), name, parent);
+    }
+
+
+    /**
      * Creates a new class loader using the specified parent class loader for
      * delegation.
      *
      * <p> If there is a security manager, its {@link
      * SecurityManager#checkCreateClassLoader()

@@ -373,13 +396,14 @@
      *          of a new class loader.
      *
      * @since  1.2
      */
     protected ClassLoader(ClassLoader parent) {
-        this(checkCreateClassLoader(), parent);
+        this(checkCreateClassLoader(), null, parent);
     }
 
+
     /**
      * Creates a new class loader using the <tt>ClassLoader</tt> returned by
      * the method {@link #getSystemClassLoader()
      * <tt>getSystemClassLoader()</tt>} as the parent class loader.
      *

@@ -392,13 +416,28 @@
      *          If a security manager exists and its
      *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
      *          of a new class loader.
      */
     protected ClassLoader() {
-        this(checkCreateClassLoader(), getSystemClassLoader());
+        this(checkCreateClassLoader(), null, getSystemClassLoader());
     }
 
+
+    /**
+     * Returns the name of this class loader or {@code null} if
+     * this class loader is not named.
+     *
+     * @return name of this class loader; or {@code null} if
+     * this class loader is not named.
+     *
+     * @since 9
+     */
+    public String getName() {
+        return name;
+    }
+
+
     // -- Class --
 
     /**
      * Loads the class with the specified <a href="#name">binary name</a>.
      * This method searches for classes in the same manner as the {@link
< prev index next >