jdk/src/java.base/share/classes/java/lang/ClassLoader.java
author simonis
Fri, 14 Aug 2015 10:35:45 +0200
changeset 32209 24bb680a1609
parent 31084 0acb07ceb0bc
child 32649 2ee9017c7597
permissions -rw-r--r--
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX Reviewed-by: rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
     2
 * Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4352
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4352
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4352
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4352
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4352
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.ProtectionDomain;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    40
import java.security.cert.Certificate;
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    41
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Vector;
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    49
import java.util.Hashtable;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    50
import java.util.WeakHashMap;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    51
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.misc.CompoundEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.misc.Resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.misc.URLClassPath;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
    55
import sun.reflect.CallerSensitive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.reflect.Reflection;
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
    57
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * A class loader is an object that is responsible for loading classes. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * href="#name">binary name</a> of a class, a class loader should attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * locate or generate data that constitutes a definition for the class.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * typical strategy is to transform the name into a file name and then read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * "class file" of that name from a file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> <tt>Class</tt> objects for array classes are not created by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * loaders, but are created automatically as required by the Java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The class loader for an array class, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Class#getClassLoader()} is the same as the class loader for its element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * type; if the element type is a primitive type, then the array class has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p> Applications implement subclasses of <tt>ClassLoader</tt> in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * extend the manner in which the Java virtual machine dynamically loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> Class loaders may typically be used by security managers to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * security domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p> The <tt>ClassLoader</tt> class uses a delegation model to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * classes and resources.  Each instance of <tt>ClassLoader</tt> has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * associated parent class loader.  When requested to find a class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * resource, a <tt>ClassLoader</tt> instance will delegate the search for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * class or resource to its parent class loader before attempting to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * class or resource itself.  The virtual machine's built-in class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * called the "bootstrap class loader", does not itself have a parent but may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * serve as the parent of a <tt>ClassLoader</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    95
 * <p> Class loaders that support concurrent loading of classes are known as
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    96
 * <em>parallel capable</em> class loaders and are required to register
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    97
 * themselves at their class initialization time by invoking the
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    98
 * {@link
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    99
 * #registerAsParallelCapable <tt>ClassLoader.registerAsParallelCapable</tt>}
6890
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   100
 * method. Note that the <tt>ClassLoader</tt> class is registered as parallel
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   101
 * capable by default. However, its subclasses still need to register themselves
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   102
 * if they are parallel capable. <br>
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   103
 * In environments in which the delegation model is not strictly
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   104
 * hierarchical, class loaders need to be parallel capable, otherwise class
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   105
 * loading can lead to deadlocks because the loader lock is held for the
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   106
 * duration of the class loading process (see {@link #loadClass
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   107
 * <tt>loadClass</tt>} methods).
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   108
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p> Normally, the Java virtual machine loads classes from the local file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * system in a platform-dependent manner.  For example, on UNIX systems, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * virtual machine loads classes from the directory defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <tt>CLASSPATH</tt> environment variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p> However, some classes may not originate from a file; they may originate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * from other sources, such as the network, or they could be constructed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * application.  The method {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <tt>defineClass</tt>} converts an array of bytes into an instance of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <tt>Class</tt>. Instances of this newly defined class can be created using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * {@link Class#newInstance <tt>Class.newInstance</tt>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <p> The methods and constructors of objects created by a class loader may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * reference other classes.  To determine the class(es) referred to, the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * the class loader that originally created the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p> For example, an application could create a network class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * download class files from a server.  Sample code might look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *   ClassLoader loader&nbsp;= new NetworkClassLoader(host,&nbsp;port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *   Object main&nbsp;= loader.loadClass("Main", true).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *       &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <p> The network class loader subclass must define the methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * #findClass <tt>findClass</tt>} and <tt>loadClassData</tt> to load a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * from the network.  Once it has downloaded the bytes that make up the class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * create a class instance.  A sample implementation is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *     class NetworkClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *         String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *         int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *             byte[] b = loadClassData(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *             return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *         private byte[] loadClassData(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *             // load the class data from the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *             &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   158
 * <h3> <a name="name">Binary names</a> </h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p> Any class name provided as a {@link String} parameter to methods in
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   161
 * <tt>ClassLoader</tt> must be a binary name as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   162
 * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <p> Examples of valid class names include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *   "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *   "javax.swing.JSpinner$DefaultEditor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *   "java.security.KeyStore$Builder$FileBuilder$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *   "java.net.URLClassLoader$3$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
22571
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   172
 * {@code Class} objects for array classes are not created by {@code ClassLoader};
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   173
 * use the {@link Class#forName} method instead.
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   174
 *
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   175
 * @jls 13.1 The Form of a Binary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * @see      #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
public abstract class ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // The parent class loader for delegation
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   187
    // Note: VM hardcoded the offset of this field, thus all new fields
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   188
    // must be added *after* it.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   189
    private final ClassLoader parent;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   190
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   191
    /**
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   192
     * Encapsulates the set of parallel capable loader types.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   193
     */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   194
    private static class ParallelLoaders {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   195
        private ParallelLoaders() {}
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   196
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   197
        // the set of parallel capable loader types
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   198
        private static final Set<Class<? extends ClassLoader>> loaderTypes =
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   199
            Collections.newSetFromMap(new WeakHashMap<>());
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   200
        static {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   201
            synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   202
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   203
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   204
        /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25167
diff changeset
   205
         * Registers the given class loader type as parallel capable.
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   206
         * Returns {@code true} is successfully registered; {@code false} if
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   207
         * loader's super class is not registered.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   208
         */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   209
        static boolean register(Class<? extends ClassLoader> c) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   210
            synchronized (loaderTypes) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   211
                if (loaderTypes.contains(c.getSuperclass())) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   212
                    // register the class loader as parallel capable
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   213
                    // if and only if all of its super classes are.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   214
                    // Note: given current classloading sequence, if
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   215
                    // the immediate super class is parallel capable,
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   216
                    // all the super classes higher up must be too.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   217
                    loaderTypes.add(c);
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   218
                    return true;
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   219
                } else {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   220
                    return false;
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   221
                }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   222
            }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   223
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   224
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   225
        /**
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   226
         * Returns {@code true} if the given class loader type is
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   227
         * registered as parallel capable.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   228
         */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   229
        static boolean isRegistered(Class<? extends ClassLoader> c) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   230
            synchronized (loaderTypes) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   231
                return loaderTypes.contains(c);
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   232
            }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   233
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   234
    }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   235
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   236
    // Maps class name to the corresponding lock object when the current
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   237
    // class loader is parallel capable.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   238
    // Note: VM also uses this field to decide if the current class loader
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   239
    // is parallel capable and the appropriate lock object for class loading.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   240
    private final ConcurrentHashMap<String, Object> parallelLockMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // Hashtable that maps packages to certs
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   243
    private final Map <String, Certificate[]> package2certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // Shared among all packages with unsigned classes
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   246
    private static final Certificate[] nocerts = new Certificate[0];
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   247
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   248
    // The classes loaded by this class loader. The only purpose of this table
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   249
    // is to keep the classes from being GC'ed until the loader is GC'ed.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   250
    private final Vector<Class<?>> classes = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   252
    // The "default" domain. Set as the default ProtectionDomain on newly
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   253
    // created classes.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   254
    private final ProtectionDomain defaultDomain =
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   255
        new ProtectionDomain(new CodeSource(null, (Certificate[]) null),
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   256
                             null, this, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // The initiating protection domains for all classes loaded by this loader
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   259
    private final Set<ProtectionDomain> domains;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // Invoked by the VM to record every loaded class with this loader.
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   262
    void addClass(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        classes.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // The packages defined in this class loader.  Each package name is mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // to its corresponding Package object.
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
   268
    private final ConcurrentHashMap<String, Package> packages
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
   269
            = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   271
    private static Void checkCreateClassLoader() {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   272
        SecurityManager security = System.getSecurityManager();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   273
        if (security != null) {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   274
            security.checkCreateClassLoader();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   275
        }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   276
        return null;
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   277
    }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   278
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   279
    private ClassLoader(Void unused, ClassLoader parent) {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   280
        this.parent = parent;
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   281
        if (ParallelLoaders.isRegistered(this.getClass())) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   282
            parallelLockMap = new ConcurrentHashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   283
            package2certs = new ConcurrentHashMap<>();
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   284
            domains = Collections.synchronizedSet(new HashSet<>());
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   285
            assertionLock = new Object();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   286
        } else {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   287
            // no finer-grained lock; lock on the classloader instance
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   288
            parallelLockMap = null;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   289
            package2certs = new Hashtable<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   290
            domains = new HashSet<>();
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   291
            assertionLock = this;
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   292
        }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   293
    }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Creates a new class loader using the specified parent class loader for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param  parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *         The parent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    protected ClassLoader(ClassLoader parent) {
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   315
        this(checkCreateClassLoader(), parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Creates a new class loader using the <tt>ClassLoader</tt> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the method {@link #getSystemClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <tt>getSystemClassLoader()</tt>} as the parent class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    protected ClassLoader() {
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   334
        this(checkCreateClassLoader(), getSystemClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // -- Class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Loads the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * This method searches for classes in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * machine to resolve class references.  Invoking this method is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   345
     * false)</tt>}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *          If the class was not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public Class<?> loadClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Loads the class with the specified <a href="#name">binary name</a>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * default implementation of this method searches for classes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 20854
diff changeset
   364
     * <ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *   has already been loaded.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *   loader built-in to the virtual machine is used, instead.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *   class.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <p> If the class was found using the above steps, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * #findClass(String)}, rather than this method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   385
     * <p> Unless overridden, this method synchronizes on the result of
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   386
     * {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   387
     * during the entire class loading process.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   388
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param  resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *         If <tt>true</tt> then resolve the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   400
    protected Class<?> loadClass(String name, boolean resolve)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   403
        synchronized (getClassLoadingLock(name)) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   404
            // First, check if the class has already been loaded
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   405
            Class<?> c = findLoadedClass(name);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   406
            if (c == null) {
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   407
                long t0 = System.nanoTime();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   408
                try {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   409
                    if (parent != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   410
                        c = parent.loadClass(name, false);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   411
                    } else {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   412
                        c = findBootstrapClassOrNull(name);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   413
                    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   414
                } catch (ClassNotFoundException e) {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   415
                    // ClassNotFoundException thrown if class not found
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   416
                    // from the non-null parent class loader
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   417
                }
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   418
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   419
                if (c == null) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   420
                    // If still not found, then invoke findClass in order
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   421
                    // to find the class.
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   422
                    long t1 = System.nanoTime();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   423
                    c = findClass(name);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   424
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   425
                    // this is the defining class loader; record the stats
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   426
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   427
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   428
                    sun.misc.PerfCounter.getFindClasses().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   430
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   431
            if (resolve) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   432
                resolveClass(c);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   433
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   434
            return c;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   435
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   436
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   437
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   438
    /**
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   439
     * Returns the lock object for class loading operations.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   440
     * For backward compatibility, the default implementation of this method
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   441
     * behaves as follows. If this ClassLoader object is registered as
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   442
     * parallel capable, the method returns a dedicated object associated
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   443
     * with the specified class name. Otherwise, the method returns this
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   444
     * ClassLoader object.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   445
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   446
     * @param  className
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   447
     *         The name of the to-be-loaded class
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   448
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   449
     * @return the lock for class loading operations
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   450
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   451
     * @throws NullPointerException
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   452
     *         If registered as parallel capable and <tt>className</tt> is null
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   453
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   454
     * @see #loadClass(String, boolean)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   455
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   456
     * @since  1.7
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   457
     */
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   458
    protected Object getClassLoadingLock(String className) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   459
        Object lock = this;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   460
        if (parallelLockMap != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   461
            Object newLock = new Object();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   462
            lock = parallelLockMap.putIfAbsent(className, newLock);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   463
            if (lock == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   464
                lock = newLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   467
        return lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // This method is invoked by the virtual machine to load a class.
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   471
    private Class<?> loadClassInternal(String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   474
        // For backward compatibility, explicitly lock on 'this' when
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   475
        // the current class loader is not parallel capable.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   476
        if (parallelLockMap == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   477
            synchronized (this) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   478
                 return loadClass(name);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   479
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   480
        } else {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   481
            return loadClass(name);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   482
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   485
    // Invoked by the VM after loading class with this loader.
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   486
    private void checkPackageAccess(Class<?> cls, ProtectionDomain pd) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (sm != null) {
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   489
            if (ReflectUtil.isNonPublicProxyClass(cls)) {
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 20854
diff changeset
   490
                for (Class<?> intf: cls.getInterfaces()) {
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   491
                    checkPackageAccess(intf, pd);
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   492
                }
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   493
                return;
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   494
            }
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   495
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            final String name = cls.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            final int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (i != -1) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28544
diff changeset
   499
                AccessController.doPrivileged(new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   500
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }, new AccessControlContext(new ProtectionDomain[] {pd}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        domains.add(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Finds the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * This method should be overridden by class loader implementations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * follow the delegation model for loading classes, and will be invoked by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * the {@link #loadClass <tt>loadClass</tt>} method after checking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * parent class loader for the requested class.  The default implementation
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   516
     * throws a <tt>ClassNotFoundException</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    protected Class<?> findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Before the <tt>Class</tt> can be used it must be resolved.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * is deprecated in favor of the version that takes a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * href="#name">binary name</a> as its first argument, and is more secure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   541
     *         of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   542
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *          class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
7029
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   560
     * @throws  SecurityException
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   561
     *          If an attempt is made to add this class to a package that
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   562
     *          contains classes that were signed by a different set of
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   563
     *          certificates than this class, or if an attempt is made
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   564
     *          to define a class in a package with a fully-qualified name
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   565
     *          that starts with "{@code java.}".
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   566
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @deprecated  Replaced by {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * defineClass(String, byte[], int, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    protected final Class<?> defineClass(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return defineClass(null, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Before the <tt>Class</tt> can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p> This method assigns a default {@link java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <tt>ProtectionDomain</tt>} to the newly defined class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <tt>ProtectionDomain</tt> is effectively granted the same set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * permissions returned when {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * java.security.Policy#getPermissions(java.security.CodeSource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * is invoked.  The default domain is created on the first invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * {@link #defineClass(String, byte[], int, int) <tt>defineClass</tt>},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * and re-used on subsequent invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p> To assign a specific <tt>ProtectionDomain</tt> to the class, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * the {@link #defineClass(String, byte[], int, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * java.security.ProtectionDomain) <tt>defineClass</tt>} method that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <tt>ProtectionDomain</tt> as one of its arguments.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   606
     *         of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   607
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *          class data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          certificates than this class (which is unsigned), or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          <tt>name</tt> begins with "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @see  java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @see  java.security.SecureClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    protected final Class<?> defineClass(String name, byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return defineClass(name, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /* Determine protection domain, and check that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        - not define java.* class,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   646
        - signer of this class matches signers for the rest of the classes in
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   647
          package.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    private ProtectionDomain preDefineClass(String name,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   650
                                            ProtectionDomain pd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new NoClassDefFoundError("IllegalName: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if ((name != null) && name.startsWith("java.")) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   656
            throw new SecurityException
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   657
                ("Prohibited package name: " +
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   658
                 name.substring(0, name.lastIndexOf('.')));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   660
        if (pd == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   661
            pd = defaultDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   664
        if (name != null) checkCerts(name, pd.getCodeSource());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   666
        return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   669
    private String defineClassSourceLocation(ProtectionDomain pd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   671
        CodeSource cs = pd.getCodeSource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        String source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (cs != null && cs.getLocation() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            source = cs.getLocation().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   679
    private void postDefineClass(Class<?> c, ProtectionDomain pd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   681
        if (pd.getCodeSource() != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   682
            Certificate certs[] = pd.getCodeSource().getCertificates();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (certs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                setSigners(c, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Converts an array of bytes into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * <p> The first class defined in a package determines the exact set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * certificates that all subsequent classes defined in that package must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * contain.  The set of certificates for a class is obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * {@link java.security.CodeSource <tt>CodeSource</tt>} within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <tt>ProtectionDomain</tt> of the class.  Any classes added to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * package must contain the same set of certificates or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <tt>SecurityException</tt> will be thrown.  Note that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <tt>name</tt> is <tt>null</tt>, this check is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * You should always pass in the <a href="#name">binary name</a> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * class you are defining as well as the bytes.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * class you are defining is indeed the class you think it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <p> The specified <tt>name</tt> cannot begin with "<tt>java.</tt>", since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * all classes in the "<tt>java.*</tt> packages can only be defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * bootstrap class loader.  If <tt>name</tt> is not <tt>null</tt>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * must be equal to the <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * specified by the byte array "<tt>b</tt>", otherwise a {@link
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
   712
     * NoClassDefFoundError <tt>NoClassDefFoundError</tt>} will be thrown. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *         The bytes that make up the class data. The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   721
     *         of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   722
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *         The ProtectionDomain of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    protected final Class<?> defineClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        String source = defineClassSourceLocation(protectionDomain);
16063
18e7ee7b4301 8009645: ClassFileTransformer hooks in ClassLoader no longer required
alanb
parents: 13589
diff changeset
   759
        Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   772
     * <p>The rules about the first class defined in a package determining the
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   773
     * set of certificates for the package, and the restrictions on class names
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   774
     * are identical to those specified in the documentation for {@link
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   775
     * #defineClass(String, byte[], int, int, ProtectionDomain)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * result as the statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   782
     *<p> <tt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * ...<br>
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   784
     * byte[] temp = new byte[bBuffer.{@link
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   785
     * java.nio.ByteBuffer#remaining remaining}()];<br>
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   786
     *     bBuffer.{@link java.nio.ByteBuffer#get(byte[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * get}(temp);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   789
     * cl.defineClass}(name, temp, 0,
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   790
     * temp.length, pd);<br>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   791
     * </tt></p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @param  name
6519
1a7d0030a638 6977548: Broken link in ClassLoader.defineClass javadoc
mchung
parents: 5506
diff changeset
   794
     *         The expected <a href="#name">binary name</a>. of the class, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *         The bytes that make up the class data. The bytes from positions
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   799
     *         <tt>b.position()</tt> through <tt>b.position() + b.limit() -1
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   800
     *         </tt> should have the format of a valid class file as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   801
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *         The ProtectionDomain of the class, or <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *          If the data did not contain a valid class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        int len = b.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25167
diff changeset
   832
        // Use byte[] if not a direct ByteBuffer:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (!b.isDirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            if (b.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                return defineClass(name, b.array(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                   b.position() + b.arrayOffset(), len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                   protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                // no array, or read-only array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                return defineClass(name, tb, 0, len, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        String source = defineClassSourceLocation(protectionDomain);
16063
18e7ee7b4301 8009645: ClassFileTransformer hooks in ClassLoader no longer required
alanb
parents: 13589
diff changeset
   848
        Class<?> c = defineClass2(name, b, b.position(), len, protectionDomain, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   853
    private native Class<?> defineClass1(String name, byte[] b, int off, int len,
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   854
                                         ProtectionDomain pd, String source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   856
    private native Class<?> defineClass2(String name, java.nio.ByteBuffer b,
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   857
                                         int off, int len, ProtectionDomain pd,
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   858
                                         String source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    // true if the name is null or has the potential to be a valid binary name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    private boolean checkName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if ((name == null) || (name.length() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return true;
22571
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   864
        if ((name.indexOf('/') != -1) || (name.charAt(0) == '['))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   869
    private void checkCerts(String name, CodeSource cs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        String pname = (i == -1) ? "" : name.substring(0, i);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   872
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   873
        Certificate[] certs = null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   874
        if (cs != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   875
            certs = cs.getCertificates();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   876
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   877
        Certificate[] pcerts = null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   878
        if (parallelLockMap == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   879
            synchronized (this) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   880
                pcerts = package2certs.get(pname);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   881
                if (pcerts == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   882
                    package2certs.put(pname, (certs == null? nocerts:certs));
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   883
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        } else {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   886
            pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs).
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   887
                putIfAbsent(pname, (certs == null? nocerts:certs));
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   888
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   889
        if (pcerts != null && !compareCerts(pcerts, certs)) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   890
            throw new SecurityException("class \""+ name +
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   891
                 "\"'s signer information does not match signer information of other classes in the same package");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * check to make sure the certs for the new class (certs) are the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * the certs for the first class inserted in the package (pcerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   899
    private boolean compareCerts(Certificate[] pcerts,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   900
                                 Certificate[] certs)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        // certs can be null, indicating no certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if ((certs == null) || (certs.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            return pcerts.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        // the length must be the same at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (certs.length != pcerts.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // go through and make sure all the certs in one array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // are in the other and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        boolean match;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   914
        for (Certificate cert : certs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            match = false;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   916
            for (Certificate pcert : pcerts) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   917
                if (cert.equals(pcert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        // now do the same for pcerts
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   926
        for (Certificate pcert : pcerts) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            match = false;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   928
            for (Certificate cert : certs) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
   929
                if (pcert.equals(cert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Links the specified class.  This (misleadingly named) method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * used by a class loader to link a class.  If the class <tt>c</tt> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * already been linked, then this method simply returns. Otherwise, the
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   944
     * class is linked as described in the "Execution" chapter of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
   945
     * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *         The class to link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *          If <tt>c</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @see  #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    protected final void resolveClass(Class<?> c) {
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
   956
        if (c == null) {
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
   957
            throw new NullPointerException();
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
   958
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Finds a class with the specified <a href="#name">binary name</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * loading it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <p> This method loads the class through the system class loader (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * {@link #getSystemClassLoader()}).  The <tt>Class</tt> object returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * might have more than one <tt>ClassLoader</tt> associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Subclasses of <tt>ClassLoader</tt> need not usually invoke this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * because most class loaders need to override just {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * #findClass(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @return  The <tt>Class</tt> object for the specified <tt>name</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @see  #ClassLoader(ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @see  #getParent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    protected final Class<?> findSystemClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                throw new ClassNotFoundException(name);
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
   990
            Class<?> cls = findBootstrapClass(name);
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   991
            if (cls == null) {
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   992
                throw new ClassNotFoundException(name);
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   993
            }
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   994
            return cls;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return system.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   999
    /**
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1000
     * Returns a class loaded by the bootstrap class loader;
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1001
     * or return null if not found.
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1002
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1003
    private Class<?> findBootstrapClassOrNull(String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1005
        if (!checkName(name)) return null;
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1006
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1010
    // return null if not found
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1011
    private native Class<?> findBootstrapClass(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Returns the class with the given <a href="#name">binary name</a> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * loader has been recorded by the Java virtual machine as an initiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * loader of a class with that <a href="#name">binary name</a>.  Otherwise
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1017
     * <tt>null</tt> is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @return  The <tt>Class</tt> object, or <tt>null</tt> if the class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *          not been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    protected final Class<?> findLoadedClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        return findLoadedClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1033
    private native final Class<?> findLoadedClass0(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * Sets the signers of a class.  This should be invoked after defining a
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1037
     * class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *         The <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @param  signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *         The signers for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    protected final void setSigners(Class<?> c, Object[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        c.setSigners(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    // -- Resource --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Finds the resource with the given name.  A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <p> This method will first search the parent class loader for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * resource; if the parent is <tt>null</tt> the path of the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * built-in to the virtual machine is searched.  That failing, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * will invoke {@link #findResource(String)} to find the resource.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
20782
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1067
     * @apiNote When overriding this method it is recommended that an
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1068
     * implementation ensures that any delegation is consistent with the {@link
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1069
     * #getResources(java.lang.String) getResources(String)} method.
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1070
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *          <tt>null</tt> if the resource could not be found or the invoker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *          doesn't have adequate  privileges to get the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    public URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            url = parent.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            url = getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            url = findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * Finds all the resources with the given name. A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * <p>The name of a resource is a <tt>/</tt>-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
20782
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1104
     * @apiNote When overriding this method it is recommended that an
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1105
     * implementation ensures that any delegation is consistent with the {@link
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1106
     * #getResource(java.lang.String) getResource(String)} method. This should
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1107
     * ensure that the first element returned by the Enumeration's
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1108
     * {@code nextElement} method is the same resource that the
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1109
     * {@code getResource(String)} method would return.
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1110
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *          the resource.  If no resources could  be found, the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     *          will be empty.  Resources that the class loader doesn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *          access to will not be in the enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @see  #findResources(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    public Enumeration<URL> getResources(String name) throws IOException {
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11117
diff changeset
  1127
        @SuppressWarnings("unchecked")
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11117
diff changeset
  1128
        Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            tmp[0] = parent.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            tmp[0] = getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        tmp[1] = findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1136
        return new CompoundEnumeration<>(tmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * Finds the resource with the given name. Class loader implementations
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1141
     * should override this method to specify where to find resources.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *          <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    protected URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * representing all the resources with the given name. Class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * implementations should override this method to specify where to load
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1159
     * resources from.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *          the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    protected Enumeration<URL> findResources(String name) throws IOException {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1173
        return java.util.Collections.emptyEnumeration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1176
    /**
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1177
     * Registers the caller as parallel capable.
6890
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
  1178
     * The registration succeeds if and only if all of the following
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1179
     * conditions are met:
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1180
     * <ol>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1181
     * <li> no instance of the caller has been created</li>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1182
     * <li> all of the super classes (except class Object) of the caller are
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1183
     * registered as parallel capable</li>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1184
     * </ol>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1185
     * <p>Note that once a class loader is registered as parallel capable, there
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1186
     * is no way to change it back.</p>
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1187
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1188
     * @return  true if the caller is successfully registered as
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1189
     *          parallel capable and false if otherwise.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1190
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1191
     * @since   1.7
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1192
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1193
    @CallerSensitive
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1194
    protected static boolean registerAsParallelCapable() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1195
        Class<? extends ClassLoader> callerClass =
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1196
            Reflection.getCallerClass().asSubclass(ClassLoader.class);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1197
        return ParallelLoaders.register(callerClass);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1198
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1199
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * Find a resource of the specified name from the search path used to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * classes.  This method locates the resource through the system class
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1203
     * loader (see {@link #getSystemClassLoader()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return  A {@link java.net.URL <tt>URL</tt>} object for reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *          resource, or <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    public static URL getSystemResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            return getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        return system.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * Finds all resources of the specified name from the search path used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * load classes.  The resources thus found are returned as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * java.net.URL <tt>URL</tt>} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * #getSystemResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @return  An enumeration of resource {@link java.net.URL <tt>URL</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *          objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public static Enumeration<URL> getSystemResources(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            return getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        return system.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    private static URL getBootstrapResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        URLClassPath ucp = getBootstrapClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        Resource res = ucp.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        return res != null ? res.getURL() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1263
    private static Enumeration<URL> getBootstrapResources(String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1266
        final Enumeration<Resource> e =
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1267
            getBootstrapClassPath().getResources(name);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28544
diff changeset
  1268
        return new Enumeration<> () {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1269
            public URL nextElement() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1270
                return e.nextElement().getURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                return e.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    // Returns the URLClassPath that is used for finding system resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    static URLClassPath getBootstrapClassPath() {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
  1280
        return sun.misc.Launcher.getBootstrapClassPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        URL url = getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Open for reading, a resource of the specified name from the search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * used to load classes.  This method locates the resource through the
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1310
     * system class loader (see {@link #getSystemClassLoader()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    public static InputStream getSystemResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        URL url = getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // -- Hierarchy --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * Returns the parent class loader for delegation. Some implementations may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * use <tt>null</tt> to represent the bootstrap class loader. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * will return <tt>null</tt> in such implementations if this class loader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * parent is the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * not <tt>null</tt> and is not an ancestor of this class loader, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * method invokes the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * access to the parent class loader is permitted.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * @return  The parent <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *          method doesn't allow access to this class loader's parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *          loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1357
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    public final ClassLoader getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        if (parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (sm != null) {
28544
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1363
            // Check access to the parent class loader
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1364
            // If the caller's class loader is same as this class loader,
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1365
            // permission check is performed.
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1366
            checkClassLoaderPermission(parent, Reflection.getCallerClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Returns the system class loader for delegation.  This is the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * delegation parent for new <tt>ClassLoader</tt> instances, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * typically the class loader used to start the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <p> This method is first invoked early in the runtime's startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * sequence, at which point it creates the system class loader and sets it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * as the context class loader of the invoking <tt>Thread</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * <p> The default system class loader is an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * when this method is first invoked then the value of that property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * taken to be the name of a class that will be returned as the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * class loader.  The class is loaded using the default system class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * and must define a public constructor that takes a single parameter of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * type <tt>ClassLoader</tt> which is used as the delegation parent.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * instance is then created using this constructor with the default system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * class loader as the parameter.  The resulting class loader is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * to be the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * not <tt>null</tt> and the invoker's class loader is not the same as or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * an ancestor of the system class loader, then this method invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * access to the system class loader.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @return  The system <tt>ClassLoader</tt> for delegation, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *          <tt>null</tt> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *          method doesn't allow access to the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *          If invoked recursively during the construction of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *          loader specified by the "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *          property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * @throws  Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *          If the system property "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *          is defined but the named class could not be loaded, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *          provider class does not define the required constructor, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *          exception is thrown by that constructor when it is invoked. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *          underlying cause of the error can be retrieved via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *          {@link Throwable#getCause()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @revised  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1426
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    public static ClassLoader getSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        initSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (scl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (sm != null) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1434
            checkClassLoaderPermission(scl, Reflection.getCallerClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        return scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    private static synchronized void initSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        if (!sclSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            if (scl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                throw new IllegalStateException("recursive invocation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                Throwable oops = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                scl = l.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1448
                    scl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1449
                        new SystemClassLoaderAction(scl));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    oops = pae.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    if (oops instanceof InvocationTargetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                        oops = oops.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                if (oops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                    if (oops instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                        throw (Error) oops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                        // wrap the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                        throw new Error(oops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            sclSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    // Returns true if the specified class loader can be found in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    // loader's delegation chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    boolean isAncestor(ClassLoader cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        ClassLoader acl = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            acl = acl.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            if (cl == acl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        } while (acl != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1482
    // Tests if class loader access requires "getClassLoader" permission
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1483
    // check.  A class loader 'from' can access class loader 'to' if
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1484
    // class loader 'from' is same as class loader 'to' or an ancestor
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1485
    // of 'to'.  The class loader in a system domain can access
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1486
    // any class loader.
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1487
    private static boolean needsClassLoaderPermissionCheck(ClassLoader from,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1488
                                                           ClassLoader to)
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1489
    {
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1490
        if (from == to)
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1491
            return false;
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1492
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1493
        if (from == null)
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1494
            return false;
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1495
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1496
        return !to.isAncestor(from);
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1497
    }
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  1498
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1499
    // Returns the class's class loader, or null if none.
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1500
    static ClassLoader getClassLoader(Class<?> caller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        // This can be null if the VM is requesting it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        if (caller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        // Circumvent security check since this is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        return caller.getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
28544
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1509
    /*
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1510
     * Checks RuntimePermission("getClassLoader") permission
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1511
     * if caller's class loader is not null and caller's class loader
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1512
     * is not the same as or an ancestor of the given cl argument.
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1513
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1514
    static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1515
        SecurityManager sm = System.getSecurityManager();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1516
        if (sm != null) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1517
            // caller can be null if the VM is requesting it
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1518
            ClassLoader ccl = getClassLoader(caller);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1519
            if (needsClassLoaderPermissionCheck(ccl, cl)) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1520
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1521
            }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1522
        }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1523
    }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1524
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    // The class loader for the system
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1526
    // @GuardedBy("ClassLoader.class")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    private static ClassLoader scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    // Set to true once the system class loader has been set
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1530
    // @GuardedBy("ClassLoader.class")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    private static boolean sclSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    // -- Package --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * Defines a package by name in this <tt>ClassLoader</tt>.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * class loaders to define the packages for their classes. Packages must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * be created before the class is defined, and package names must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * unique within a class loader and cannot be redefined or changed once
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1541
     * created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @param  specTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *         The specification title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * @param  specVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *         The specification version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @param  specVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     *         The specification vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * @param  implTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *         The implementation title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * @param  implVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *         The implementation version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * @param  implVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     *         The implementation vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @param  sealBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     *         If not <tt>null</tt>, then this package is sealed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     *         respect to the given code source {@link java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     *         <tt>URL</tt>}  object.  Otherwise, the package is not sealed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * @return  The newly defined <tt>Package</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     *          If package name duplicates an existing package either in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *          class loader or one of its ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    protected Package definePackage(String name, String specTitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                                    String specVersion, String specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                    String implTitle, String implVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                    String implVendor, URL sealBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    {
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1583
        Package pkg = getPackage(name);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1584
        if (pkg != null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1585
            throw new IllegalArgumentException(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1587
        pkg = new Package(name, specTitle, specVersion, specVendor,
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1588
                          implTitle, implVersion, implVendor,
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1589
                          sealBase, this);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1590
        if (packages.putIfAbsent(name, pkg) != null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1591
            throw new IllegalArgumentException(name);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1592
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1593
        return pkg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * Returns a <tt>Package</tt> that has been defined by this class loader
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1598
     * or any of its ancestors.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * @return  The <tt>Package</tt> corresponding to the given name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *          <tt>null</tt> if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    protected Package getPackage(String name) {
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1609
        Package pkg = packages.get(name);
8789
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1610
        if (pkg == null) {
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1611
            if (parent != null) {
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1612
                pkg = parent.getPackage(name);
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1613
            } else {
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1614
                pkg = Package.getSystemPackage(name);
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1615
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
8789
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  1617
        return pkg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * Returns all of the <tt>Packages</tt> defined by this class loader and
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1622
     * its ancestors.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * @return  The array of <tt>Package</tt> objects defined by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *          <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    protected Package[] getPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        Package[] pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            pkgs = parent.getPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            pkgs = Package.getSystemPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        }
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1636
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1637
        Map<String, Package> map = packages;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        if (pkgs != null) {
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1639
            map = new HashMap<>(packages);
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1640
            for (Package pkg : pkgs) {
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  1641
                map.putIfAbsent(pkg.getName(), pkg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1644
        return map.values().toArray(new Package[map.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    // -- Native library access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * Returns the absolute path name of a native library.  The VM invokes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * method to locate the native libraries that belong to classes loaded with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * this class loader. If this method returns <tt>null</tt>, the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * searches the library along the path specified as the
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1655
     * "<tt>java.library.path</tt>" property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * @param  libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *         The library name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @return  The absolute path of the native library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @see  System#loadLibrary(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * @see  System#mapLibraryName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    protected String findLibrary(String libname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * The inner class NativeLibrary denotes a loaded native library instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * Every classloader contains a vector of loaded native libraries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * private field <tt>nativeLibraries</tt>.  The native libraries loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * into the system are entered into the <tt>systemNativeLibraries</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * <p> Every native library requires a particular version of JNI. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * denoted by the private <tt>jniVersion</tt> field.  This field is set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * the VM when it loads the library, and used by the VM to pass the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * version of JNI to the native methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * @see      ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    static class NativeLibrary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        // opaque handle to native library, used in native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        // the version of JNI environment the native library requires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        private int jniVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        // the class from which the library is loaded, also indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        // the loader this native library belongs.
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1693
        private final Class<?> fromClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        // the canonicalized name of the native library.
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1695
        // or static library name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        String name;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1697
        // Indicates if the native library is linked into the VM
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1698
        boolean isBuiltin;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1699
        // Indicates if the native library is loaded
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1700
        boolean loaded;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1701
        native void load(String name, boolean isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        native long find(String name);
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1704
        native void unload(String name, boolean isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1706
        public NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            this.fromClass = fromClass;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1709
            this.isBuiltin = isBuiltin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            synchronized (loadedLibraryNames) {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1714
                if (fromClass.getClassLoader() != null && loaded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    /* remove the native library name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    int size = loadedLibraryNames.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                        if (name.equals(loadedLibraryNames.elementAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                            loadedLibraryNames.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    /* unload the library. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    ClassLoader.nativeLibraryContext.push(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    try {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1726
                        unload(name, isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                        ClassLoader.nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        // Invoked in the VM to determine the context class in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        // JNI_Load/JNI_Unload
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1735
        static Class<?> getFromClass() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1736
            return ClassLoader.nativeLibraryContext.peek().fromClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    // All native library names we've loaded.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1741
    private static Vector<String> loadedLibraryNames = new Vector<>();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1742
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    // Native libraries belonging to system classes.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1744
    private static Vector<NativeLibrary> systemNativeLibraries
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1745
        = new Vector<>();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1746
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    // Native libraries associated with the class loader.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1748
    private Vector<NativeLibrary> nativeLibraries = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    // native libraries being loaded/unloaded.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1751
    private static Stack<NativeLibrary> nativeLibraryContext = new Stack<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    // The paths searched for libraries
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1754
    private static String usr_paths[];
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1755
    private static String sys_paths[];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1757
    private static String[] initializePath(String propName) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1758
        String ldPath = System.getProperty(propName, "");
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1759
        int ldLen = ldPath.length();
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1760
        char ps = File.pathSeparatorChar;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1761
        int psCount = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1762
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1763
        if (ClassLoaderHelper.allowsQuotedPathElements &&
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1764
                ldPath.indexOf('\"') >= 0) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1765
            // First, remove quotes put around quoted parts of paths.
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1766
            // Second, use a quotation mark as a new path separator.
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1767
            // This will preserve any quoted old path separators.
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1768
            char[] buf = new char[ldLen];
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1769
            int bufLen = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1770
            for (int i = 0; i < ldLen; ++i) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1771
                char ch = ldPath.charAt(i);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1772
                if (ch == '\"') {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1773
                    while (++i < ldLen &&
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1774
                            (ch = ldPath.charAt(i)) != '\"') {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1775
                        buf[bufLen++] = ch;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1776
                    }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1777
                } else {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1778
                    if (ch == ps) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1779
                        psCount++;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1780
                        ch = '\"';
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1781
                    }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1782
                    buf[bufLen++] = ch;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1783
                }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1784
            }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1785
            ldPath = new String(buf, 0, bufLen);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1786
            ldLen = bufLen;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1787
            ps = '\"';
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1788
        } else {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1789
            for (int i = ldPath.indexOf(ps); i >= 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1790
                    i = ldPath.indexOf(ps, i + 1)) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1791
                psCount++;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1792
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1795
        String[] paths = new String[psCount + 1];
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1796
        int pathStart = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1797
        for (int j = 0; j < psCount; ++j) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1798
            int pathEnd = ldPath.indexOf(ps, pathStart);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1799
            paths[j] = (pathStart < pathEnd) ?
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1800
                    ldPath.substring(pathStart, pathEnd) : ".";
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1801
            pathStart = pathEnd + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1803
        paths[psCount] = (pathStart < ldLen) ?
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  1804
                ldPath.substring(pathStart, ldLen) : ".";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        return paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1809
    static void loadLibrary(Class<?> fromClass, String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                            boolean isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            (fromClass == null) ? null : fromClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        if (sys_paths == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            usr_paths = initializePath("java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            sys_paths = initializePath("sun.boot.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        if (isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            if (loadLibrary0(fromClass, new File(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            throw new UnsatisfiedLinkError("Can't load library: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            String libfilename = loader.findLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            if (libfilename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                File libfile = new File(libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                if (!libfile.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    throw new UnsatisfiedLinkError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                throw new UnsatisfiedLinkError("Can't load " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1837
        for (String sys_path : sys_paths) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1838
            File libfile = new File(sys_path, System.mapLibraryName(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
12298
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1842
            libfile = ClassLoaderHelper.mapAlternativeName(libfile);
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1843
            if (libfile != null && loadLibrary0(fromClass, libfile)) {
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1844
                return;
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1845
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        if (loader != null) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1848
            for (String usr_path : usr_paths) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1849
                File libfile = new File(usr_path, System.mapLibraryName(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                }
12298
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1853
                libfile = ClassLoaderHelper.mapAlternativeName(libfile);
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1854
                if (libfile != null && loadLibrary0(fromClass, libfile)) {
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1855
                    return;
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  1856
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        // Oops, it failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
31084
0acb07ceb0bc 8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents: 29986
diff changeset
  1863
    static native String findBuiltinLib(String name);
0acb07ceb0bc 8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents: 29986
diff changeset
  1864
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1865
    private static boolean loadLibrary0(Class<?> fromClass, final File file) {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1866
        // Check to see if we're attempting to access a static library
31084
0acb07ceb0bc 8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents: 29986
diff changeset
  1867
        String name = findBuiltinLib(file.getName());
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1868
        boolean isBuiltin = (name != null);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1869
        if (!isBuiltin) {
25167
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1870
            name = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28544
diff changeset
  1871
                new PrivilegedAction<>() {
25167
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1872
                    public String run() {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1873
                        try {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1874
                            return file.exists() ? file.getCanonicalPath() : null;
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1875
                        } catch (IOException e) {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1876
                            return null;
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1877
                        }
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1878
                    }
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1879
                });
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  1880
            if (name == null) {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1881
                return false;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1882
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            (fromClass == null) ? null : fromClass.getClassLoader();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1886
        Vector<NativeLibrary> libs =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            for (int i = 0; i < size; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1891
                NativeLibrary lib = libs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            synchronized (loadedLibraryNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                if (loadedLibraryNames.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                    throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                        ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                         name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                         " already loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                /* If the library is being loaded (must be by the same thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                 * because Runtime.load and Runtime.loadLibrary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                 * synchronous). The reason is can occur is that the JNI_OnLoad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                 * function can cause another loadLibrary invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                 * Thus we can use a static stack to hold the list of libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                 * we are loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                 * If there is a pending load operation for the library, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                 * immediately return success; otherwise, we raise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                 * UnsatisfiedLinkError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                int n = nativeLibraryContext.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                for (int i = 0; i < n; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1918
                    NativeLibrary lib = nativeLibraryContext.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                        if (loader == lib.fromClass.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                            throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                                ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                                 name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                                 " is being loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                }
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1930
                NativeLibrary lib = new NativeLibrary(fromClass, name, isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                nativeLibraryContext.push(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                try {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1933
                    lib.load(name, isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                    nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                }
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  1937
                if (lib.loaded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                    loadedLibraryNames.addElement(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                    libs.addElement(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    // Invoked in the VM class linking code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    static long findNative(ClassLoader loader, String name) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1949
        Vector<NativeLibrary> libs =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            for (int i = 0; i < size; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1954
                NativeLibrary lib = libs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                long entry = lib.find(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                if (entry != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                    return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    // -- Assertion management --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1966
    final Object assertionLock;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1967
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    // The default toggle for assertion checking.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1969
    // @GuardedBy("assertionLock")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    private boolean defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    // Maps String packageName to Boolean package default assertion status Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    // that the default package is placed under a null map key.  If this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
    // is null then we are delegating assertion status queries to the VM, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    // none of this ClassLoader's assertion status modification methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    // been invoked.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1977
    // @GuardedBy("assertionLock")
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1978
    private Map<String, Boolean> packageAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    // field is null then we are delegating assertion status queries to the VM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    // i.e., none of this ClassLoader's assertion status modification methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    // have been invoked.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1984
    // @GuardedBy("assertionLock")
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1985
    Map<String, Boolean> classAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * Sets the default assertion status for this class loader.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * determines whether classes loaded by this class loader and initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * in the future will have assertions enabled or disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * This setting may be overridden on a per-package or per-class basis by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1993
     * #setClassAssertionStatus(String, boolean)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     *         <tt>true</tt> if classes loaded by this class loader will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     *         henceforth have assertions enabled by default, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *         if they will have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2002
    public void setDefaultAssertionStatus(boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2003
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2004
            if (classAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2005
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2007
            defaultAssertionStatus = enabled;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2008
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * Sets the package default assertion status for the named package.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * package default assertion status determines the assertion status for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * classes initialized in the future that belong to the named package or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * any of its "subpackages".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * <p> A subpackage of a package named p is any package whose name begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * with "<tt>p.</tt>".  For example, <tt>javax.swing.text</tt> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * subpackage of <tt>javax.swing</tt>, and both <tt>java.util</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * <tt>java.lang.reflect</tt> are subpackages of <tt>java</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * <p> In the event that multiple package defaults apply to a given class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * the package default pertaining to the most specific package takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * precedence over the others.  For example, if <tt>javax.lang</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * <tt>javax.lang.reflect</tt> both have package defaults associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * them, the latter package default applies to classes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * <tt>javax.lang.reflect</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * <p> Package defaults take precedence over the class loader's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * assertion status, and may be overridden on a per-class basis by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * {@link #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * @param  packageName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     *         The name of the package whose package default assertion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     *         is to be set. A <tt>null</tt> value indicates the unnamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     *         package that is "current"
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
  2037
     *         (see section 7.4.2 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8789
diff changeset
  2038
     *         <cite>The Java&trade; Language Specification</cite>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     *         <tt>true</tt> if classes loaded by this classloader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     *         belonging to the named package or any of its subpackages will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     *         have assertions enabled by default, <tt>false</tt> if they will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     *         have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2048
    public void setPackageAssertionStatus(String packageName,
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2049
                                          boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2050
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2051
            if (packageAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2052
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2054
            packageAssertionStatus.put(packageName, enabled);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2055
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * Sets the desired assertion status for the named top-level class in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * class loader and any nested classes contained therein.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * takes precedence over the class loader's default assertion status, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * over any applicable per-package default.  This method has no effect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * the named class has already been initialized.  (Once a class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * initialized, its assertion status cannot change.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * <p> If the named class is not a top-level class, this invocation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * have no effect on the actual assertion status of any class. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     *         The fully qualified class name of the top-level class whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *         assertion status is to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *         <tt>true</tt> if the named class is to have assertions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     *         enabled when (and if) it is initialized, <tt>false</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     *         class is to have assertions disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2080
    public void setClassAssertionStatus(String className, boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2081
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2082
            if (classAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2083
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2085
            classAssertionStatus.put(className, enabled);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2086
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * Sets the default assertion status for this class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * <tt>false</tt> and discards any package defaults or class assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * status settings associated with the class loader.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * provided so that class loaders can be made to ignore any command line or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * persistent assertion status settings and "start with a clean slate."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2098
    public void clearAssertionStatus() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
         * Whether or not "Java assertion maps" are initialized, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
         * them to empty maps, effectively ignoring any present settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
         */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2103
        synchronized (assertionLock) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2104
            classAssertionStatus = new HashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2105
            packageAssertionStatus = new HashMap<>();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2106
            defaultAssertionStatus = false;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2107
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * Returns the assertion status that would be assigned to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * If the named class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * otherwise, this class loader's default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     *         The fully qualified class name of the class whose desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     *         assertion status is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @return  The desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * @see  #setClassAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @see  #setPackageAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @see  #setDefaultAssertionStatus(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2132
    boolean desiredAssertionStatus(String className) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2133
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2134
            // assert classAssertionStatus   != null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2135
            // assert packageAssertionStatus != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2137
            // Check for a class entry
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2138
            Boolean result = classAssertionStatus.get(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                return result.booleanValue();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2141
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2142
            // Check for most specific package entry
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22581
diff changeset
  2143
            int dotIndex = className.lastIndexOf('.');
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2144
            if (dotIndex < 0) { // default package
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2145
                result = packageAssertionStatus.get(null);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2146
                if (result != null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2147
                    return result.booleanValue();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2148
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2149
            while(dotIndex > 0) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2150
                className = className.substring(0, dotIndex);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2151
                result = packageAssertionStatus.get(className);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2152
                if (result != null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2153
                    return result.booleanValue();
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22581
diff changeset
  2154
                dotIndex = className.lastIndexOf('.', dotIndex-1);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2155
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2156
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2157
            // Return the classloader default
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2158
            return defaultAssertionStatus;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    // Set up the assertions with information provided by the VM.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2163
    // Note: Should only be called inside a synchronized block
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    private void initializeJavaAssertionMaps() {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2165
        // assert Thread.holdsLock(assertionLock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2167
        classAssertionStatus = new HashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2168
        packageAssertionStatus = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        AssertionStatusDirectives directives = retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        for(int i = 0; i < directives.classes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            classAssertionStatus.put(directives.classes[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2173
                                     directives.classEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        for(int i = 0; i < directives.packages.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            packageAssertionStatus.put(directives.packages[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2177
                                       directives.packageEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        defaultAssertionStatus = directives.deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    // Retrieves the assertion directives from the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
    private static native AssertionStatusDirectives retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2187
class SystemClassLoaderAction
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2188
    implements PrivilegedExceptionAction<ClassLoader> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    SystemClassLoaderAction(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2195
    public ClassLoader run() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        String cls = System.getProperty("java.system.class.loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  2201
        Constructor<?> ctor = Class.forName(cls, true, parent)
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  2202
            .getDeclaredConstructor(new Class<?>[] { ClassLoader.class });
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2203
        ClassLoader sys = (ClassLoader) ctor.newInstance(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2204
            new Object[] { parent });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        Thread.currentThread().setContextClassLoader(sys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        return sys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
}