author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 42338 | a60f280f803c |
child 45004 | ea3137042a61 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36511 | 2 |
* Copyright (c) 2003, 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 |
*/ |
25 |
||
26 |
package sun.instrument; |
|
27 |
||
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
28 |
import java.lang.instrument.UnmodifiableModuleException; |
2 | 29 |
import java.lang.reflect.Method; |
30 |
import java.lang.reflect.AccessibleObject; |
|
31 |
import java.lang.instrument.ClassFileTransformer; |
|
32 |
import java.lang.instrument.ClassDefinition; |
|
33 |
import java.lang.instrument.Instrumentation; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.security.ProtectionDomain; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
37 |
import java.util.Collections; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
38 |
import java.util.ArrayList; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
39 |
import java.util.HashMap; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
40 |
import java.util.HashSet; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
41 |
import java.util.List; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
42 |
import java.util.Map; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
43 |
import java.util.Set; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
44 |
import java.util.jar.JarFile; |
2 | 45 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
46 |
import jdk.internal.module.Modules; |
2 | 47 |
|
48 |
/* |
|
49 |
* Copyright 2003 Wily Technology, Inc. |
|
50 |
*/ |
|
51 |
||
52 |
/** |
|
53 |
* The Java side of the JPLIS implementation. Works in concert with a native JVMTI agent |
|
54 |
* to implement the JPLIS API set. Provides both the Java API implementation of |
|
55 |
* the Instrumentation interface and utility Java routines to support the native code. |
|
56 |
* Keeps a pointer to the native data structure in a scalar field to allow native |
|
57 |
* processing behind native methods. |
|
58 |
*/ |
|
59 |
public class InstrumentationImpl implements Instrumentation { |
|
60 |
private final TransformerManager mTransformerManager; |
|
61 |
private TransformerManager mRetransfomableTransformerManager; |
|
62 |
// needs to store a native pointer, so use 64 bits |
|
63 |
private final long mNativeAgent; |
|
64 |
private final boolean mEnvironmentSupportsRedefineClasses; |
|
65 |
private volatile boolean mEnvironmentSupportsRetransformClassesKnown; |
|
66 |
private volatile boolean mEnvironmentSupportsRetransformClasses; |
|
67 |
private final boolean mEnvironmentSupportsNativeMethodPrefix; |
|
68 |
||
69 |
private |
|
70 |
InstrumentationImpl(long nativeAgent, |
|
71 |
boolean environmentSupportsRedefineClasses, |
|
72 |
boolean environmentSupportsNativeMethodPrefix) { |
|
73 |
mTransformerManager = new TransformerManager(false); |
|
74 |
mRetransfomableTransformerManager = null; |
|
75 |
mNativeAgent = nativeAgent; |
|
76 |
mEnvironmentSupportsRedefineClasses = environmentSupportsRedefineClasses; |
|
77 |
mEnvironmentSupportsRetransformClassesKnown = false; // false = need to ask |
|
78 |
mEnvironmentSupportsRetransformClasses = false; // don't know yet |
|
79 |
mEnvironmentSupportsNativeMethodPrefix = environmentSupportsNativeMethodPrefix; |
|
80 |
} |
|
81 |
||
82 |
public void |
|
83 |
addTransformer(ClassFileTransformer transformer) { |
|
84 |
addTransformer(transformer, false); |
|
85 |
} |
|
86 |
||
87 |
public synchronized void |
|
88 |
addTransformer(ClassFileTransformer transformer, boolean canRetransform) { |
|
89 |
if (transformer == null) { |
|
90 |
throw new NullPointerException("null passed as 'transformer' in addTransformer"); |
|
91 |
} |
|
92 |
if (canRetransform) { |
|
93 |
if (!isRetransformClassesSupported()) { |
|
94 |
throw new UnsupportedOperationException( |
|
95 |
"adding retransformable transformers is not supported in this environment"); |
|
96 |
} |
|
97 |
if (mRetransfomableTransformerManager == null) { |
|
98 |
mRetransfomableTransformerManager = new TransformerManager(true); |
|
99 |
} |
|
100 |
mRetransfomableTransformerManager.addTransformer(transformer); |
|
101 |
if (mRetransfomableTransformerManager.getTransformerCount() == 1) { |
|
102 |
setHasRetransformableTransformers(mNativeAgent, true); |
|
103 |
} |
|
104 |
} else { |
|
105 |
mTransformerManager.addTransformer(transformer); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
public synchronized boolean |
|
110 |
removeTransformer(ClassFileTransformer transformer) { |
|
111 |
if (transformer == null) { |
|
112 |
throw new NullPointerException("null passed as 'transformer' in removeTransformer"); |
|
113 |
} |
|
114 |
TransformerManager mgr = findTransformerManager(transformer); |
|
115 |
if (mgr != null) { |
|
116 |
mgr.removeTransformer(transformer); |
|
117 |
if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) { |
|
118 |
setHasRetransformableTransformers(mNativeAgent, false); |
|
119 |
} |
|
120 |
return true; |
|
121 |
} |
|
122 |
return false; |
|
123 |
} |
|
124 |
||
125 |
public boolean |
|
126 |
isModifiableClass(Class<?> theClass) { |
|
127 |
if (theClass == null) { |
|
128 |
throw new NullPointerException( |
|
129 |
"null passed as 'theClass' in isModifiableClass"); |
|
130 |
} |
|
131 |
return isModifiableClass0(mNativeAgent, theClass); |
|
132 |
} |
|
133 |
||
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
134 |
public boolean isModifiableModule(Module module) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
135 |
if (module == null) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
136 |
throw new NullPointerException("'module' is null"); |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
137 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
138 |
return true; |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
139 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
140 |
|
2 | 141 |
public boolean |
142 |
isRetransformClassesSupported() { |
|
143 |
// ask lazily since there is some overhead |
|
144 |
if (!mEnvironmentSupportsRetransformClassesKnown) { |
|
145 |
mEnvironmentSupportsRetransformClasses = isRetransformClassesSupported0(mNativeAgent); |
|
146 |
mEnvironmentSupportsRetransformClassesKnown = true; |
|
147 |
} |
|
148 |
return mEnvironmentSupportsRetransformClasses; |
|
149 |
} |
|
150 |
||
151 |
public void |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
152 |
retransformClasses(Class<?>... classes) { |
2 | 153 |
if (!isRetransformClassesSupported()) { |
154 |
throw new UnsupportedOperationException( |
|
155 |
"retransformClasses is not supported in this environment"); |
|
156 |
} |
|
157 |
retransformClasses0(mNativeAgent, classes); |
|
158 |
} |
|
159 |
||
160 |
public boolean |
|
161 |
isRedefineClassesSupported() { |
|
162 |
return mEnvironmentSupportsRedefineClasses; |
|
163 |
} |
|
164 |
||
165 |
public void |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
166 |
redefineClasses(ClassDefinition... definitions) |
2 | 167 |
throws ClassNotFoundException { |
168 |
if (!isRedefineClassesSupported()) { |
|
169 |
throw new UnsupportedOperationException("redefineClasses is not supported in this environment"); |
|
170 |
} |
|
171 |
if (definitions == null) { |
|
172 |
throw new NullPointerException("null passed as 'definitions' in redefineClasses"); |
|
173 |
} |
|
174 |
for (int i = 0; i < definitions.length; ++i) { |
|
175 |
if (definitions[i] == null) { |
|
176 |
throw new NullPointerException("element of 'definitions' is null in redefineClasses"); |
|
177 |
} |
|
178 |
} |
|
179 |
if (definitions.length == 0) { |
|
180 |
return; // short-circuit if there are no changes requested |
|
181 |
} |
|
182 |
||
183 |
redefineClasses0(mNativeAgent, definitions); |
|
184 |
} |
|
185 |
||
20754
3d7b2fafc34b
8025967: addition of -Werror broke the old build
valeriep
parents:
14342
diff
changeset
|
186 |
@SuppressWarnings("rawtypes") |
2 | 187 |
public Class[] |
188 |
getAllLoadedClasses() { |
|
189 |
return getAllLoadedClasses0(mNativeAgent); |
|
190 |
} |
|
191 |
||
20754
3d7b2fafc34b
8025967: addition of -Werror broke the old build
valeriep
parents:
14342
diff
changeset
|
192 |
@SuppressWarnings("rawtypes") |
2 | 193 |
public Class[] |
194 |
getInitiatedClasses(ClassLoader loader) { |
|
195 |
return getInitiatedClasses0(mNativeAgent, loader); |
|
196 |
} |
|
197 |
||
198 |
public long |
|
199 |
getObjectSize(Object objectToSize) { |
|
200 |
if (objectToSize == null) { |
|
201 |
throw new NullPointerException("null passed as 'objectToSize' in getObjectSize"); |
|
202 |
} |
|
203 |
return getObjectSize0(mNativeAgent, objectToSize); |
|
204 |
} |
|
205 |
||
206 |
public void |
|
207 |
appendToBootstrapClassLoaderSearch(JarFile jarfile) { |
|
208 |
appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), true); |
|
209 |
} |
|
210 |
||
211 |
public void |
|
212 |
appendToSystemClassLoaderSearch(JarFile jarfile) { |
|
213 |
appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), false); |
|
214 |
} |
|
215 |
||
216 |
public boolean |
|
217 |
isNativeMethodPrefixSupported() { |
|
218 |
return mEnvironmentSupportsNativeMethodPrefix; |
|
219 |
} |
|
220 |
||
221 |
public synchronized void |
|
222 |
setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { |
|
223 |
if (!isNativeMethodPrefixSupported()) { |
|
224 |
throw new UnsupportedOperationException( |
|
225 |
"setNativeMethodPrefix is not supported in this environment"); |
|
226 |
} |
|
227 |
if (transformer == null) { |
|
228 |
throw new NullPointerException( |
|
229 |
"null passed as 'transformer' in setNativeMethodPrefix"); |
|
230 |
} |
|
231 |
TransformerManager mgr = findTransformerManager(transformer); |
|
232 |
if (mgr == null) { |
|
233 |
throw new IllegalArgumentException( |
|
234 |
"transformer not registered in setNativeMethodPrefix"); |
|
235 |
} |
|
236 |
mgr.setNativeMethodPrefix(transformer, prefix); |
|
237 |
String[] prefixes = mgr.getNativeMethodPrefixes(); |
|
238 |
setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable()); |
|
239 |
} |
|
240 |
||
36511 | 241 |
@Override |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
242 |
public void redefineModule(Module module, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
243 |
Set<Module> extraReads, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
244 |
Map<String, Set<Module>> extraExports, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
245 |
Map<String, Set<Module>> extraOpens, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
246 |
Set<Class<?>> extraUses, |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
247 |
Map<Class<?>, List<Class<?>>> extraProvides) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
248 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
249 |
if (!module.isNamed()) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
250 |
return; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
251 |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
252 |
if (!isModifiableModule(module)) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
253 |
throw new UnmodifiableModuleException(module.getName()); |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
254 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
255 |
// copy and check reads |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
256 |
extraReads = new HashSet<>(extraReads); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
257 |
if (extraReads.contains(null)) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
258 |
throw new NullPointerException("'extraReads' contains null"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
259 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
260 |
// copy and check exports and opens |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
261 |
extraExports = cloneAndCheckMap(module, extraExports); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
262 |
extraOpens = cloneAndCheckMap(module, extraOpens); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
263 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
264 |
// copy and check uses |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
265 |
extraUses = new HashSet<>(extraUses); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
266 |
if (extraUses.contains(null)) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
267 |
throw new NullPointerException("'extraUses' contains null"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
268 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
269 |
// copy and check provides |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
270 |
Map<Class<?>, List<Class<?>>> tmpProvides = new HashMap<>(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
271 |
for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
272 |
Class<?> service = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
273 |
if (service == null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
274 |
throw new NullPointerException("'extraProvides' contains null"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
275 |
List<Class<?>> providers = new ArrayList<>(e.getValue()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
276 |
if (providers.isEmpty()) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
277 |
throw new IllegalArgumentException("list of providers is empty"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
278 |
providers.forEach(p -> { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
279 |
if (p.getModule() != module) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
280 |
throw new IllegalArgumentException(p + " not in " + module); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
281 |
if (!service.isAssignableFrom(p)) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
282 |
throw new IllegalArgumentException(p + " is not a " + service); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
283 |
}); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
284 |
tmpProvides.put(service, providers); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
285 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
286 |
extraProvides = tmpProvides; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
287 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
288 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
289 |
// update reads |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
290 |
extraReads.forEach(m -> Modules.addReads(module, m)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
291 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
292 |
// update exports |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
293 |
for (Map.Entry<String, Set<Module>> e : extraExports.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
294 |
String pkg = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
295 |
Set<Module> targets = e.getValue(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
296 |
targets.forEach(m -> Modules.addExports(module, pkg, m)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
297 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
298 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
299 |
// update opens |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
300 |
for (Map.Entry<String, Set<Module>> e : extraOpens.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
301 |
String pkg = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
302 |
Set<Module> targets = e.getValue(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
303 |
targets.forEach(m -> Modules.addOpens(module, pkg, m)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
304 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
305 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
306 |
// update uses |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
307 |
extraUses.forEach(service -> Modules.addUses(module, service)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
308 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
309 |
// update provides |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
310 |
for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
311 |
Class<?> service = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
312 |
List<Class<?>> providers = e.getValue(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
313 |
providers.forEach(p -> Modules.addProvides(module, service, p)); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
314 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
315 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
316 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
317 |
private Map<String, Set<Module>> |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
318 |
cloneAndCheckMap(Module module, Map<String, Set<Module>> map) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
319 |
{ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
320 |
if (map.isEmpty()) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
321 |
return Collections.emptyMap(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
322 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
323 |
Map<String, Set<Module>> result = new HashMap<>(); |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
324 |
Set<String> packages = module.getPackages(); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
325 |
for (Map.Entry<String, Set<Module>> e : map.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
326 |
String pkg = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
327 |
if (pkg == null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
328 |
throw new NullPointerException("package cannot be null"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
329 |
if (!packages.contains(pkg)) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
330 |
throw new IllegalArgumentException(pkg + " not in module"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
331 |
Set<Module> targets = new HashSet<>(e.getValue()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
332 |
if (targets.isEmpty()) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
333 |
throw new IllegalArgumentException("set of targets is empty"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
334 |
if (targets.contains(null)) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
335 |
throw new NullPointerException("set of targets cannot include null"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
336 |
result.put(pkg, targets); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
337 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39739
diff
changeset
|
338 |
return result; |
36511 | 339 |
} |
340 |
||
341 |
||
2 | 342 |
private TransformerManager |
343 |
findTransformerManager(ClassFileTransformer transformer) { |
|
344 |
if (mTransformerManager.includesTransformer(transformer)) { |
|
345 |
return mTransformerManager; |
|
346 |
} |
|
347 |
if (mRetransfomableTransformerManager != null && |
|
348 |
mRetransfomableTransformerManager.includesTransformer(transformer)) { |
|
349 |
return mRetransfomableTransformerManager; |
|
350 |
} |
|
351 |
return null; |
|
352 |
} |
|
353 |
||
354 |
||
355 |
/* |
|
356 |
* Natives |
|
357 |
*/ |
|
358 |
private native boolean |
|
359 |
isModifiableClass0(long nativeAgent, Class<?> theClass); |
|
360 |
||
361 |
private native boolean |
|
362 |
isRetransformClassesSupported0(long nativeAgent); |
|
363 |
||
364 |
private native void |
|
365 |
setHasRetransformableTransformers(long nativeAgent, boolean has); |
|
366 |
||
367 |
private native void |
|
368 |
retransformClasses0(long nativeAgent, Class<?>[] classes); |
|
369 |
||
370 |
private native void |
|
371 |
redefineClasses0(long nativeAgent, ClassDefinition[] definitions) |
|
372 |
throws ClassNotFoundException; |
|
373 |
||
20754
3d7b2fafc34b
8025967: addition of -Werror broke the old build
valeriep
parents:
14342
diff
changeset
|
374 |
@SuppressWarnings("rawtypes") |
2 | 375 |
private native Class[] |
376 |
getAllLoadedClasses0(long nativeAgent); |
|
377 |
||
20754
3d7b2fafc34b
8025967: addition of -Werror broke the old build
valeriep
parents:
14342
diff
changeset
|
378 |
@SuppressWarnings("rawtypes") |
2 | 379 |
private native Class[] |
380 |
getInitiatedClasses0(long nativeAgent, ClassLoader loader); |
|
381 |
||
382 |
private native long |
|
383 |
getObjectSize0(long nativeAgent, Object objectToSize); |
|
384 |
||
385 |
private native void |
|
386 |
appendToClassLoaderSearch0(long nativeAgent, String jarfile, boolean bootLoader); |
|
387 |
||
388 |
private native void |
|
389 |
setNativeMethodPrefixes(long nativeAgent, String[] prefixes, boolean isRetransformable); |
|
390 |
||
391 |
static { |
|
392 |
System.loadLibrary("instrument"); |
|
393 |
} |
|
394 |
||
395 |
/* |
|
396 |
* Internals |
|
397 |
*/ |
|
398 |
||
399 |
||
400 |
// Enable or disable Java programming language access checks on a |
|
401 |
// reflected object (for example, a method) |
|
402 |
private static void setAccessible(final AccessibleObject ao, final boolean accessible) { |
|
403 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
404 |
public Object run() { |
|
405 |
ao.setAccessible(accessible); |
|
406 |
return null; |
|
407 |
}}); |
|
408 |
} |
|
409 |
||
410 |
// Attempt to load and start an agent |
|
411 |
private void |
|
412 |
loadClassAndStartAgent( String classname, |
|
413 |
String methodname, |
|
414 |
String optionsString) |
|
415 |
throws Throwable { |
|
416 |
||
417 |
ClassLoader mainAppLoader = ClassLoader.getSystemClassLoader(); |
|
418 |
Class<?> javaAgentClass = mainAppLoader.loadClass(classname); |
|
419 |
||
420 |
Method m = null; |
|
421 |
NoSuchMethodException firstExc = null; |
|
422 |
boolean twoArgAgent = false; |
|
423 |
||
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
424 |
// The agent class must have a premain or agentmain method that |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
425 |
// has 1 or 2 arguments. We check in the following order: |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
426 |
// |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
427 |
// 1) declared with a signature of (String, Instrumentation) |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
428 |
// 2) declared with a signature of (String) |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
429 |
// 3) inherited with a signature of (String, Instrumentation) |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
430 |
// 4) inherited with a signature of (String) |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
431 |
// |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
432 |
// So the declared version of either 1-arg or 2-arg always takes |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
433 |
// primary precedence over an inherited version. After that, the |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
434 |
// 2-arg version takes precedence over the 1-arg version. |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
435 |
// |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
436 |
// If no method is found then we throw the NoSuchMethodException |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
437 |
// from the first attempt so that the exception text indicates |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
438 |
// the lookup failed for the 2-arg method (same as JDK5.0). |
2 | 439 |
|
440 |
try { |
|
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
441 |
m = javaAgentClass.getDeclaredMethod( methodname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
442 |
new Class<?>[] { |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
443 |
String.class, |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
444 |
java.lang.instrument.Instrumentation.class |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
445 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
446 |
); |
2 | 447 |
twoArgAgent = true; |
448 |
} catch (NoSuchMethodException x) { |
|
449 |
// remember the NoSuchMethodException |
|
450 |
firstExc = x; |
|
451 |
} |
|
452 |
||
453 |
if (m == null) { |
|
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
454 |
// now try the declared 1-arg method |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
455 |
try { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
456 |
m = javaAgentClass.getDeclaredMethod(methodname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
457 |
new Class<?>[] { String.class }); |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
458 |
} catch (NoSuchMethodException x) { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
459 |
// ignore this exception because we'll try |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
460 |
// two arg inheritance next |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
461 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
462 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
463 |
|
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
464 |
if (m == null) { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
465 |
// now try the inherited 2-arg method |
2 | 466 |
try { |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
467 |
m = javaAgentClass.getMethod( methodname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
468 |
new Class<?>[] { |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
469 |
String.class, |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
470 |
java.lang.instrument.Instrumentation.class |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
471 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
472 |
); |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
473 |
twoArgAgent = true; |
2 | 474 |
} catch (NoSuchMethodException x) { |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
475 |
// ignore this exception because we'll try |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
476 |
// one arg inheritance next |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
477 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
478 |
} |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
479 |
|
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
480 |
if (m == null) { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
481 |
// finally try the inherited 1-arg method |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
482 |
try { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
483 |
m = javaAgentClass.getMethod(methodname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
484 |
new Class<?>[] { String.class }); |
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
485 |
} catch (NoSuchMethodException x) { |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
486 |
// none of the methods exists so we throw the |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
487 |
// first NoSuchMethodException as per 5.0 |
2 | 488 |
throw firstExc; |
489 |
} |
|
490 |
} |
|
491 |
||
492 |
// the premain method should not be required to be public, |
|
493 |
// make it accessible so we can call it |
|
274
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
494 |
// Note: The spec says the following: |
d0ca9ee56273
6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents:
2
diff
changeset
|
495 |
// The agent class must implement a public static premain method... |
2 | 496 |
setAccessible(m, true); |
497 |
||
498 |
// invoke the 1 or 2-arg method |
|
499 |
if (twoArgAgent) { |
|
500 |
m.invoke(null, new Object[] { optionsString, this }); |
|
501 |
} else { |
|
502 |
m.invoke(null, new Object[] { optionsString }); |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
// WARNING: the native code knows the name & signature of this method |
|
507 |
private void |
|
508 |
loadClassAndCallPremain( String classname, |
|
509 |
String optionsString) |
|
510 |
throws Throwable { |
|
511 |
||
512 |
loadClassAndStartAgent( classname, "premain", optionsString ); |
|
513 |
} |
|
514 |
||
515 |
||
516 |
// WARNING: the native code knows the name & signature of this method |
|
517 |
private void |
|
518 |
loadClassAndCallAgentmain( String classname, |
|
519 |
String optionsString) |
|
520 |
throws Throwable { |
|
521 |
||
522 |
loadClassAndStartAgent( classname, "agentmain", optionsString ); |
|
523 |
} |
|
524 |
||
525 |
// WARNING: the native code knows the name & signature of this method |
|
526 |
private byte[] |
|
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
37807
diff
changeset
|
527 |
transform( Module module, |
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
37807
diff
changeset
|
528 |
ClassLoader loader, |
2 | 529 |
String classname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
530 |
Class<?> classBeingRedefined, |
2 | 531 |
ProtectionDomain protectionDomain, |
532 |
byte[] classfileBuffer, |
|
533 |
boolean isRetransformer) { |
|
534 |
TransformerManager mgr = isRetransformer? |
|
535 |
mRetransfomableTransformerManager : |
|
536 |
mTransformerManager; |
|
36511 | 537 |
// module is null when not a class load or when loading a class in an |
538 |
// unnamed module and this is the first type to be loaded in the package. |
|
539 |
if (module == null) { |
|
540 |
if (classBeingRedefined != null) { |
|
541 |
module = classBeingRedefined.getModule(); |
|
542 |
} else { |
|
37807
78e5cb21ca13
8156147: NPE in InstrumentationImpl.transform when loading classes from -Xbootclasspath/a
sspitsyn
parents:
36511
diff
changeset
|
543 |
module = (loader == null) ? jdk.internal.loader.BootLoader.getUnnamedModule() |
78e5cb21ca13
8156147: NPE in InstrumentationImpl.transform when loading classes from -Xbootclasspath/a
sspitsyn
parents:
36511
diff
changeset
|
544 |
: loader.getUnnamedModule(); |
36511 | 545 |
} |
546 |
} |
|
2 | 547 |
if (mgr == null) { |
548 |
return null; // no manager, no transform |
|
549 |
} else { |
|
36511 | 550 |
return mgr.transform( module, |
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
37807
diff
changeset
|
551 |
loader, |
2 | 552 |
classname, |
553 |
classBeingRedefined, |
|
554 |
protectionDomain, |
|
555 |
classfileBuffer); |
|
556 |
} |
|
557 |
} |
|
558 |
} |