author | sspitsyn |
Sat, 09 Jul 2016 21:41:14 -0700 | |
changeset 39739 | 5d058d6bd245 |
parent 36511 | 9d0388c6b336 |
child 44545 | 83b611b88ac8 |
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 |
||
28 |
||
29 |
import java.lang.instrument.Instrumentation; |
|
30 |
import java.lang.instrument.ClassFileTransformer; |
|
36511 | 31 |
import java.lang.reflect.Module; |
2 | 32 |
import java.security.ProtectionDomain; |
33 |
||
34 |
/* |
|
35 |
* Copyright 2003 Wily Technology, Inc. |
|
36 |
*/ |
|
37 |
||
38 |
/** |
|
39 |
* Support class for the InstrumentationImpl. Manages the list of registered transformers. |
|
40 |
* Keeps everything in the right order, deals with sync of the list, |
|
41 |
* and actually does the calling of the transformers. |
|
42 |
*/ |
|
43 |
public class TransformerManager |
|
44 |
{ |
|
45 |
private class TransformerInfo { |
|
46 |
final ClassFileTransformer mTransformer; |
|
47 |
String mPrefix; |
|
48 |
||
49 |
TransformerInfo(ClassFileTransformer transformer) { |
|
50 |
mTransformer = transformer; |
|
51 |
mPrefix = null; |
|
52 |
} |
|
53 |
||
54 |
ClassFileTransformer transformer() { |
|
55 |
return mTransformer; |
|
56 |
} |
|
57 |
||
58 |
String getPrefix() { |
|
59 |
return mPrefix; |
|
60 |
} |
|
61 |
||
62 |
void setPrefix(String prefix) { |
|
63 |
mPrefix = prefix; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* a given instance of this list is treated as immutable to simplify sync; |
|
69 |
* we pay copying overhead whenever the list is changed rather than every time |
|
70 |
* the list is referenced. |
|
71 |
* The array is kept in the order the transformers are added via addTransformer |
|
72 |
* (first added is 0, last added is length-1) |
|
73 |
* Use an array, not a List or other Collection. This keeps the set of classes |
|
74 |
* used by this code to a minimum. We want as few dependencies as possible in this |
|
75 |
* code, since it is used inside the class definition system. Any class referenced here |
|
76 |
* cannot be transformed by Java code. |
|
77 |
*/ |
|
78 |
private TransformerInfo[] mTransformerList; |
|
79 |
||
80 |
/*** |
|
81 |
* Is this TransformerManager for transformers capable of retransformation? |
|
82 |
*/ |
|
83 |
private boolean mIsRetransformable; |
|
84 |
||
85 |
TransformerManager(boolean isRetransformable) { |
|
86 |
mTransformerList = new TransformerInfo[0]; |
|
87 |
mIsRetransformable = isRetransformable; |
|
88 |
} |
|
89 |
||
90 |
boolean isRetransformable() { |
|
91 |
return mIsRetransformable; |
|
92 |
} |
|
93 |
||
94 |
public synchronized void |
|
95 |
addTransformer( ClassFileTransformer transformer) { |
|
96 |
TransformerInfo[] oldList = mTransformerList; |
|
97 |
TransformerInfo[] newList = new TransformerInfo[oldList.length + 1]; |
|
98 |
System.arraycopy( oldList, |
|
99 |
0, |
|
100 |
newList, |
|
101 |
0, |
|
102 |
oldList.length); |
|
103 |
newList[oldList.length] = new TransformerInfo(transformer); |
|
104 |
mTransformerList = newList; |
|
105 |
} |
|
106 |
||
107 |
public synchronized boolean |
|
108 |
removeTransformer(ClassFileTransformer transformer) { |
|
109 |
boolean found = false; |
|
110 |
TransformerInfo[] oldList = mTransformerList; |
|
111 |
int oldLength = oldList.length; |
|
112 |
int newLength = oldLength - 1; |
|
113 |
||
114 |
// look for it in the list, starting at the last added, and remember |
|
115 |
// where it was if we found it |
|
116 |
int matchingIndex = 0; |
|
117 |
for ( int x = oldLength - 1; x >= 0; x-- ) { |
|
118 |
if ( oldList[x].transformer() == transformer ) { |
|
119 |
found = true; |
|
120 |
matchingIndex = x; |
|
121 |
break; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
// make a copy of the array without the matching element |
|
126 |
if ( found ) { |
|
127 |
TransformerInfo[] newList = new TransformerInfo[newLength]; |
|
128 |
||
129 |
// copy up to but not including the match |
|
130 |
if ( matchingIndex > 0 ) { |
|
131 |
System.arraycopy( oldList, |
|
132 |
0, |
|
133 |
newList, |
|
134 |
0, |
|
135 |
matchingIndex); |
|
136 |
} |
|
137 |
||
138 |
// if there is anything after the match, copy it as well |
|
139 |
if ( matchingIndex < (newLength) ) { |
|
140 |
System.arraycopy( oldList, |
|
141 |
matchingIndex + 1, |
|
142 |
newList, |
|
143 |
matchingIndex, |
|
144 |
(newLength) - matchingIndex); |
|
145 |
} |
|
146 |
mTransformerList = newList; |
|
147 |
} |
|
148 |
return found; |
|
149 |
} |
|
150 |
||
151 |
synchronized boolean |
|
152 |
includesTransformer(ClassFileTransformer transformer) { |
|
153 |
for (TransformerInfo info : mTransformerList) { |
|
154 |
if ( info.transformer() == transformer ) { |
|
155 |
return true; |
|
156 |
} |
|
157 |
} |
|
158 |
return false; |
|
159 |
} |
|
160 |
||
161 |
// This function doesn't actually snapshot anything, but should be |
|
162 |
// used to set a local variable, which will snapshot the transformer |
|
163 |
// list because of the copying semantics of mTransformerList (see |
|
164 |
// the comment for mTransformerList). |
|
165 |
private TransformerInfo[] |
|
166 |
getSnapshotTransformerList() { |
|
167 |
return mTransformerList; |
|
168 |
} |
|
169 |
||
170 |
public byte[] |
|
36511 | 171 |
transform( Module module, |
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
172 |
ClassLoader loader, |
2 | 173 |
String classname, |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
174 |
Class<?> classBeingRedefined, |
2 | 175 |
ProtectionDomain protectionDomain, |
176 |
byte[] classfileBuffer) { |
|
177 |
boolean someoneTouchedTheBytecode = false; |
|
178 |
||
179 |
TransformerInfo[] transformerList = getSnapshotTransformerList(); |
|
180 |
||
181 |
byte[] bufferToUse = classfileBuffer; |
|
182 |
||
183 |
// order matters, gotta run 'em in the order they were added |
|
184 |
for ( int x = 0; x < transformerList.length; x++ ) { |
|
185 |
TransformerInfo transformerInfo = transformerList[x]; |
|
186 |
ClassFileTransformer transformer = transformerInfo.transformer(); |
|
187 |
byte[] transformedBytes = null; |
|
188 |
||
189 |
try { |
|
36511 | 190 |
transformedBytes = transformer.transform( module, |
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
191 |
loader, |
2 | 192 |
classname, |
193 |
classBeingRedefined, |
|
194 |
protectionDomain, |
|
195 |
bufferToUse); |
|
196 |
} |
|
197 |
catch (Throwable t) { |
|
198 |
// don't let any one transformer mess it up for the others. |
|
199 |
// This is where we need to put some logging. What should go here? FIXME |
|
200 |
} |
|
201 |
||
202 |
if ( transformedBytes != null ) { |
|
203 |
someoneTouchedTheBytecode = true; |
|
204 |
bufferToUse = transformedBytes; |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
// if someone modified it, return the modified buffer. |
|
209 |
// otherwise return null to mean "no transforms occurred" |
|
210 |
byte [] result; |
|
211 |
if ( someoneTouchedTheBytecode ) { |
|
212 |
result = bufferToUse; |
|
213 |
} |
|
214 |
else { |
|
215 |
result = null; |
|
216 |
} |
|
217 |
||
218 |
return result; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
int |
|
223 |
getTransformerCount() { |
|
224 |
TransformerInfo[] transformerList = getSnapshotTransformerList(); |
|
225 |
return transformerList.length; |
|
226 |
} |
|
227 |
||
228 |
boolean |
|
229 |
setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) { |
|
230 |
TransformerInfo[] transformerList = getSnapshotTransformerList(); |
|
231 |
||
232 |
for ( int x = 0; x < transformerList.length; x++ ) { |
|
233 |
TransformerInfo transformerInfo = transformerList[x]; |
|
234 |
ClassFileTransformer aTransformer = transformerInfo.transformer(); |
|
235 |
||
236 |
if ( aTransformer == transformer ) { |
|
237 |
transformerInfo.setPrefix(prefix); |
|
238 |
return true; |
|
239 |
} |
|
240 |
} |
|
241 |
return false; |
|
242 |
} |
|
243 |
||
244 |
||
245 |
String[] |
|
246 |
getNativeMethodPrefixes() { |
|
247 |
TransformerInfo[] transformerList = getSnapshotTransformerList(); |
|
248 |
String[] prefixes = new String[transformerList.length]; |
|
249 |
||
250 |
for ( int x = 0; x < transformerList.length; x++ ) { |
|
251 |
TransformerInfo transformerInfo = transformerList[x]; |
|
252 |
prefixes[x] = transformerInfo.getPrefix(); |
|
253 |
} |
|
254 |
return prefixes; |
|
255 |
} |
|
256 |
} |