src/java.base/share/classes/java/lang/ClassLoader.java
author rpatil
Wed, 26 Dec 2018 17:09:19 +0530
changeset 53120 de9fd809bb47
parent 53018 8bf9268df0e2
child 54206 003cc64366da
permissions -rw-r--r--
8214567: Use {@systemProperty} for definitions of system properties 8214569: Use {@systemProperty} for definitions of system properties Reviewed-by: lancea, mchung, alanb, naoto Contributed-by: Deepak kejriwal <deepak.kejriwal@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49037
dc68aeea4840 8184289: Obsolete -XX:+UnsyncloadClass and -XX:+MustCallLoadClassInternal options
hseigel
parents: 48671
diff changeset
     2
 * Copyright (c) 2013, 2018, 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
 */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    25
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
    30
import java.io.UncheckedIOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Constructor;
48066
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
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;
48205
6cd25cd7df81 8193159: Reduce the number of classes loaded due to NativeLibrary
mchung
parents: 48066
diff changeset
    41
import java.util.ArrayDeque;
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
    42
import java.util.Arrays;
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    43
import java.util.Collections;
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
    44
import java.util.Deque;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.HashMap;
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
    47
import java.util.HashSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    48
import java.util.Hashtable;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    49
import java.util.Map;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
    50
import java.util.NoSuchElementException;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    51
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Set;
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
    53
import java.util.Spliterator;
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
    54
import java.util.Spliterators;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Vector;
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    56
import java.util.WeakHashMap;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
    57
import java.util.concurrent.ConcurrentHashMap;
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
    58
import java.util.function.Supplier;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    59
import java.util.stream.Stream;
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
    60
import java.util.stream.StreamSupport;
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34825
diff changeset
    61
50634
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
    62
import jdk.internal.loader.BuiltinClassLoader;
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34825
diff changeset
    63
import jdk.internal.perf.PerfCounter;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    64
import jdk.internal.loader.BootLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    65
import jdk.internal.loader.ClassLoaders;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    66
import jdk.internal.misc.Unsafe;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
    67
import jdk.internal.misc.VM;
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
    68
import jdk.internal.ref.CleanerFactory;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36972
diff changeset
    69
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36972
diff changeset
    70
