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