import jdk.internal.reflect.Reflection;
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
    71
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * A class loader is an object that is responsible for loading classes. The
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
    76
 * class {@code ClassLoader} is an abstract class.  Given the <a
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
    77
 * href="#binary-name">binary name</a> of a class, a class loader should attempt to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * locate or generate data that constitutes a definition for the class.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * typical strategy is to transform the name into a file name and then read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * "class file" of that name from a file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
    82
 * <p> Every {@link java.lang.Class Class} object contains a {@link
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
    83
 * Class#getClassLoader() reference} to the {@code ClassLoader} that defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
    86
 * <p> {@code Class} objects for array classes are not created by class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * loaders, but are created automatically as required by the Java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The class loader for an array class, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Class#getClassLoader()} is the same as the class loader for its element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * type; if the element type is a primitive type, then the array class has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
    93
 * <p> Applications implement subclasses of {@code ClassLoader} in order to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * extend the manner in which the Java virtual machine dynamically loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p> Class loaders may typically be used by security managers to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * security domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   100
 * <p> In addition to loading classes, a class loader is also responsible for
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   101
 * locating resources. A resource is some data (a "{@code .class}" file,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   102
 * configuration data, or an image for example) that is identified with an
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   103
 * abstract '/'-separated path name. Resources are typically packaged with an
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   104
 * application or library so that they can be located by code in the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   105
 * application or library. In some cases, the resources are included so that
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   106
 * they can be located by other libraries.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   107
 *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   108
 * <p> The {@code ClassLoader} class uses a delegation model to search for
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   109
 * classes and resources.  Each instance of {@code ClassLoader} has an
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   110
 * associated parent class loader. When requested to find a class or
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   111
 * resource, a {@code ClassLoader} instance will usually delegate the search
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   112
 * for the class or resource to its parent class loader before attempting to
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   113
 * find the class or resource itself.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   115
 * <p> Class loaders that support concurrent loading of classes are known as
42224
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
   116
 * <em>{@linkplain #isRegisteredAsParallelCapable() parallel capable}</em> class
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
   117
 * loaders and are required to register themselves at their class initialization
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
   118
 * time by invoking the {@link
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   119
 * #registerAsParallelCapable ClassLoader.registerAsParallelCapable}
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   120
 * method. Note that the {@code ClassLoader} class is registered as parallel
6890
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   121
 * capable by default. However, its subclasses still need to register themselves
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   122
 * if they are parallel capable.
6890
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
   123
 * 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
   124
 * 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
   125
 * 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
   126
 * duration of the class loading process (see {@link #loadClass
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   127
 * loadClass} methods).
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   128
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44546
diff changeset
   129
 * <h3> <a id="builtinLoaders">Run-time Built-in Class Loaders</a></h3>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   130
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   131
 * The Java run-time has the following built-in class loaders:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   132
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   133
 * <ul>
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   134
 * <li><p>Bootstrap class loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   135
 *     It is the virtual machine's built-in class loader, typically represented
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   136
 *     as {@code null}, and does not have a parent.</li>
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   137
 * <li><p>{@linkplain #getPlatformClassLoader() Platform class loader}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   138
 *     All <em>platform classes</em> are visible to the platform class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   139
 *     that can be used as the parent of a {@code ClassLoader} instance.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   140
 *     Platform classes include Java SE platform APIs, their implementation
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   141
 *     classes and JDK-specific run-time classes that are defined by the
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   142
 *     platform class loader or its ancestors.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   143
 *     <p> To allow for upgrading/overriding of modules defined to the platform
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   144
 *     class loader, and where upgraded modules read modules defined to class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   145
 *     loaders other than the platform class loader and its ancestors, then
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   146
 *     the platform class loader may have to delegate to other class loaders,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   147
 *     the application class loader for example.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   148
 *     In other words, classes in named modules defined to class loaders
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   149
 *     other than the platform class loader and its ancestors may be visible
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   150
 *     to the platform class loader. </li>
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   151
 * <li><p>{@linkplain #getSystemClassLoader() System class loader}.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   152
 *     It is also known as <em>application class loader</em> and is distinct
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   153
 *     from the platform class loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   154
 *     The system class loader is typically used to define classes on the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   155
 *     application class path, module path, and JDK-specific tools.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   156
 *     The platform class loader is a parent or an ancestor of the system class
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   157
 *     loader that all platform classes are visible to it.</li>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   158
 * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   159
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p> Normally, the Java virtual machine loads classes from the local file
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   161
 * system in a platform-dependent manner.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   162
 * However, some classes may not originate from a file; they may originate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * from other sources, such as the network, or they could be constructed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * application.  The method {@link #defineClass(String, byte[], int, int)
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   165
 * defineClass} converts an array of bytes into an instance of class
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   166
 * {@code Class}. Instances of this newly defined class can be created using
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   167
 * {@link Class#newInstance Class.newInstance}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <p> The methods and constructors of objects created by a class loader may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * reference other classes.  To determine the class(es) referred to, the Java
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   171
 * virtual machine invokes the {@link #loadClass loadClass} method of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * the class loader that originally created the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <p> For example, an application could create a network class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * download class files from a server.  Sample code might look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *   ClassLoader loader&nbsp;= new NetworkClassLoader(host,&nbsp;port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *   Object main&nbsp;= loader.loadClass("Main", true).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *       &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p> The network class loader subclass must define the methods {@link
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   184
 * #findClass findClass} and {@code loadClassData} to load a class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * from the network.  Once it has downloaded the bytes that make up the class,
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   186
 * it should use the method {@link #defineClass defineClass} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * create a class instance.  A sample implementation is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     class NetworkClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *         String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *         int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *             byte[] b = loadClassData(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *             return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *         private byte[] loadClassData(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *             // load the class data from the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *             &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   206
 * <h3> <a id="binary-name">Binary names</a> </h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   208
 * <p> Any class name provided as a {@code String} parameter to methods in
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   209
 * {@code ClassLoader} must be a binary name 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
   210
 * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * <p> Examples of valid class names include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *   "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *   "javax.swing.JSpinner$DefaultEditor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *   "java.security.KeyStore$Builder$FileBuilder$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *   "java.net.URLClassLoader$3$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   220
 * <p> Any package name provided as a {@code String} parameter to methods in
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   221
 * {@code ClassLoader} must be either the empty string (denoting an unnamed package)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   222
 * or a fully qualified name as defined by
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   223
 * <cite>The Java&trade; Language Specification</cite>.
22571
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   224
 *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   225
 * @jls 6.7  Fully Qualified Names
22571
985dc9e27435 6516909: (cl spec) ClassLoader.loadClass() clarification to indicate it shouldn't be used for array classes
mchung
parents: 22570
diff changeset
   226
 * @jls 13.1 The Form of a Binary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * @see      #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * @since 1.0
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   229
 * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   230
 * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
public abstract class ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // The parent class loader for delegation
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   240
    // 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
   241
    // must be added *after* it.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   242
    private final ClassLoader parent;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   243
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   244
    // class loader name
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   245
    private final String name;
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   246
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   247
    // the unnamed module for this ClassLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   248
    private final Module unnamedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   249
50634
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   250
    // a string for exception message printing
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   251
    private final String nameAndId;
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   252
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   253
    /**
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   254
     * Encapsulates the set of parallel capable loader types.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   255
     */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   256
    private static class ParallelLoaders {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   257
        private ParallelLoaders() {}
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   258
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   259
        // the set of parallel capable loader types
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   260
        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
   261
            Collections.newSetFromMap(new WeakHashMap<>());
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   262
        static {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   263
            synchronized (loaderTypes) { loaderTypes.add(ClassLoader.class); }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   264
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   265
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   266
        /**
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25167
diff changeset
   267
         * Registers the given class loader type as parallel capable.
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   268
         * Returns {@code true} is successfully registered; {@code false} if
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   269
         * loader's super class is not registered.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   270
         */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   271
        static boolean register(Class<? extends ClassLoader> c) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   272
            synchronized (loaderTypes) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   273
                if (loaderTypes.contains(c.getSuperclass())) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   274
                    // register the class loader as parallel capable
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   275
                    // if and only if all of its super classes are.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   276
                    // Note: given current classloading sequence, if
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   277
                    // the immediate super class is parallel capable,
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   278
                    // all the super classes higher up must be too.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   279
                    loaderTypes.add(c);
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   280
                    return true;
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   281
                } else {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   282
                    return false;
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   283
                }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   284
            }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   285
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   286
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   287
        /**
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   288
         * Returns {@code true} if the given class loader type is
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   289
         * registered as parallel capable.
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   290
         */
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   291
        static boolean isRegistered(Class<? extends ClassLoader> c) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   292
            synchronized (loaderTypes) {
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   293
                return loaderTypes.contains(c);
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   294
            }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   295
        }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   296
    }
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   297
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   298
    // 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
   299
    // class loader is parallel capable.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   300
    // 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
   301
    // 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
   302
    private final ConcurrentHashMap<String, Object> parallelLockMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   304
    // Maps packages to certs
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   305
    private final Map <String, Certificate[]> package2certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    // 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
   308
    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
   309
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   310
    // 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
   311
    // 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
   312
    private final Vector<Class<?>> classes = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   314
    // 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
   315
    // created classes.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   316
    private final ProtectionDomain defaultDomain =
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   317
        new ProtectionDomain(new CodeSource(null, (Certificate[]) null),
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   318
                             null, this, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    // 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
   321
    void addClass(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        classes.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   325
    // The packages defined in this class loader.  Each package name is
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   326
    // mapped to its corresponding NamedPackage object.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   327
    //
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   328
    // The value is a Package object if ClassLoader::definePackage,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   329
    // Class::getPackage, ClassLoader::getDefinePackage(s) or
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   330
    // Package::getPackage(s) method is called to define it.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   331
    // Otherwise, the value is a NamedPackage object.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   332
    private final ConcurrentHashMap<String, NamedPackage> packages
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
   333
            = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   335
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   336
     * Returns a named package for the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   337
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   338
    private NamedPackage getNamedPackage(String pn, Module m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   339
        NamedPackage p = packages.get(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   340
        if (p == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   341
            p = new NamedPackage(pn, m);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   342
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   343
            NamedPackage value = packages.putIfAbsent(pn, p);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   344
            if (value != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   345
                // Package object already be defined for the named package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   346
                p = value;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   347
                // if definePackage is called by this class loader to define
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   348
                // a package in a named module, this will return Package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   349
                // object of the same name.  Package object may contain
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   350
                // unexpected information but it does not impact the runtime.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   351
                // this assertion may be helpful for troubleshooting
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   352
                assert value.module() == m;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   353
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   354
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   355
        return p;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   356
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   357
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   358
    private static Void checkCreateClassLoader() {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   359
        return checkCreateClassLoader(null);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   360
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   361
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   362
    private static Void checkCreateClassLoader(String name) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   363
        if (name != null && name.isEmpty()) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   364
            throw new IllegalArgumentException("name must be non-empty or null");
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   365
        }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   366
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   367
        SecurityManager security = System.getSecurityManager();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   368
        if (security != null) {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   369
            security.checkCreateClassLoader();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   370
        }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   371
        return null;
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   372
    }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   373
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   374
    private ClassLoader(Void unused, String name, ClassLoader parent) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   375
        this.name = name;
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   376
        this.parent = parent;
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
   377
        this.unnamedModule = new Module(this);
4352
ddaa5f39a2ac 6902010: (cl) Delay initialization of ClassLoader.parallelLoaders
alanb
parents: 4209
diff changeset
   378
        if (ParallelLoaders.isRegistered(this.getClass())) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   379
            parallelLockMap = new ConcurrentHashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   380
            package2certs = new ConcurrentHashMap<>();
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   381
            assertionLock = new Object();
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   382
        } else {
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   383
            // no finer-grained lock; lock on the classloader instance
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   384
            parallelLockMap = null;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
   385
            package2certs = new Hashtable<>();
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   386
            assertionLock = this;
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   387
        }
50634
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   388
        this.nameAndId = nameAndId(this);
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   389
    }
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   390
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   391
    /**
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   392
     * If the defining loader has a name explicitly set then
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   393
     *       '<loader-name>' @<id>
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   394
     * If the defining loader has no name then
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   395
     *       <qualified-class-name> @<id>
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   396
     * If it's built-in loader then omit `@<id>` as there is only one instance.
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   397
     */
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   398
    private static String nameAndId(ClassLoader ld) {
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   399
        String nid = ld.getName() != null ? "\'" + ld.getName() + "\'"
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   400
                                          : ld.getClass().getName();
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   401
        if (!(ld instanceof BuiltinClassLoader)) {
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   402
            String id = Integer.toHexString(System.identityHashCode(ld));
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   403
            nid = nid + " @" + id;
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   404
        }
c349d409262a 8202605: Standardize on ClassLoaderData::loader_name() throughout the VM to obtain a class loader's name
lfoltan
parents: 49528
diff changeset
   405
        return nid;
4192
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   406
    }
34d923c996e4 6636650: (cl) Resurrected ClassLoaders can still have children
valeriep
parents: 3111
diff changeset
   407
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   409
     * Creates a new class loader of the specified name and using the
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   410
     * specified parent class loader for delegation.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   411
     *
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   412
     * @apiNote If the parent is specified as {@code null} (for the
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   413
     * bootstrap class loader) then there is no guarantee that all platform
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   414
     * classes are visible.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   415
     *
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   416
     * @param  name   class loader name; or {@code null} if not named
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   417
     * @param  parent the parent class loader
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   418
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   419
     * @throws IllegalArgumentException if the given name is empty.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   420
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   421
     * @throws SecurityException
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   422
     *         If a security manager exists and its
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   423
     *         {@link SecurityManager#checkCreateClassLoader()}
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   424
     *         method doesn't allow creation of a new class loader.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   425
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   426
     * @since  9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   427
     * @spec JPMS
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   428
     */
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   429
    protected ClassLoader(String name, ClassLoader parent) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   430
        this(checkCreateClassLoader(name), name, parent);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   431
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   432
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   433
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Creates a new class loader using the specified parent class loader for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p> If there is a security manager, its {@link
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   438
     * SecurityManager#checkCreateClassLoader() checkCreateClassLoader} method
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   439
     * is invoked.  This may result in a security exception.  </p>
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   440
     *
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   441
     * @apiNote If the parent is specified as {@code null} (for the
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   442
     * bootstrap class loader) then there is no guarantee that all platform
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
   443
     * classes are visible.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param  parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *         The parent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          If a security manager exists and its
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   450
     *          {@code checkCreateClassLoader} method doesn't allow creation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    protected ClassLoader(ClassLoader parent) {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   456
        this(checkCreateClassLoader(), null, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   460
     * Creates a new class loader using the {@code ClassLoader} returned by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * the method {@link #getSystemClassLoader()
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   462
     * getSystemClassLoader()} as the parent class loader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * SecurityManager#checkCreateClassLoader()
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   466
     * checkCreateClassLoader} method is invoked.  This may result in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          If a security manager exists and its
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   471
     *          {@code checkCreateClassLoader} method doesn't allow creation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    protected ClassLoader() {
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   475
        this(checkCreateClassLoader(), null, getSystemClassLoader());
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   476
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   477
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   478
    /**
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   479
     * Returns the name of this class loader or {@code null} if
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   480
     * this class loader is not named.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   481
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   482
     * @apiNote This method is non-final for compatibility.  If this
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   483
     * method is overridden, this method must return the same name
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   484
     * as specified when this class loader was instantiated.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   485
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   486
     * @return name of this class loader; or {@code null} if
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   487
     * this class loader is not named.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   488
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   489
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   490
     * @spec JPMS
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   491
     */
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   492
    public String getName() {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   493
        return name;
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   494
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   495
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   496
    // package-private used by StackTraceElement to avoid
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   497
    // calling the overrideable getName method
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   498
    final String name() {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
   499
        return name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    // -- Class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   505
     * Loads the class with the specified <a href="#binary-name">binary name</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * This method searches for classes in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * machine to resolve class references.  Invoking this method is equivalent
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   509
     * to invoking {@link #loadClass(String, boolean) loadClass(name,
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   510
     * false)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   513
     *         The <a href="#binary-name">binary name</a> of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   515
     * @return  The resulting {@code Class} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *          If the class was not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public Class<?> loadClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   525
     * Loads the class with the specified <a href="#binary-name">binary name</a>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * default implementation of this method searches for classes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 20854
diff changeset
   529
     * <ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *   has already been loaded.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   534
     *   <li><p> Invoke the {@link #loadClass(String) loadClass} method
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   535
     *   on the parent class loader.  If the parent is {@code null} the class
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   536
     *   loader built into the virtual machine is used, instead.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *   class.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <p> If the class was found using the above steps, and the
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   544
     * {@code resolve} flag is true, this method will then invoke the {@link
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   545
     * #resolveClass(Class)} method on the resulting {@code Class} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   547
     * <p> Subclasses of {@code ClassLoader} are encouraged to override {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * #findClass(String)}, rather than this method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   550
     * <p> Unless overridden, this method synchronizes on the result of
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   551
     * {@link #getClassLoadingLock getClassLoadingLock} method
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   552
     * during the entire class loading process.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   553
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   555
     *         The <a href="#binary-name">binary name</a> of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param  resolve
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   558
     *         If {@code true} then resolve the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   560
     * @return  The resulting {@code Class} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   565
    protected Class<?> loadClass(String name, boolean resolve)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   568
        synchronized (getClassLoadingLock(name)) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   569
            // 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
   570
            Class<?> c = findLoadedClass(name);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   571
            if (c == null) {
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   572
                long t0 = System.nanoTime();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   573
                try {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   574
                    if (parent != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   575
                        c = parent.loadClass(name, false);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   576
                    } else {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   577
                        c = findBootstrapClassOrNull(name);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   578
                    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   579
                } catch (ClassNotFoundException e) {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   580
                    // ClassNotFoundException thrown if class not found
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   581
                    // from the non-null parent class loader
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   582
                }
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   583
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
   584
                if (c == null) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   585
                    // 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
   586
                    // to find the class.
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   587
                    long t1 = System.nanoTime();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   588
                    c = findClass(name);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   589
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3833
diff changeset
   590
                    // this is the defining class loader; record the stats
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34825
diff changeset
   591
                    PerfCounter.getParentDelegationTime().addTime(t1 - t0);
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34825
diff changeset
   592
                    PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34825
diff changeset
   593
                    PerfCounter.getFindClasses().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   595
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   596
            if (resolve) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   597
                resolveClass(c);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   598
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   599
            return c;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   600
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   601
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   602
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   603
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   604
     * Loads the class with the specified <a href="#binary-name">binary name</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   605
     * in a module defined to this class loader.  This method returns {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   606
     * if the class could not be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   607
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   608
     * @apiNote This method does not delegate to the parent class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   609
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   610
     * @implSpec The default implementation of this method searches for classes
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   611
     * in the following order:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   612
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   613
     * <ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   614
     *   <li>Invoke {@link #findLoadedClass(String)} to check if the class
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   615
     *   has already been loaded.</li>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   616
     *   <li>Invoke the {@link #findClass(String, String)} method to find the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   617
     *   class in the given module.</li>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   618
     * </ol>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   619
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   620
     * @param  module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   621
     *         The module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   622
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   623
     *         The <a href="#binary-name">binary name</a> of the class
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   624
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   625
     * @return The resulting {@code Class} object in a module defined by
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   626
     *         this class loader, or {@code null} if the class could not be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   627
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   628
    final Class<?> loadClass(Module module, String name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   629
        synchronized (getClassLoadingLock(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   630
            // First, check if the class has already been loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   631
            Class<?> c = findLoadedClass(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   632
            if (c == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   633
                c = findClass(module.getName(), name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   634
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   635
            if (c != null && c.getModule() == module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   636
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   637
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   638
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   639
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   640
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   641
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   642
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   643
    /**
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   644
     * 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
   645
     * 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
   646
     * 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
   647
     * 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
   648
     * with the specified class name. Otherwise, the method returns this
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
   649
     * ClassLoader object.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   650
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   651
     * @param  className
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   652
     *         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
   653
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   654
     * @return the lock for class loading operations
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   655
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   656
     * @throws NullPointerException
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   657
     *         If registered as parallel capable and {@code className} is null
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   658
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   659
     * @see #loadClass(String, boolean)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   660
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   661
     * @since  1.7
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   662
     */
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   663
    protected Object getClassLoadingLock(String className) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   664
        Object lock = this;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   665
        if (parallelLockMap != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   666
            Object newLock = new Object();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   667
            lock = parallelLockMap.putIfAbsent(className, newLock);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   668
            if (lock == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   669
                lock = newLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   672
        return lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   675
    // 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
   676
    private void checkPackageAccess(Class<?> cls, ProtectionDomain pd) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (sm != null) {
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   679
            if (ReflectUtil.isNonPublicProxyClass(cls)) {
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 20854
diff changeset
   680
                for (Class<?> intf: cls.getInterfaces()) {
20817
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   681
                    checkPackageAccess(intf, pd);
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   682
                }
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   683
                return;
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   684
            }
a842c616ae88 8017291: Cast Proxies Aside
mchung
parents: 18156
diff changeset
   685
47722
ce6ff74192fc 8190733: Use Class::getPackageName in java.base implementation
mchung
parents: 47707
diff changeset
   686
            final String packageName = cls.getPackageName();
ce6ff74192fc 8190733: Use Class::getPackageName in java.base implementation
mchung
parents: 47707
diff changeset
   687
            if (!packageName.isEmpty()) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28544
diff changeset
   688
                AccessController.doPrivileged(new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   689
                    public Void run() {
47722
ce6ff74192fc 8190733: Use Class::getPackageName in java.base implementation
mchung
parents: 47707
diff changeset
   690
                        sm.checkPackageAccess(packageName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                }, new AccessControlContext(new ProtectionDomain[] {pd}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   699
     * Finds the class with the specified <a href="#binary-name">binary name</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * This method should be overridden by class loader implementations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * follow the delegation model for loading classes, and will be invoked by
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   702
     * the {@link #loadClass loadClass} method after checking the
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   703
     * parent class loader for the requested class.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   704
     *
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
   705
     * @implSpec The default implementation throws {@code ClassNotFoundException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   708
     *         The <a href="#binary-name">binary name</a> of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   710
     * @return  The resulting {@code Class} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    protected Class<?> findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   722
     * Finds the class with the given <a href="#binary-name">binary name</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   723
     * in a module defined to this class loader.
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
   724
     * Class loader implementations that support loading from modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   725
     * should override this method.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   726
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   727
     * @apiNote This method returns {@code null} rather than throwing
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   728
     *          {@code ClassNotFoundException} if the class could not be found.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   729
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   730
     * @implSpec The default implementation attempts to find the class by
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   731
     * invoking {@link #findClass(String)} when the {@code moduleName} is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   732
     * {@code null}. It otherwise returns {@code null}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   733
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   734
     * @param  moduleName
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   735
     *         The module name; or {@code null} to find the class in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   736
     *         {@linkplain #getUnnamedModule() unnamed module} for this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   737
     *         class loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   738
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   739
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   740
     *         The <a href="#binary-name">binary name</a> of the class
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   741
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   742
     * @return The resulting {@code Class} object, or {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   743
     *         if the class could not be found.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   744
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   745
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   746
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   747
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   748
    protected Class<?> findClass(String moduleName, String name) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   749
        if (moduleName == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   750
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   751
                return findClass(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   752
            } catch (ClassNotFoundException ignore) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
   753
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   754
        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   755
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   756
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   757
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   758
    /**
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   759
     * Converts an array of bytes into an instance of class {@code Class}.
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   760
     * Before the {@code Class} can be used it must be resolved.  This method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * is deprecated in favor of the version that takes a <a
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   762
     * href="#binary-name">binary name</a> as its first argument, and is more secure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *         The bytes that make up the class data.  The bytes in positions
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   766
     *         {@code off} through {@code off+len-1} 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
   767
     *         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
   768
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @param  off
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   771
     *         The start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   776
     * @return  The {@code Class} object that was created from the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *          class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws  IndexOutOfBoundsException
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   783
     *          If either {@code off} or {@code len} is negative, or if
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
   784
     *          {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
7029
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   786
     * @throws  SecurityException
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   787
     *          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
   788
     *          contains classes that were signed by a different set of
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   789
     *          certificates than this class, or if an attempt is made
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   790
     *          to define a class in a package with a fully-qualified name
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   791
     *          that starts with "{@code java.}".
1f8f7240a1e2 6541462: outdated specification for CCC 6339875
kamg
parents: 6890
diff changeset
   792
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @deprecated  Replaced by {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * defineClass(String, byte[], int, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   799
    @Deprecated(since="1.1")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    protected final Class<?> defineClass(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return defineClass(null, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   807
     * Converts an array of bytes into an instance of class {@code Class}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   808
     * Before the {@code Class} can be used it must be resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <p> This method assigns a default {@link java.security.ProtectionDomain
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   811
     * ProtectionDomain} to the newly defined class.  The
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   812
     * {@code ProtectionDomain} is effectively granted the same set of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * permissions returned when {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * java.security.Policy#getPermissions(java.security.CodeSource)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   815
     * Policy.getPolicy().getPermissions(new CodeSource(null, null))}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   816
     * is invoked.  The default protection domain is created on the first invocation
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   817
     * of {@link #defineClass(String, byte[], int, int) defineClass},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * and re-used on subsequent invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   820
     * <p> To assign a specific {@code ProtectionDomain} to the class, use
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * the {@link #defineClass(String, byte[], int, int,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   822
     * java.security.ProtectionDomain) defineClass} method that takes a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   823
     * {@code ProtectionDomain} as one of its arguments.  </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   824
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   825
     * <p>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   826
     * This method defines a package in this class loader corresponding to the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   827
     * package of the {@code Class} (if such a package has not already been defined
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   828
     * in this class loader). The name of the defined package is derived from
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   829
     * the <a href="#binary-name">binary name</a> of the class specified by
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   830
     * the byte array {@code b}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   831
     * Other properties of the defined package are as specified by {@link Package}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   834
     *         The expected <a href="#binary-name">binary name</a> of the class, or
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   835
     *         {@code null} if not known
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *         The bytes that make up the class data.  The bytes in positions
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   839
     *         {@code off} through {@code off+len-1} 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
   840
     *         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
   841
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param  off
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   844
     *         The start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   849
     * @return  The {@code Class} object that was created from the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *          class data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @throws  IndexOutOfBoundsException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   856
     *          If either {@code off} or {@code len} is negative, or if
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   857
     *          {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *          certificates than this class (which is unsigned), or if
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   863
     *          {@code name} begins with "{@code java.}".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @see  java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @see  java.security.SecureClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @since  1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   871
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
   872
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    protected final Class<?> defineClass(String name, byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        return defineClass(name, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /* Determine protection domain, and check that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        - not define java.* class,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   882
        - 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
   883
          package.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    private ProtectionDomain preDefineClass(String name,
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   886
                                            ProtectionDomain pd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            throw new NoClassDefFoundError("IllegalName: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
37585
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37521
diff changeset
   891
        // Note:  Checking logic in java.lang.invoke.MemberName.checkForTypeAlias
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37521
diff changeset
   892
        // relies on the fact that spoofing is impossible if a class has a name
63061ecdf354 8152335: Improve MethodHandle consistency
vlivanov
parents: 37521
diff changeset
   893
        // of the form "java.*"
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   894
        if ((name != null) && name.startsWith("java.")
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   895
                && this != getBuiltinPlatformClassLoader()) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   896
            throw new SecurityException
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   897
                ("Prohibited package name: " +
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   898
                 name.substring(0, name.lastIndexOf('.')));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   900
        if (pd == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   901
            pd = defaultDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   904
        if (name != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   905
            checkCerts(name, pd.getCodeSource());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   906
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   908
        return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   911
    private String defineClassSourceLocation(ProtectionDomain pd) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   912
        CodeSource cs = pd.getCodeSource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        String source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (cs != null && cs.getLocation() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            source = cs.getLocation().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   920
    private void postDefineClass(Class<?> c, ProtectionDomain pd) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   921
        // define a named package, if not present
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   922
        getNamedPackage(c.getPackageName(), c.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   923
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   924
        if (pd.getCodeSource() != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   925
            Certificate certs[] = pd.getCodeSource().getCertificates();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (certs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                setSigners(c, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   932
     * Converts an array of bytes into an instance of class {@code Class},
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   933
     * with a given {@code ProtectionDomain}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   934
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   935
     * <p> If the given {@code ProtectionDomain} is {@code null},
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   936
     * then a default protection domain will be assigned to the class as specified
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   937
     * in the documentation for {@link #defineClass(String, byte[], int, int)}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   938
     * Before the class can be used it must be resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * <p> The first class defined in a package determines the exact set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * certificates that all subsequent classes defined in that package must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * contain.  The set of certificates for a class is obtained from the
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   943
     * {@link java.security.CodeSource CodeSource} within the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   944
     * {@code ProtectionDomain} of the class.  Any classes added to that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * package must contain the same set of certificates or a
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   946
     * {@code SecurityException} will be thrown.  Note that if
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   947
     * {@code name} is {@code null}, this check is not performed.
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   948
     * You should always pass in the <a href="#binary-name">binary name</a> of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * class you are defining as well as the bytes.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * class you are defining is indeed the class you think it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   952
     * <p> If the specified {@code name} begins with "{@code java.}", it can
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   953
     * only be defined by the {@linkplain #getPlatformClassLoader()
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   954
     * platform class loader} or its ancestors; otherwise {@code SecurityException}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   955
     * will be thrown.  If {@code name} is not {@code null}, it must be equal to
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   956
     * the <a href="#binary-name">binary name</a> of the class
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   957
     * specified by the byte array {@code b}, otherwise a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   958
     * NoClassDefFoundError NoClassDefFoundError} will be thrown.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   959
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   960
     * <p> This method defines a package in this class loader corresponding to the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   961
     * package of the {@code Class} (if such a package has not already been defined
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   962
     * in this class loader). The name of the defined package is derived from
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   963
     * the <a href="#binary-name">binary name</a> of the class specified by
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   964
     * the byte array {@code b}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   965
     * Other properties of the defined package are as specified by {@link Package}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   968
     *         The expected <a href="#binary-name">binary name</a> of the class, or
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   969
     *         {@code null} if not known
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *         The bytes that make up the class data. The bytes in positions
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   973
     *         {@code off} through {@code off+len-1} 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
   974
     *         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
   975
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @param  off
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   978
     *         The start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param  protectionDomain
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   984
     *         The {@code ProtectionDomain} of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   986
     * @return  The {@code Class} object created from the data,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   987
     *          and {@code ProtectionDomain}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @throws  NoClassDefFoundError
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   993
     *          If {@code name} is not {@code null} and not equal to the
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
   994
     *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @throws  IndexOutOfBoundsException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   997
     *          If either {@code off} or {@code len} is negative, or if
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
   998
     *          {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *          contains classes that were signed by a different set of
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1003
     *          certificates than this class, or if {@code name} begins with
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1004
     *          "{@code java.}" and this class loader is not the platform
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1005
     *          class loader or its ancestor.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1006
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1007
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1008
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    protected final Class<?> defineClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        String source = defineClassSourceLocation(protectionDomain);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1016
        Class<?> c = defineClass1(this, name, b, off, len, protectionDomain, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1022
     * Converts a {@link java.nio.ByteBuffer ByteBuffer} into an instance
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1023
     * of class {@code Class}, with the given {@code ProtectionDomain}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1024
     * If the given {@code ProtectionDomain} is {@code null}, then a default
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1025
     * protection domain will be assigned to the class as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1029
     * <p>The rules about the first class defined in a package determining the
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1030
     * set of certificates for the package, the restrictions on class names,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1031
     * and the defined package of the class
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1032
     * 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
  1033
     * #defineClass(String, byte[], int, int, ProtectionDomain)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * <p> An invocation of this method of the form
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1036
     * <i>cl</i>{@code .defineClass(}<i>name</i>{@code ,}
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1037
     * <i>bBuffer</i>{@code ,} <i>pd</i>{@code )} yields exactly the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * result as the statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1040
     *<p> <code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * ...<br>
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1042
     * byte[] temp = new byte[bBuffer.{@link
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1043
     * java.nio.ByteBuffer#remaining remaining}()];<br>
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1044
     *     bBuffer.{@link java.nio.ByteBuffer#get(byte[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * get}(temp);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1047
     * cl.defineClass}(name, temp, 0,
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1048
     * temp.length, pd);<br>
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1049
     * </code></p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1052
     *         The expected <a href="#binary-name">binary name</a>. of the class, or
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1053
     *         {@code null} if not known
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *         The bytes that make up the class data. The bytes from positions
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1057
     *         {@code b.position()} through {@code b.position() + b.limit() -1
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1058
     *         } 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
  1059
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param  protectionDomain
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1062
     *         The {@code ProtectionDomain} of the class, or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1064
     * @return  The {@code Class} object created from the data,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1065
     *          and {@code ProtectionDomain}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *          If the data did not contain a valid class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @throws  NoClassDefFoundError
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1071
     *          If {@code name} is not {@code null} and not equal to the
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1072
     *          <a href="#binary-name">binary name</a> of the class specified by {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *          contains classes that were signed by a different set of
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1077
     *          certificates than this class, or if {@code name} begins with
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1078
     *          "{@code java.}".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @since  1.5
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1083
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1084
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        int len = b.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25167
diff changeset
  1092
        // Use byte[] if not a direct ByteBuffer:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (!b.isDirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            if (b.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                return defineClass(name, b.array(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                   b.position() + b.arrayOffset(), len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                   protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                // no array, or read-only array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                return defineClass(name, tb, 0, len, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        String source = defineClassSourceLocation(protectionDomain);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1108
        Class<?> c = defineClass2(this, name, b, b.position(), len, protectionDomain, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1113
    static native Class<?> defineClass1(ClassLoader loader, String name, byte[] b, int off, int len,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1114
                                        ProtectionDomain pd, String source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1116
    static native Class<?> defineClass2(ClassLoader loader, String name, java.nio.ByteBuffer b,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1117
                                        int off, int len, ProtectionDomain pd,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1118
                                        String source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    // true if the name is null or has the potential to be a valid binary name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private boolean checkName(String name) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52915
diff changeset
  1122
        if ((name == null) || (name.isEmpty()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            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
  1124
        if ((name.indexOf('/') != -1) || (name.charAt(0) == '['))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1129
    private void checkCerts(String name, CodeSource cs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        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
  1132
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1133
        Certificate[] certs = null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1134
        if (cs != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1135
            certs = cs.getCertificates();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1136
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1137
        Certificate[] pcerts = null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1138
        if (parallelLockMap == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1139
            synchronized (this) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1140
                pcerts = package2certs.get(pname);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1141
                if (pcerts == null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1142
                    package2certs.put(pname, (certs == null? nocerts:certs));
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1143
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        } else {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1146
            pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs).
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1147
                putIfAbsent(pname, (certs == null? nocerts:certs));
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1148
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1149
        if (pcerts != null && !compareCerts(pcerts, certs)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1150
            throw new SecurityException("class \"" + name
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1151
                + "\"'s signer information does not match signer information"
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1152
                + " of other classes in the same package");
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * check to make sure the certs for the new class (certs) are the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * the certs for the first class inserted in the package (pcerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1160
    private boolean compareCerts(Certificate[] pcerts,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1161
                                 Certificate[] certs)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // certs can be null, indicating no certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if ((certs == null) || (certs.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return pcerts.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // the length must be the same at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        if (certs.length != pcerts.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // go through and make sure all the certs in one array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // are in the other and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        boolean match;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1175
        for (Certificate cert : certs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            match = false;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1177
            for (Certificate pcert : pcerts) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1178
                if (cert.equals(pcert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // now do the same for pcerts
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1187
        for (Certificate pcert : pcerts) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            match = false;
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1189
            for (Certificate cert : certs) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  1190
                if (pcert.equals(cert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * Links the specified class.  This (misleadingly named) method may be
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1203
     * used by a class loader to link a class.  If the class {@code c} has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * 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
  1205
     * 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
  1206
     * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *         The class to link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @throws  NullPointerException
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1212
     *          If {@code c} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @see  #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    protected final void resolveClass(Class<?> c) {
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
  1217
        if (c == null) {
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
  1218
            throw new NullPointerException();
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25991
diff changeset
  1219
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1223
     * Finds a class with the specified <a href="#binary-name">binary name</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * loading it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * <p> This method loads the class through the system class loader (see
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1227
     * {@link #getSystemClassLoader()}).  The {@code Class} object returned
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1228
     * might have more than one {@code ClassLoader} associated with it.
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1229
     * Subclasses of {@code ClassLoader} need not usually invoke this method,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * because most class loaders need to override just {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * #findClass(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1234
     *         The <a href="#binary-name">binary name</a> of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1236
     * @return  The {@code Class} object for the specified {@code name}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @see  #ClassLoader(ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @see  #getParent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    protected final Class<?> findSystemClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    {
40796
91ffd8a6f473 8165563: ClassLoader::getSystemClassLoader will never be null
mchung
parents: 40536
diff changeset
  1247
        return getSystemClassLoader().loadClass(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1250
    /**
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1251
     * 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
  1252
     * or return null if not found.
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1253
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1254
    Class<?> findBootstrapClassOrNull(String name) {
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1255
        if (!checkName(name)) return null;
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 3111
diff changeset
  1260
    // return null if not found
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  1261
    private native Class<?> findBootstrapClass(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1264
     * Returns the class with the given <a href="#binary-name">binary name</a> if this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * loader has been recorded by the Java virtual machine as an initiating
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1266
     * loader of a class with that <a href="#binary-name">binary name</a>.  Otherwise
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1267
     * {@code null} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  1270
     *         The <a href="#binary-name">binary name</a> of the class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1272
     * @return  The {@code Class} object, or {@code null} if the class has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *          not been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    protected final Class<?> findLoadedClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        return findLoadedClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31084
diff changeset
  1283
    private final native Class<?> findLoadedClass0(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * 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
  1287
     * class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @param  c
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1290
     *         The {@code Class} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @param  signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *         The signers for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    protected final void setSigners(Class<?> c, Object[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        c.setSigners(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1302
    // -- Resources --
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1303
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1304
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1305
     * Returns a URL to a resource in a module defined to this class loader.
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1306
     * Class loader implementations that support loading from modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1307
     * should override this method.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1308
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1309
     * @apiNote This method is the basis for the {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1310
     * Class#getResource Class.getResource}, {@link Class#getResourceAsStream
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1311
     * Class.getResourceAsStream}, and {@link Module#getResourceAsStream
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1312
     * Module.getResourceAsStream} methods. It is not subject to the rules for
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1313
     * encapsulation specified by {@code Module.getResourceAsStream}.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1314
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1315
     * @implSpec The default implementation attempts to find the resource by
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1316
     * invoking {@link #findResource(String)} when the {@code moduleName} is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1317
     * {@code null}. It otherwise returns {@code null}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1318
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1319
     * @param  moduleName
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1320
     *         The module name; or {@code null} to find a resource in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1321
     *         {@linkplain #getUnnamedModule() unnamed module} for this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1322
     *         class loader
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1323
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1324
     *         The resource name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1325
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1326
     * @return A URL to the resource; {@code null} if the resource could not be
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1327
     *         found, a URL could not be constructed to locate the resource,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1328
     *         access to the resource is denied by the security manager, or
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1329
     *         there isn't a module of the given name defined to the class
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1330
     *         loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1331
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1332
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1333
     *         If I/O errors occur
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1334
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1335
     * @see java.lang.module.ModuleReader#find(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1336
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1337
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1338
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1339
    protected URL findResource(String moduleName, String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1340
        if (moduleName == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1341
            return findResource(name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1342
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1343
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1344
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1345
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * Finds the resource with the given name.  A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1352
     * <p> The name of a resource is a '{@code /}'-separated path name that
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1353
     * identifies the resource. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1355
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1356
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1357
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1358
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1359
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1360
     * opened} unconditionally (even if the caller of this method is in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1361
     * same module as the resource). </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1362
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1363
     * @implSpec The default implementation will first search the parent class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1364
     * loader for the resource; if the parent is {@code null} the path of the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1365
     * class loader built into the virtual machine is searched. If not found,
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1366
     * this method will invoke {@link #findResource(String)} to find the resource.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1367
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1368
     * @apiNote Where several modules are defined to the same class loader,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1369
     * and where more than one module contains a resource with the given name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1370
     * then the ordering that modules are searched is not specified and may be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1371
     * very unpredictable.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1372
     * When overriding this method it is recommended that an implementation
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1373
     * ensures that any delegation is consistent with the {@link
20782
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1374
     * #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
  1375
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1379
     * @return  {@code URL} object for reading the resource; {@code null} if
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1380
     *          the resource could not be found, a {@code URL} could not be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1381
     *          constructed to locate the resource, the resource is in a package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1382
     *          that is not opened unconditionally, or access to the resource is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1383
     *          denied by the security manager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     *
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1385
     * @throws  NullPointerException If {@code name} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * @since  1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1388
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1389
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    public URL getResource(String name) {
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1392
        Objects.requireNonNull(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            url = parent.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        } else {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1397
            url = BootLoader.findResource(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            url = findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * Finds all the resources with the given name. A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1410
     * <p> The name of a resource is a {@code /}-separated path name that
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1411
     * identifies the resource. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1413
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1414
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1415
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1416
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1417
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1418
     * opened} unconditionally (even if the caller of this method is in the
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1419
     * same module as the resource). </p>
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1420
     *
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1421
     * @implSpec The default implementation will first search the parent class
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1422
     * loader for the resource; if the parent is {@code null} the path of the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1423
     * class loader built into the virtual machine is searched. It then
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1424
     * invokes {@link #findResources(String)} to find the resources with the
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1425
     * name in this class loader. It returns an enumeration whose elements
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1426
     * are the URLs found by searching the parent class loader followed by
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1427
     * the elements found with {@code findResources}.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1428
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1429
     * @apiNote Where several modules are defined to the same class loader,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1430
     * and where more than one module contains a resource with the given name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1431
     * then the ordering is not specified and may be very unpredictable.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1432
     * When overriding this method it is recommended that an
20782
2711da7ccd30 8024704: Improve API documentation of ClassLoader and ServiceLoader with respect to enumeration of resources.
dfuchs
parents: 18776
diff changeset
  1433
     * 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
  1434
     * #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
  1435
     * 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
  1436
     * {@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
  1437
     * {@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
  1438
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     *
49528
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1442
     * @return  An enumeration of {@link java.net.URL URL} objects for the
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1443
     *          resource. If no resources could be found, the enumeration will
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1444
     *          be empty. Resources for which a {@code URL} cannot be
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1445
     *          constructed, are in a package that is not opened
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1446
     *          unconditionally, or access to the resource is denied by the
c1eb35eb5f38 8200125: Fix some classloader/module typos
martin
parents: 49511
diff changeset
  1447
     *          security manager, are not returned in the enumeration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *          If I/O errors occur
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1451
     * @throws  NullPointerException If {@code name} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1454
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1455
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    public Enumeration<URL> getResources(String name) throws IOException {
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1458
        Objects.requireNonNull(name);
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11117
diff changeset
  1459
        @SuppressWarnings("unchecked")
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 11117
diff changeset
  1460
        Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            tmp[0] = parent.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        } else {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1464
            tmp[0] = BootLoader.findResources(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        tmp[1] = findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  1468
        return new CompoundEnumeration<>(tmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    /**
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1472
     * Returns a stream whose elements are the URLs of all the resources with
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1473
     * the given name. A resource is some data (images, audio, text, etc) that
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1474
     * can be accessed by class code in a way that is independent of the
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1475
     * location of the code.
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1476
     *
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1477
     * <p> The name of a resource is a {@code /}-separated path name that
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1478
     * identifies the resource.
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1479
     *
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1480
     * <p> The resources will be located when the returned stream is evaluated.
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1481
     * If the evaluation results in an {@code IOException} then the I/O
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1482
     * exception is wrapped in an {@link UncheckedIOException} that is then
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1483
     * thrown.
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1484
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1485
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1486
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1487
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1488
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1489
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1490
     * opened} unconditionally (even if the caller of this method is in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1491
     * same module as the resource). </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1492
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1493
     * @implSpec The default implementation invokes {@link #getResources(String)
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1494
     * getResources} to find all the resources with the given name and returns
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1495
     * a stream with the elements in the enumeration as the source.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1496
     *
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1497
     * @apiNote When overriding this method it is recommended that an
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1498
     * implementation ensures that any delegation is consistent with the {@link
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1499
     * #getResource(java.lang.String) getResource(String)} method. This should
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1500
     * ensure that the first element returned by the stream is the same
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1501
     * resource that the {@code getResource(String)} method would return.
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1502
     *
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1503
     * @param  name
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1504
     *         The resource name
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1505
     *
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1506
     * @return  A stream of resource {@link java.net.URL URL} objects. If no
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1507
     *          resources could  be found, the stream will be empty. Resources
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1508
     *          for which a {@code URL} cannot be constructed, are in a package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1509
     *          that is not opened unconditionally, or access to the resource
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1510
     *          is denied by the security manager, will not be in the stream.
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1511
     *
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1512
     * @throws  NullPointerException If {@code name} is {@code null}
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1513
     *
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1514
     * @since  9
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1515
     */
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1516
    public Stream<URL> resources(String name) {
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1517
        Objects.requireNonNull(name);
40807
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1518
        int characteristics = Spliterator.NONNULL | Spliterator.IMMUTABLE;
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1519
        Supplier<Spliterator<URL>> si = () -> {
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1520
            try {
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1521
                return Spliterators.spliteratorUnknownSize(
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1522
                    getResources(name).asIterator(), characteristics);
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1523
            } catch (IOException e) {
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1524
                throw new UncheckedIOException(e);
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1525
            }
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1526
        };
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1527
        return StreamSupport.stream(si, characteristics, false);
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1528
    }
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1529
aa6a4a3f9874 8161230: ClassLoader: add resource methods returning java.util.stream.Stream
psandoz
parents: 40804
diff changeset
  1530
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * Finds the resource with the given name. Class loader implementations
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1532
     * should override this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1534
     * <p> For resources in named modules then the method must implement the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1535
     * rules for encapsulation specified in the {@code Module} {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1536
     * Module#getResourceAsStream getResourceAsStream} method. Additionally,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1537
     * it must not find non-"{@code .class}" resources in packages of named
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1538
     * modules unless the package is {@link Module#isOpen(String) opened}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1539
     * unconditionally. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1540
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1541
     * @implSpec The default implementation returns {@code null}.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1542
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1546
     * @return  {@code URL} object for reading the resource; {@code null} if
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1547
     *          the resource could not be found, a {@code URL} could not be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1548
     *          constructed to locate the resource, the resource is in a package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1549
     *          that is not opened unconditionally, or access to the resource is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1550
     *          denied by the security manager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1553
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1554
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    protected URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    /**
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1561
     * Returns an enumeration of {@link java.net.URL URL} objects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * representing all the resources with the given name. Class loader
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1563
     * implementations should override this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1565
     * <p> For resources in named modules then the method must implement the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1566
     * rules for encapsulation specified in the {@code Module} {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1567
     * Module#getResourceAsStream getResourceAsStream} method. Additionally,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1568
     * it must not find non-"{@code .class}" resources in packages of named
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1569
     * modules unless the package is {@link Module#isOpen(String) opened}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1570
     * unconditionally. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1571
     *
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1572
     * @implSpec The default implementation returns an enumeration that
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1573
     * contains no elements.
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45527
diff changeset
  1574
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1578
     * @return  An enumeration of {@link java.net.URL URL} objects for
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1579
     *          the resource. If no resources could  be found, the enumeration
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1580
     *          will be empty. Resources for which a {@code URL} cannot be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1581
     *          constructed, are in a package that is not opened unconditionally,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1582
     *          or access to the resource is denied by the security manager,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1583
     *          are not returned in the enumeration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1589
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1590
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    protected Enumeration<URL> findResources(String name) throws IOException {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1593
        return Collections.emptyEnumeration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1596
    /**
42224
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
  1597
     * Registers the caller as
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
  1598
     * {@linkplain #isRegisteredAsParallelCapable() parallel capable}.
6890
43cb79a6b123 6887853: javadoc for java.lang.Classloader should be more clear
valeriep
parents: 6519
diff changeset
  1599
     * 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
  1600
     * conditions are met:
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1601
     * <ol>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1602
     * <li> no instance of the caller has been created</li>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1603
     * <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
  1604
     * registered as parallel capable</li>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1605
     * </ol>
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1606
     * <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
  1607
     * 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
  1608
     *
41753
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1609
     * @return  {@code true} if the caller is successfully registered as
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1610
     *          parallel capable and {@code false} if otherwise.
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1611
     *
42224
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
  1612
     * @see #isRegisteredAsParallelCapable()
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1613
     *
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1614
     * @since   1.7
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1615
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1616
    @CallerSensitive
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1617
    protected static boolean registerAsParallelCapable() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1618
        Class<? extends ClassLoader> callerClass =
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1619
            Reflection.getCallerClass().asSubclass(ClassLoader.class);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1620
        return ParallelLoaders.register(callerClass);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1621
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  1622
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    /**
42224
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
  1624
     * Returns {@code true} if this class loader is registered as
41753
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1625
     * {@linkplain #registerAsParallelCapable parallel capable}, otherwise
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1626
     * {@code false}.
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1627
     *
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1628
     * @return  {@code true} if this class loader is parallel capable,
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1629
     *          otherwise {@code false}.
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1630
     *
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1631
     * @see #registerAsParallelCapable()
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1632
     *
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1633
     * @since   9
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1634
     */
42224
62f59542b9d8 8169435: ClassLoader.isParallelCapable is final and conflicting method may get VerifyError
bchristi
parents: 41911
diff changeset
  1635
    public final boolean isRegisteredAsParallelCapable() {
41753
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1636
        return ParallelLoaders.isRegistered(this.getClass());
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1637
    }
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1638
ffa31454ab0f 8165793: Provide an API to query if a ClassLoader is parallel capable
bchristi
parents: 41375
diff changeset
  1639
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * Find a resource of the specified name from the search path used to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * classes.  This method locates the resource through the system class
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  1642
     * loader (see {@link #getSystemClassLoader()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1644
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1645
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1646
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1647
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1648
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1649
     * opened} unconditionally. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1650
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1654
     * @return  A {@link java.net.URL URL} to the resource; {@code
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1655
     *          null} if the resource could not be found, a URL could not be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1656
     *          constructed to locate the resource, the resource is in a package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1657
     *          that is not opened unconditionally or access to the resource is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1658
     *          denied by the security manager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @since  1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1661
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1662
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    public static URL getSystemResource(String name) {
40796
91ffd8a6f473 8165563: ClassLoader::getSystemClassLoader will never be null
mchung
parents: 40536
diff changeset
  1665
        return getSystemClassLoader().getResource(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * Finds all resources of the specified name from the search path used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * load classes.  The resources thus found are returned as an
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1671
     * {@link java.util.Enumeration Enumeration} of {@link
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1672
     * java.net.URL URL} objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * #getSystemResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1677
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1678
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1679
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1680
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1681
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1682
     * opened} unconditionally. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1683
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1687
     * @return  An enumeration of {@link java.net.URL URL} objects for
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1688
     *          the resource. If no resources could  be found, the enumeration
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1689
     *          will be empty. Resources for which a {@code URL} cannot be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1690
     *          constructed, are in a package that is not opened unconditionally,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1691
     *          or access to the resource is denied by the security manager,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1692
     *          are not returned in the enumeration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *          If I/O errors occur
40796
91ffd8a6f473 8165563: ClassLoader::getSystemClassLoader will never be null
mchung
parents: 40536
diff changeset
  1696
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1698
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1699
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    public static Enumeration<URL> getSystemResources(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    {
40796
91ffd8a6f473 8165563: ClassLoader::getSystemClassLoader will never be null
mchung
parents: 40536
diff changeset
  1704
        return getSystemClassLoader().getResources(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1713
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1714
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1715
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1716
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1717
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1718
     * opened} unconditionally. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1719
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1723
     * @return  An input stream for reading the resource; {@code null} if the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1724
     *          resource could not be found, the resource is in a package that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1725
     *          is not opened unconditionally, or access to the resource is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1726
     *          denied by the security manager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     *
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1728
     * @throws  NullPointerException If {@code name} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * @since  1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1731
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1732
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
    public InputStream getResourceAsStream(String name) {
42227
074dfec7f994 8136831: Undefined null behavior in ClassLoader.getResourceXXXX()
bchristi
parents: 42224
diff changeset
  1735
        Objects.requireNonNull(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        URL url = getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * Open for reading, a resource of the specified name from the search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * 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
  1747
     * system class loader (see {@link #getSystemClassLoader()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1749
     * <p> Resources in named modules are subject to the encapsulation rules
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1750
     * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1751
     * Additionally, and except for the special case where the resource has a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1752
     * name ending with "{@code .class}", this method will only find resources in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1753
     * packages of named modules when the package is {@link Module#isOpen(String)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1754
     * opened} unconditionally. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1755
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1759
     * @return  An input stream for reading the resource; {@code null} if the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1760
     *          resource could not be found, the resource is in a package that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1761
     *          is not opened unconditionally, or access to the resource is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  1762
     *          denied by the security manager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * @since  1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1765
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1766
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    public static InputStream getSystemResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        URL url = getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    // -- Hierarchy --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * Returns the parent class loader for delegation. Some implementations may
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1782
     * use {@code null} to represent the bootstrap class loader. This method
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1783
     * will return {@code null} in such implementations if this class loader's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * parent is the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     *
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1786
     * @return  The parent {@code ClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * @throws  SecurityException
40536
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1789
     *          If a security manager is present, and the caller's class loader
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1790
     *          is not {@code null} and is not an ancestor of this class loader,
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1791
     *          and the caller does not have the
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1792
     *          {@link RuntimePermission}{@code ("getClassLoader")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1796
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    public final ClassLoader getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        if (sm != null) {
28544
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1802
            // Check access to the parent class loader
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1803
            // If the caller's class loader is same as this class loader,
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1804
            // permission check is performed.
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  1805
            checkClassLoaderPermission(parent, Reflection.getCallerClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1811
     * Returns the unnamed {@code Module} for this class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1812
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1813
     * @return The unnamed Module for this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1814
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1815
     * @see Module#isNamed()
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1816
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1817
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1818
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1819
    public final Module getUnnamedModule() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1820
        return unnamedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1821
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1822
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1823
    /**
47951
a1f88c937a77 8191173: (cl) Clarify or remove "for delegation" in ClassLoader spec
bchristi
parents: 47722
diff changeset
  1824
     * Returns the platform class loader.  All
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1825
     * <a href="#builtinLoaders">platform classes</a> are visible to
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1826
     * the platform class loader.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1827
     *
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
  1828
     * @implNote The name of the builtin platform class loader is
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
  1829
     * {@code "platform"}.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
  1830
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1831
     * @return  The platform {@code ClassLoader}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1832
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1833
     * @throws  SecurityException
40536
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1834
     *          If a security manager is present, and the caller's class loader is
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1835
     *          not {@code null}, and the caller's class loader is not the same
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1836
     *          as or an ancestor of the platform class loader,
40536
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1837
     *          and the caller does not have the
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1838
     *          {@link RuntimePermission}{@code ("getClassLoader")}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1839
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1840
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1841
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1842
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1843
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1844
    public static ClassLoader getPlatformClassLoader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1845
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1846
        ClassLoader loader = getBuiltinPlatformClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1847
        if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1848
            checkClassLoaderPermission(loader, Reflection.getCallerClass());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1849
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1850
        return loader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1851
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1852
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1853
    /**
47951
a1f88c937a77 8191173: (cl) Clarify or remove "for delegation" in ClassLoader spec
bchristi
parents: 47722
diff changeset
  1854
     * Returns the system class loader.  This is the default
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1855
     * delegation parent for new {@code ClassLoader} instances, and is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * typically the class loader used to start the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * <p> This method is first invoked early in the runtime's startup
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1859
     * sequence, at which point it creates the system class loader. This
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1860
     * class loader will be the context class loader for the main application
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1861
     * thread (for example, the thread that invokes the {@code main} method of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1862
     * the main class).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * <p> The default system class loader is an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     *
53120
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1867
     * <p> If the system property "{@systemProperty java.system.class.loader}"
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1868
     * is defined when this method is first invoked then the value of that
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1869
     * property is taken to be the name of a class that will be returned as the
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1870
     * system class loader. The class is loaded using the default system class
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1871
     * loader and must define a public constructor that takes a single parameter
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 53018
diff changeset
  1872
     * of type {@code ClassLoader} which is used as the delegation parent. An
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * instance is then created using this constructor with the default system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * class loader as the parameter.  The resulting class loader is defined
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1875
     * to be the system class loader. During construction, the class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1876
     * should take great care to avoid calling {@code getSystemClassLoader()}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1877
     * If circular initialization of the system class loader is detected then
48066
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1878
     * an {@code IllegalStateException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1880
     * @implNote The system property to override the system class loader is not
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1881
     * examined until the VM is almost fully initialized. Code that executes
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1882
     * this method during startup should take care not to cache the return
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1883
     * value until the system is fully initialized.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1884
     *
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 41814
diff changeset
  1885
     * <p> The name of the built-in system class loader is {@code "app"}.
48671
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1886
     * The system property "{@code java.class.path}" is read during early
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1887
     * initialization of the VM to determine the class path.
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1888
     * An empty value of "{@code java.class.path}" property is interpreted
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1889
     * differently depending on whether the initial module (the module
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1890
     * containing the main class) is named or unnamed:
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1891
     * If named, the built-in system class loader will have no class path and
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1892
     * will search for classes and resources using the application module path;
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1893
     * otherwise, if unnamed, it will set the class path to the current
a47ee8b3d308 8191170: Clarify if java.class.path can be undefined
mchung
parents: 48205
diff changeset
  1894
     * working directory.
41814
a0333150713e 8168205: Should not default class path to CWD if -cp is not specified but -m is specified
mchung
parents: 41753
diff changeset
  1895
     *
47951
a1f88c937a77 8191173: (cl) Clarify or remove "for delegation" in ClassLoader spec
bchristi
parents: 47722
diff changeset
  1896
     * @return  The system {@code ClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * @throws  SecurityException
40536
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1899
     *          If a security manager is present, and the caller's class loader
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1900
     *          is not {@code null} and is not the same as or an ancestor of the
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1901
     *          system class loader, and the caller does not have the
8ea134098b80 7180225: SecurityExceptions not defined in some class loader methods
bchristi
parents: 37779
diff changeset
  1902
     *          {@link RuntimePermission}{@code ("getClassLoader")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     *          If invoked recursively during the construction of the class
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1906
     *          loader specified by the "{@code java.system.class.loader}"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *          property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * @throws  Error
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  1910
     *          If the system property "{@code java.system.class.loader}"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     *          is defined but the named class could not be loaded, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     *          provider class does not define the required constructor, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *          exception is thrown by that constructor when it is invoked. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     *          underlying cause of the error can be retrieved via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *          {@link Throwable#getCause()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @revised  1.4
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1918
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  1919
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  1921
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    public static ClassLoader getSystemClassLoader() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1923
        switch (VM.initLevel()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1924
            case 0:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1925
            case 1:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1926
            case 2:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1927
                // the system class loader is the built-in app class loader during startup
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1928
                return getBuiltinAppClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1929
            case 3:
48066
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1930
                String msg = "getSystemClassLoader cannot be called during the system class loader instantiation";
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1931
                throw new IllegalStateException(msg);
48984
b5d1fb0701d4 8198653: ClassLoader::getSystemClassLoader throws InternalError when called after shutdown
mchung
parents: 48671
diff changeset
  1932
            default:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1933
                // system fully initialized
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1934
                assert VM.isBooted() && scl != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1935
                SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1936
                if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1937
                    checkClassLoaderPermission(scl, Reflection.getCallerClass());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1938
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1939
                return scl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1941
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1942
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1943
    static ClassLoader getBuiltinPlatformClassLoader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1944
        return ClassLoaders.platformClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1945
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1946
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1947
    static ClassLoader getBuiltinAppClassLoader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1948
        return ClassLoaders.appClassLoader();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1951
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1952
     * Initialize the system class loader that may be a custom class on the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1953
     * application class path or application module path.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1954
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1955
     * @see java.lang.System#initPhase3
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1956
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1957
    static synchronized ClassLoader initSystemClassLoader() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1958
        if (VM.initLevel() != 3) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1959
            throw new InternalError("system class loader cannot be set at initLevel " +
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1960
                                    VM.initLevel());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1961
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1962
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1963
        // detect recursive initialization
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1964
        if (scl != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1965
            throw new IllegalStateException("recursive invocation");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1966
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1967
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1968
        ClassLoader builtinLoader = getBuiltinAppClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1969
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1970
        // All are privileged frames.  No need to call doPrivileged.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1971
        String cn = System.getProperty("java.system.class.loader");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1972
        if (cn != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1973
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1974
                // custom class loader is only supported to be loaded from unnamed module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1975
                Constructor<?> ctor = Class.forName(cn, false, builtinLoader)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1976
                                           .getDeclaredConstructor(ClassLoader.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1977
                scl = (ClassLoader) ctor.newInstance(builtinLoader);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1978
            } catch (Exception e) {
48066
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1979
                Throwable cause = e;
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1980
                if (e instanceof InvocationTargetException) {
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1981
                    cause = e.getCause();
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1982
                    if (cause instanceof Error) {
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1983
                        throw (Error) cause;
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1984
                    }
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1985
                }
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1986
                if (cause instanceof RuntimeException) {
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1987
                    throw (RuntimeException) cause;
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1988
                }
df95bd1fd4b1 8187222: ClassLoader.getSystemClassLoader not clear if recursive initialization leads to ISE or unspecified error
bchristi
parents: 47951
diff changeset
  1989
                throw new Error(cause.getMessage(), cause);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1991
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1992
            scl = builtinLoader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  1994
        return scl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    // Returns true if the specified class loader can be found in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    // loader's delegation chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    boolean isAncestor(ClassLoader cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        ClassLoader acl = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            acl = acl.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            if (cl == acl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        } while (acl != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2010
    // 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
  2011
    // 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
  2012
    // 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
  2013
    // 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
  2014
    // any class loader.
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2015
    private static boolean needsClassLoaderPermissionCheck(ClassLoader from,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2016
                                                           ClassLoader to)
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2017
    {
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2018
        if (from == to)
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2019
            return false;
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2020
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2021
        if (from == null)
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2022
            return false;
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2023
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2024
        return !to.isAncestor(from);
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2025
    }
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 12298
diff changeset
  2026
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2027
    // 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
  2028
    static ClassLoader getClassLoader(Class<?> caller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        // This can be null if the VM is requesting it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        if (caller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        // Circumvent security check since this is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        return caller.getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
28544
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  2037
    /*
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  2038
     * Checks RuntimePermission("getClassLoader") permission
a132e8e301e5 8055314: Update refactoring for new loader
mchung
parents: 28521
diff changeset
  2039
     * 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
  2040
     * 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
  2041
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2042
    static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2043
        SecurityManager sm = System.getSecurityManager();
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2044
        if (sm != null) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2045
            // 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
  2046
            ClassLoader ccl = getClassLoader(caller);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2047
            if (needsClassLoaderPermissionCheck(ccl, cl)) {
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2048
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2049
            }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2050
        }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2051
    }
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
  2052
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2053
    // The system class loader
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2054
    // @GuardedBy("ClassLoader.class")
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2055
    private static volatile ClassLoader scl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    // -- Package --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2060
     * Define a Package of the given Class object.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2061
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2062
     * If the given class represents an array type, a primitive type or void,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2063
     * this method returns {@code null}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2064
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2065
     * This method does not throw IllegalArgumentException.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2066
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2067
    Package definePackage(Class<?> c) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2068
        if (c.isPrimitive() || c.isArray()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2069
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2070
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2071
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2072
        return definePackage(c.getPackageName(), c.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2073
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2074
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2075
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2076
     * Defines a Package of the given name and module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2077
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2078
     * This method does not throw IllegalArgumentException.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2079
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2080
     * @param name package name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2081
     * @param m    module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2082
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2083
    Package definePackage(String name, Module m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2084
        if (name.isEmpty() && m.isNamed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2085
            throw new InternalError("unnamed package in  " + m);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2086
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2087
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2088
        // check if Package object is already defined
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2089
        NamedPackage pkg = packages.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2090
        if (pkg instanceof Package)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2091
            return (Package)pkg;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2092
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2093
        return (Package)packages.compute(name, (n, p) -> toPackage(n, p, m));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2094
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2095
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2096
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2097
     * Returns a Package object for the named package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2098
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2099
    private Package toPackage(String name, NamedPackage p, Module m) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2100
        // define Package object if the named package is not yet defined
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2101
        if (p == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2102
            return NamedPackage.toPackage(name, m);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2103
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2104
        // otherwise, replace the NamedPackage object with Package object
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2105
        if (p instanceof Package)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2106
            return (Package)p;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2107
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2108
        return NamedPackage.toPackage(p.packageName(), p.module());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2109
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2110
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2111
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2112
     * Defines a package by <a href="#binary-name">name</a> in this {@code ClassLoader}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2113
     * <p>
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2114
     * <a href="#binary-name">Package names</a> must be unique within a class loader and
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2115
     * cannot be redefined or changed once created.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2116
     * <p>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2117
     * If a class loader wishes to define a package with specific properties,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2118
     * such as version information, then the class loader should call this
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2119
     * {@code definePackage} method before calling {@code defineClass}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2120
     * Otherwise, the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2121
     * {@link #defineClass(String, byte[], int, int, ProtectionDomain) defineClass}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2122
     * method will define a package in this class loader corresponding to the package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2123
     * of the newly defined class; the properties of this defined package are
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2124
     * specified by {@link Package}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2125
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2126
     * @apiNote
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2127
     * A class loader that wishes to define a package for classes in a JAR
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2128
     * typically uses the specification and implementation titles, versions, and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2129
     * vendors from the JAR's manifest. If the package is specified as
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2130
     * {@linkplain java.util.jar.Attributes.Name#SEALED sealed} in the JAR's manifest,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2131
     * the {@code URL} of the JAR file is typically used as the {@code sealBase}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2132
     * If classes of package {@code 'p'} defined by this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2133
     * are loaded from multiple JARs, the {@code Package} object may contain
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2134
     * different information depending on the first class of package {@code 'p'}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2135
     * defined and which JAR's manifest is read first to explicitly define
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2136
     * package {@code 'p'}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2137
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2138
     * <p> It is strongly recommended that a class loader does not call this
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2139
     * method to explicitly define packages in <em>named modules</em>; instead,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2140
     * the package will be automatically defined when a class is {@linkplain
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2141
     * #defineClass(String, byte[], int, int, ProtectionDomain) being defined}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2142
     * If it is desirable to define {@code Package} explicitly, it should ensure
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2143
     * that all packages in a named module are defined with the properties
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2144
     * specified by {@link Package}.  Otherwise, some {@code Package} objects
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2145
     * in a named module may be for example sealed with different seal base.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2148
     *         The <a href="#binary-name">package name</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * @param  specTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     *         The specification title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * @param  specVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     *         The specification version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * @param  specVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     *         The specification vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * @param  implTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     *         The implementation title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * @param  implVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     *         The implementation version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * @param  implVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     *         The implementation vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * @param  sealBase
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2169
     *         If not {@code null}, then this package is sealed with
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2170
     *         respect to the given code source {@link java.net.URL URL}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2171
     *         object.  Otherwise, the package is not sealed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2173
     * @return  The newly defined {@code Package} object
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2174
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2175
     * @throws  NullPointerException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2176
     *          if {@code name} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * @throws  IllegalArgumentException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2179
     *          if a package of the given {@code name} is already
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2180
     *          defined by this class loader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2182
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2184
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2185
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2186
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2187
     * @jvms 5.3 Run-time package
47444
b6da56767057 8183901: Fix broken links to "Package Sealing" in the JAR spec
bchristi
parents: 47282
diff changeset
  2188
     * @see <a href="{@docRoot}/../specs/jar/jar.html#package-sealing">
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2189
     *      The JAR File Specification: Package Sealing</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    protected Package definePackage(String name, String specTitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                                    String specVersion, String specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                                    String implTitle, String implVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                                    String implVendor, URL sealBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2196
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2197
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2198
        // definePackage is not final and may be overridden by custom class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2199
        Package p = new Package(name, specTitle, specVersion, specVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2200
                                implTitle, implVersion, implVendor,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2201
                                sealBase, this);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2202
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2203
        if (packages.putIfAbsent(name, p) != null)
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents: 27184
diff changeset
  2204
            throw new IllegalArgumentException(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2205
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2206
        return p;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2207
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2208
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2209
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2210
     * Returns a {@code Package} of the given <a href="#binary-name">name</a> that
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2211
     * has been defined by this class loader.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2212
     *
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2213
     * @param  name The <a href="#binary-name">package name</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2214
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2215
     * @return The {@code Package} of the given name that has been defined
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2216
     *         by this class loader, or {@code null} if not found
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2217
     *
40804
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2218
     * @throws  NullPointerException
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2219
     *          if {@code name} is {@code null}.
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2220
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2221
     * @jvms 5.3 Run-time package
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2222
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2223
     * @since  9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2224
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2225
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2226
    public final Package getDefinedPackage(String name) {
40804
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2227
        Objects.requireNonNull(name, "name cannot be null");
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2228
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2229
        NamedPackage p = packages.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2230
        if (p == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2231
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2232
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2233
        return definePackage(name, p.module());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    /**
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2237
     * Returns all of the {@code Package}s that have been defined by
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2238
     * this class loader.  The returned array has no duplicated {@code Package}s
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2239
     * of the same name.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2240
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2241
     * @apiNote This method returns an array rather than a {@code Set} or {@code Stream}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2242
     *          for consistency with the existing {@link #getPackages} method.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2243
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2244
     * @return The array of {@code Package} objects that have been defined by
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2245
     *         this class loader; or an zero length array if no package has been
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2246
     *         defined by this class loader.
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2247
     *
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2248
     * @jvms 5.3 Run-time package
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2249
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2250
     * @since  9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2251
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2252
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2253
    public final Package[] getDefinedPackages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2254
        return packages().toArray(Package[]::new);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2255
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2256
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2257
    /**
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2258
     * Finds a package by <a href="#binary-name">name</a> in this class loader and its ancestors.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2259
     * <p>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2260
     * If this class loader defines a {@code Package} of the given name,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2261
     * the {@code Package} is returned. Otherwise, the ancestors of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2262
     * this class loader are searched recursively (parent by parent)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2263
     * for a {@code Package} of the given name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     *
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2265
     * @apiNote The {@link #getPlatformClassLoader() platform class loader}
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2266
     * may delegate to the application class loader but the application class
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2267
     * loader is not its ancestor.  When invoked on the platform class loader,
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2268
     * this method  will not find packages defined to the application
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2269
     * class loader.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2270
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * @param  name
49511
ba93b3928e7c 8199947: Rename HTML element id in ClassLoader javadoc to avoid name conflict with private elements
martin
parents: 49067
diff changeset
  2272
     *         The <a href="#binary-name">package name</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2273
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2274
     * @return The {@code Package} of the given name that has been defined by
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2275
     *         this class loader or its ancestors, or {@code null} if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     *
40804
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2277
     * @throws  NullPointerException
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2278
     *          if {@code name} is {@code null}.
aa5e0497d497 8165346: j.l.ClassLoader.getDefinedPackage(String) throws NPE
mchung
parents: 40796
diff changeset
  2279
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2280
     * @deprecated
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2281
     * If multiple class loaders delegate to each other and define classes
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2282
     * with the same package name, and one such loader relies on the lookup
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2283
     * behavior of {@code getPackage} to return a {@code Package} from
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2284
     * a parent loader, then the properties exposed by the {@code Package}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2285
     * may not be as expected in the rest of the program.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2286
     * For example, the {@code Package} will only expose annotations from the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2287
     * {@code package-info.class} file defined by the parent loader, even if
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2288
     * annotations exist in a {@code package-info.class} file defined by
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2289
     * a child loader.  A more robust approach is to use the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2290
     * {@link ClassLoader#getDefinedPackage} method which returns
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2291
     * a {@code Package} for the specified class loader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2293
     * @see ClassLoader#getDefinedPackage(String)
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2294
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2296
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2297
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  2299
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    protected Package getPackage(String name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2301
        Package pkg = getDefinedPackage(name);
8789
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2302
        if (pkg == null) {
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2303
            if (parent != null) {
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2304
                pkg = parent.getPackage(name);
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2305
            } else {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2306
                pkg = BootLoader.getDefinedPackage(name);
8789
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2307
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        }
8789
23f273e43be9 7001933: Deadlock in java.lang.classloader.getPackage()
valeriep
parents: 8552
diff changeset
  2309
        return pkg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    /**
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2313
     * Returns all of the {@code Package}s that have been defined by
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2314
     * this class loader and its ancestors.  The returned array may contain
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2315
     * more than one {@code Package} object of the same package name, each
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2316
     * defined by a different class loader in the class loader hierarchy.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     *
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2318
     * @apiNote The {@link #getPlatformClassLoader() platform class loader}
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2319
     * may delegate to the application class loader. In other words,
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2320
     * packages in modules defined to the application class loader may be
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2321
     * visible to the platform class loader.  On the other hand,
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2322
     * the application class loader is not its ancestor and hence
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2323
     * when invoked on the platform class loader, this method will not
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2324
     * return any packages defined to the application class loader.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44546
diff changeset
  2325
     *
47282
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2326
     * @return  The array of {@code Package} objects that have been defined by
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2327
     *          this class loader and its ancestors
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2328
     *
65f19a0ce7e9 6373396: (cl spec) clarify spec of ClassLoader.getPackages() about run-time package
mchung
parents: 47216
diff changeset
  2329
     * @see ClassLoader#getDefinedPackages()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @since  1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2332
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43062
diff changeset
  2333
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
    protected Package[] getPackages() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2336
        Stream<Package> pkgs = packages();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2337
        ClassLoader ld = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2338
        while (ld != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2339
            pkgs = Stream.concat(ld.packages(), pkgs);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2340
            ld = ld.parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2342
        return Stream.concat(BootLoader.packages(), pkgs)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2343
                     .toArray(Package[]::new);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2347
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2348
    // package-private
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2349
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2350
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2351
     * Returns a stream of Packages defined in this class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2352
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2353
    Stream<Package> packages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2354
        return packages.values().stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2355
                       .map(p -> definePackage(p.packageName(), p.module()));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2356
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2357
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    // -- Native library access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * Returns the absolute path name of a native library.  The VM invokes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * method to locate the native libraries that belong to classes loaded with
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2363
     * this class loader. If this method returns {@code null}, the VM
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * searches the library along the path specified as the
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2365
     * "{@code java.library.path}" property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * @param  libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     *         The library name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     * @return  The absolute path of the native library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * @see  System#loadLibrary(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * @see  System#mapLibraryName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    protected String findLibrary(String libname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * The inner class NativeLibrary denotes a loaded native library instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * Every classloader contains a vector of loaded native libraries in the
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2384
     * private field {@code nativeLibraries}.  The native libraries loaded
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2385
     * into the system are entered into the {@code systemNativeLibraries}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * <p> Every native library requires a particular version of JNI. This is
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2389
     * denoted by the private {@code jniVersion} field.  This field is set by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * the VM when it loads the library, and used by the VM to pass the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * version of JNI to the native methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * @see      ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    static class NativeLibrary {
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2397
        // the class from which the library is loaded, also indicates
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2398
        // the loader this native library belongs.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2399
        final Class<?> fromClass;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2400
        // the canonicalized name of the native library.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2401
        // or static library name
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2402
        final String name;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2403
        // Indicates if the native library is linked into the VM
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2404
        final boolean isBuiltin;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2405
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        // opaque handle to native library, used in native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        // the version of JNI environment the native library requires.
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2409
        int jniVersion;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2410
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2411
        native boolean load0(String name, boolean isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2413
        native long findEntry(String name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2415
        NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            this.fromClass = fromClass;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  2418
            this.isBuiltin = isBuiltin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2421
        /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2422
         * Loads the native library and registers for cleanup when its
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2423
         * associated class loader is unloaded
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2424
         */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2425
        boolean load() {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2426
            if (handle != 0) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2427
                throw new InternalError("Native library " + name + " has been loaded");
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2428
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2429
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2430
            if (!load0(name, isBuiltin)) return false;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2431
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2432
            // register the class loader for cleanup when unloaded
52915
3addaaf7eaea 8215048: Some classloader typos
martin
parents: 52220
diff changeset
  2433
            // builtin class loaders are never unloaded
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2434
            ClassLoader loader = fromClass.getClassLoader();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2435
            if (loader != null &&
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2436
                loader != getBuiltinPlatformClassLoader() &&
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2437
                loader != getBuiltinAppClassLoader()) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2438
                CleanerFactory.cleaner().register(loader,
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2439
                        new Unloader(name, handle, isBuiltin));
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2440
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2441
            return true;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2442
        }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2443
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2444
        static boolean loadLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2445
            ClassLoader loader =
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2446
                fromClass == null ? null : fromClass.getClassLoader();
47613
af241e3e5a13 8188052: JNI FindClass needs to specify the class loading context used for library lifecycle hooks
mchung
parents: 47282
diff changeset
  2447
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2448
            synchronized (loadedLibraryNames) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2449
                Map<String, NativeLibrary> libs =
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2450
                    loader != null ? loader.nativeLibraries() : systemNativeLibraries();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2451
                if (libs.containsKey(name)) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2452
                    return true;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2453
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2454
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2455
                if (loadedLibraryNames.contains(name)) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2456
                    throw new UnsatisfiedLinkError("Native Library " + name +
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2457
                        " already loaded in another classloader");
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2458
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2459
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2460
                /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2461
                 * When a library is being loaded, JNI_OnLoad function can cause
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2462
                 * another loadLibrary invocation that should succeed.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2463
                 *
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2464
                 * We use a static stack to hold the list of libraries we are
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2465
                 * loading because this can happen only when called by the
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2466
                 * same thread because Runtime.load and Runtime.loadLibrary
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2467
                 * are synchronous.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2468
                 *
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2469
                 * If there is a pending load operation for the library, we
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2470
                 * immediately return success; otherwise, we raise
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2471
                 * UnsatisfiedLinkError.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2472
                 */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2473
                for (NativeLibrary lib : nativeLibraryContext) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2474
                    if (name.equals(lib.name)) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2475
                        if (loader == lib.fromClass.getClassLoader()) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2476
                            return true;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2477
                        } else {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2478
                            throw new UnsatisfiedLinkError("Native Library " +
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2479
                                name + " is being loaded in another classloader");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                    }
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2482
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2483
                NativeLibrary lib = new NativeLibrary(fromClass, name, isBuiltin);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2484
                // load the native library
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2485
                nativeLibraryContext.push(lib);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2486
                try {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2487
                    if (!lib.load()) return false;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2488
                } finally {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2489
                    nativeLibraryContext.pop();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2490
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2491
                // register the loaded native library
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2492
                loadedLibraryNames.add(name);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2493
                libs.put(name, lib);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2494
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2495
            return true;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2496
        }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2497
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2498
        // Invoked in the VM to determine the context class in JNI_OnLoad
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2499
        // and JNI_OnUnload
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2500
        static Class<?> getFromClass() {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2501
            return nativeLibraryContext.peek().fromClass;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2502
        }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2503
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2504
        // native libraries being loaded
48205
6cd25cd7df81 8193159: Reduce the number of classes loaded due to NativeLibrary
mchung
parents: 48066
diff changeset
  2505
        static Deque<NativeLibrary> nativeLibraryContext = new ArrayDeque<>(8);
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2506
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2507
        /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2508
         * The run() method will be invoked when this class loader becomes
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2509
         * phantom reachable to unload the native library.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2510
         */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2511
        static class Unloader implements Runnable {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2512
            // This represents the context when a native library is unloaded
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2513
            // and getFromClass() will return null,
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2514
            static final NativeLibrary UNLOADER =
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2515
                new NativeLibrary(null, "dummy", false);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2516
            final String name;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2517
            final long handle;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2518
            final boolean isBuiltin;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2519
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2520
            Unloader(String name, long handle, boolean isBuiltin) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2521
                if (handle == 0) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2522
                    throw new IllegalArgumentException(
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2523
                        "Invalid handle for native library " + name);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2524
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2525
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2526
                this.name = name;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2527
                this.handle = handle;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2528
                this.isBuiltin = isBuiltin;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2529
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2530
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2531
            @Override
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2532
            public void run() {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2533
                synchronized (loadedLibraryNames) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2534
                    /* remove the native library name */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2535
                    loadedLibraryNames.remove(name);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2536
                    nativeLibraryContext.push(UNLOADER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                    try {
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2538
                        unload(name, isBuiltin, handle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                    } finally {
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2540
                        nativeLibraryContext.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                    }
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2542
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        }
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2546
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2547
        // JNI FindClass expects the caller class if invoked from JNI_OnLoad
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2548
        // and JNI_OnUnload is NativeLibrary class
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2549
        static native void unload(String name, boolean isBuiltin, long handle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    // The paths searched for libraries
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2553
    private static String usr_paths[];
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2554
    private static String sys_paths[];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2556
    private static String[] initializePath(String propName) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2557
        String ldPath = System.getProperty(propName, "");
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2558
        int ldLen = ldPath.length();
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2559
        char ps = File.pathSeparatorChar;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2560
        int psCount = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2561
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2562
        if (ClassLoaderHelper.allowsQuotedPathElements &&
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2563
            ldPath.indexOf('\"') >= 0) {
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2564
            // 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
  2565
            // 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
  2566
            // 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
  2567
            char[] buf = new char[ldLen];
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2568
            int bufLen = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2569
            for (int i = 0; i < ldLen; ++i) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2570
                char ch = ldPath.charAt(i);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2571
                if (ch == '\"') {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2572
                    while (++i < ldLen &&
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2573
                        (ch = ldPath.charAt(i)) != '\"') {
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2574
                        buf[bufLen++] = ch;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2575
                    }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2576
                } else {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2577
                    if (ch == ps) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2578
                        psCount++;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2579
                        ch = '\"';
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2580
                    }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2581
                    buf[bufLen++] = ch;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2582
                }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2583
            }
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2584
            ldPath = new String(buf, 0, bufLen);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2585
            ldLen = bufLen;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2586
            ps = '\"';
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2587
        } else {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2588
            for (int i = ldPath.indexOf(ps); i >= 0;
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2589
                 i = ldPath.indexOf(ps, i + 1)) {
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2590
                psCount++;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2591
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2594
        String[] paths = new String[psCount + 1];
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2595
        int pathStart = 0;
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2596
        for (int j = 0; j < psCount; ++j) {
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2597
            int pathEnd = ldPath.indexOf(ps, pathStart);
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2598
            paths[j] = (pathStart < pathEnd) ?
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2599
                ldPath.substring(pathStart, pathEnd) : ".";
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2600
            pathStart = pathEnd + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        }
28521
b86e910f3310 8067951: System.loadLibrary cannot find library when path contains quoted entry
igerasim
parents: 27348
diff changeset
  2602
        paths[psCount] = (pathStart < ldLen) ?
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2603
            ldPath.substring(pathStart, ldLen) : ".";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        return paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    // 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
  2608
    static void loadLibrary(Class<?> fromClass, String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                            boolean isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            (fromClass == null) ? null : fromClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        if (sys_paths == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            usr_paths = initializePath("java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            sys_paths = initializePath("sun.boot.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        if (isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            if (loadLibrary0(fromClass, new File(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            throw new UnsatisfiedLinkError("Can't load library: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            String libfilename = loader.findLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            if (libfilename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                File libfile = new File(libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                if (!libfile.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                    throw new UnsatisfiedLinkError(
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2628
                        "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                throw new UnsatisfiedLinkError("Can't load " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  2636
        for (String sys_path : sys_paths) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  2637
            File libfile = new File(sys_path, System.mapLibraryName(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            }
12298
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2641
            libfile = ClassLoaderHelper.mapAlternativeName(libfile);
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2642
            if (libfile != null && loadLibrary0(fromClass, libfile)) {
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2643
                return;
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2644
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        if (loader != null) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  2647
            for (String usr_path : usr_paths) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22571
diff changeset
  2648
                File libfile = new File(usr_path, System.mapLibraryName(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                }
12298
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2652
                libfile = ClassLoaderHelper.mapAlternativeName(libfile);
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2653
                if (libfile != null && loadLibrary0(fromClass, libfile)) {
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2654
                    return;
6e0b65d86fde 7134701: [macosx] Support legacy native library names
michaelm
parents: 11275
diff changeset
  2655
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        // Oops, it failed
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2659
        throw new UnsatisfiedLinkError("no " + name +
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2660
            " in java.library.path: " + Arrays.toString(usr_paths));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2663
    private static native String findBuiltinLib(String name);
31084
0acb07ceb0bc 8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents: 29986
diff changeset
  2664
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9266
diff changeset
  2665
    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
  2666
        // 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
  2667
        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
  2668
        boolean isBuiltin = (name != null);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  2669
        if (!isBuiltin) {
25167
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2670
            name = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28544
diff changeset
  2671
                new PrivilegedAction<>() {
25167
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2672
                    public String run() {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2673
                        try {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2674
                            return file.exists() ? file.getCanonicalPath() : null;
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2675
                        } catch (IOException e) {
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2676
                            return null;
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2677
                        }
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2678
                    }
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2679
                });
6a8e55aba124 8047904: Runtime.loadLibrary throws SecurityException when security manager is installed
mchung
parents: 24685
diff changeset
  2680
            if (name == null) {
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  2681
                return false;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 16063
diff changeset
  2682
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        }
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2684
        return NativeLibrary.loadLibrary(fromClass, name, isBuiltin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2687
    /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2688
     * Invoked in the VM class linking code.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2689
     */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2690
    private static long findNative(ClassLoader loader, String entryName) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2691
        Map<String, NativeLibrary> libs =
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2692
            loader != null ? loader.nativeLibraries() : systemNativeLibraries();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2693
        if (libs.isEmpty())
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2694
            return 0;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2695
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2696
        // the native libraries map may be updated in another thread
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2697
        // when a native library is being loaded.  No symbol will be
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2698
        // searched from it yet.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2699
        for (NativeLibrary lib : libs.values()) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2700
            long entry = lib.findEntry(entryName);
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2701
            if (entry != 0) return entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2706
    // All native library names we've loaded.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2707
    // This also serves as the lock to obtain nativeLibraries
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2708
    // and write to nativeLibraryContext.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2709
    private static final Set<String> loadedLibraryNames = new HashSet<>();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2710
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2711
    // Native libraries belonging to system classes.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2712
    private static volatile Map<String, NativeLibrary> systemNativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2713
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2714
    // Native libraries associated with the class loader.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2715
    private volatile Map<String, NativeLibrary> nativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2716
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2717
    /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2718
     * Returns the native libraries map associated with bootstrap class loader
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2719
     * This method will create the map at the first time when called.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2720
     */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2721
    private static Map<String, NativeLibrary> systemNativeLibraries() {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2722
        Map<String, NativeLibrary> libs = systemNativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2723
        if (libs == null) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2724
            synchronized (loadedLibraryNames) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2725
                libs = systemNativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2726
                if (libs == null) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2727
                    libs = systemNativeLibraries = new ConcurrentHashMap<>();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2728
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2729
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2730
        }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2731
        return libs;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2732
    }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2733
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2734
    /*
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2735
     * Returns the native libraries map associated with this class loader
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2736
     * This method will create the map at the first time when called.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2737
     */
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2738
    private Map<String, NativeLibrary> nativeLibraries() {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2739
        Map<String, NativeLibrary> libs = nativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2740
        if (libs == null) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2741
            synchronized (loadedLibraryNames) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2742
                libs = nativeLibraries;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2743
                if (libs == null) {
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2744
                    libs = nativeLibraries = new ConcurrentHashMap<>();
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2745
                }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2746
            }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2747
        }
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2748
        return libs;
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47701
diff changeset
  2749
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
    // -- Assertion management --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2753
    final Object assertionLock;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2754
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
    // The default toggle for assertion checking.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2756
    // @GuardedBy("assertionLock")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
    private boolean defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
    // Maps String packageName to Boolean package default assertion status Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
    // that the default package is placed under a null map key.  If this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    // is null then we are delegating assertion status queries to the VM, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
    // none of this ClassLoader's assertion status modification methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    // been invoked.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2764
    // @GuardedBy("assertionLock")
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2765
    private Map<String, Boolean> packageAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
    // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
    // field is null then we are delegating assertion status queries to the VM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
    // i.e., none of this ClassLoader's assertion status modification methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
    // have been invoked.
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2771
    // @GuardedBy("assertionLock")
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2772
    Map<String, Boolean> classAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * Sets the default assertion status for this class loader.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     * determines whether classes loaded by this class loader and initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * in the future will have assertions enabled or disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * This setting may be overridden on a per-package or per-class basis by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
18776
c17100862d86 8019862: Fix doclint errors in java.lang.*.
bpb
parents: 18156
diff changeset
  2780
     * #setClassAssertionStatus(String, boolean)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * @param  enabled
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2783
     *         {@code true} if classes loaded by this class loader will
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2784
     *         henceforth have assertions enabled by default, {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     *         if they will have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2789
    public void setDefaultAssertionStatus(boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2790
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2791
            if (classAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2792
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2794
            defaultAssertionStatus = enabled;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2795
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
     * Sets the package default assertion status for the named package.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * package default assertion status determines the assertion status for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
     * classes initialized in the future that belong to the named package or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * any of its "subpackages".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * <p> A subpackage of a package named p is any package whose name begins
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2805
     * with "{@code p.}".  For example, {@code javax.swing.text} is a
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2806
     * subpackage of {@code javax.swing}, and both {@code java.util} and
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2807
     * {@code java.lang.reflect} are subpackages of {@code java}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * <p> In the event that multiple package defaults apply to a given class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * the package default pertaining to the most specific package takes
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2811
     * precedence over the others.  For example, if {@code javax.lang} and
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2812
     * {@code javax.lang.reflect} both have package defaults associated with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * them, the latter package default applies to classes in
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2814
     * {@code javax.lang.reflect}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     * <p> Package defaults take precedence over the class loader's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     * assertion status, and may be overridden on a per-class basis by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
     * {@link #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     * @param  packageName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     *         The name of the package whose package default assertion status
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2822
     *         is to be set. A {@code null} value indicates the unnamed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     *         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
  2824
     *         (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
  2825
     *         <cite>The Java&trade; Language Specification</cite>.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * @param  enabled
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2828
     *         {@code true} if classes loaded by this classloader and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     *         belonging to the named package or any of its subpackages will
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2830
     *         have assertions enabled by default, {@code false} if they will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
     *         have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2835
    public void setPackageAssertionStatus(String packageName,
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2836
                                          boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2837
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2838
            if (packageAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2839
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2841
            packageAssertionStatus.put(packageName, enabled);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2842
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * Sets the desired assertion status for the named top-level class in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * class loader and any nested classes contained therein.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * takes precedence over the class loader's default assertion status, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * over any applicable per-package default.  This method has no effect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     * the named class has already been initialized.  (Once a class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * initialized, its assertion status cannot change.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * <p> If the named class is not a top-level class, this invocation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * have no effect on the actual assertion status of any class. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     *         The fully qualified class name of the top-level class whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     *         assertion status is to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * @param  enabled
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2861
     *         {@code true} if the named class is to have assertions
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2862
     *         enabled when (and if) it is initialized, {@code false} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     *         class is to have assertions disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2867
    public void setClassAssertionStatus(String className, boolean enabled) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2868
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2869
            if (classAssertionStatus == null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2870
                initializeJavaAssertionMaps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2872
            classAssertionStatus.put(className, enabled);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2873
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * Sets the default assertion status for this class loader to
43062
b9593792dfd9 8172475: Remove <tt></tt> usage from Class and ClassLoader
darcy
parents: 42339
diff changeset
  2878
     * {@code false} and discards any package defaults or class assertion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * status settings associated with the class loader.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * provided so that class loaders can be made to ignore any command line or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * persistent assertion status settings and "start with a clean slate."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2885
    public void clearAssertionStatus() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
         * Whether or not "Java assertion maps" are initialized, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
         * them to empty maps, effectively ignoring any present settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
         */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2890
        synchronized (assertionLock) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2891
            classAssertionStatus = new HashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2892
            packageAssertionStatus = new HashMap<>();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2893
            defaultAssertionStatus = false;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2894
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * Returns the assertion status that would be assigned to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * If the named class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * otherwise, this class loader's default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     *         The fully qualified class name of the class whose desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     *         assertion status is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * @return  The desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     * @see  #setClassAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     * @see  #setPackageAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     * @see  #setDefaultAssertionStatus(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2919
    boolean desiredAssertionStatus(String className) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2920
        synchronized (assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2921
            // assert classAssertionStatus   != null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2922
            // assert packageAssertionStatus != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2924
            // Check for a class entry
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2925
            Boolean result = classAssertionStatus.get(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                return result.booleanValue();
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2928
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2929
            // Check for most specific package entry
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22581
diff changeset
  2930
            int dotIndex = className.lastIndexOf('.');
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2931
            if (dotIndex < 0) { // default package
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2932
                result = packageAssertionStatus.get(null);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2933
                if (result != null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2934
                    return result.booleanValue();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2935
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2936
            while(dotIndex > 0) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2937
                className = className.substring(0, dotIndex);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2938
                result = packageAssertionStatus.get(className);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2939
                if (result != null)
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2940
                    return result.booleanValue();
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22581
diff changeset
  2941
                dotIndex = className.lastIndexOf('.', dotIndex-1);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2942
            }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2943
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2944
            // Return the classloader default
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2945
            return defaultAssertionStatus;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    // 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
  2950
    // Note: Should only be called inside a synchronized block
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    private void initializeJavaAssertionMaps() {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
  2952
        // assert Thread.holdsLock(assertionLock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2954
        classAssertionStatus = new HashMap<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7029
diff changeset
  2955
        packageAssertionStatus = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        AssertionStatusDirectives directives = retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        for(int i = 0; i < directives.classes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            classAssertionStatus.put(directives.classes[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2960
                                     directives.classEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        for(int i = 0; i < directives.packages.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            packageAssertionStatus.put(directives.packages[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2964
                                       directives.packageEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        defaultAssertionStatus = directives.deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
    // Retrieves the assertion directives from the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
    private static native AssertionStatusDirectives retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41911
diff changeset
  2973
    // -- Misc --
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2974
36972
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2975
    /**
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2976
     * Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s)
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2977
     * associated with this ClassLoader, creating it if it doesn't already exist.
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2978
     */
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2979
    ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap() {
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2980
        ConcurrentHashMap<?, ?> map = classLoaderValueMap;
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2981
        if (map == null) {
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2982
            map = new ConcurrentHashMap<>();
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2983
            boolean set = trySetObjectField("classLoaderValueMap", map);
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2984
            if (!set) {
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2985
                // beaten by someone else
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2986
                map = classLoaderValueMap;
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2987
            }
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2988
        }
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2989
        return map;
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2990
    }
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2991
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2992
    // the storage for ClassLoaderValue(s) associated with this ClassLoader
27147cde3256 8152115: (proxy) Examine performance of dynamic proxy creation
plevart
parents: 36511
diff changeset
  2993
    private volatile ConcurrentHashMap<?, ?> classLoaderValueMap;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2994
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2995
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2996
     * Attempts to atomically set a volatile field in this object. Returns
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2997
     * {@code true} if not beaten by another thread. Avoids the use of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2998
     * AtomicReferenceFieldUpdater in this class.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  2999
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  3000
    private boolean trySetObjectField(String name, Object obj) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  3001
        Unsafe unsafe = Unsafe.getUnsafe();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  3002
        Class<?> k = ClassLoader.class;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 34953
diff changeset
  3003
        long offset;
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45563
diff changeset
  3004
        offset = unsafe.objectFieldOffset(k, name);
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 50634
diff changeset
  3005
        return unsafe.compareAndSetReference(this, offset, null, obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
}
34825
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3008
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3009
/*
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3010
 * A utility class that will enumerate over an array of enumerations.
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3011
 */
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3012
final class CompoundEnumeration<E> implements Enumeration<E> {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3013
    private final Enumeration<E>[] enums;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3014
    private int index;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3015
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3016
    public CompoundEnumeration(Enumeration<E>[] enums) {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3017
        this.enums = enums;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3018
    }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3019
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3020
    private boolean next() {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3021
        while (index < enums.length) {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3022
            if (enums[index] != null && enums[index].hasMoreElements()) {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3023
                return true;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3024
            }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3025
            index++;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3026
        }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3027
        return false;
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3028
    }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3029
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3030
    public boolean hasMoreElements() {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3031
        return next();
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3032
    }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3033
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3034
    public E nextElement() {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3035
        if (!next()) {
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3036
            throw new NoSuchElementException();
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3037
        }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3038
        return enums[index].nextElement();
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3039
    }
d315dac0f1a8 8146000: Remove sun.mics.CompoundEnumeration
chegar
parents: 32649
diff changeset
  3040
}