jaroslav@1646
|
1 |
/*
|
jaroslav@1646
|
2 |
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@1646
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1646
|
4 |
*
|
jaroslav@1646
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1646
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1646
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1646
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1646
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1646
|
10 |
*
|
jaroslav@1646
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1646
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1646
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1646
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1646
|
15 |
* accompanied this code).
|
jaroslav@1646
|
16 |
*
|
jaroslav@1646
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1646
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1646
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1646
|
20 |
*
|
jaroslav@1646
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1646
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1646
|
23 |
* questions.
|
jaroslav@1646
|
24 |
*/
|
jaroslav@1646
|
25 |
|
jaroslav@1646
|
26 |
package sun.invoke.anon;
|
jaroslav@1646
|
27 |
|
jaroslav@1646
|
28 |
import java.io.IOException;
|
jaroslav@1646
|
29 |
import java.io.OutputStream;
|
jaroslav@1646
|
30 |
import java.util.Arrays;
|
jaroslav@1646
|
31 |
import java.util.HashSet;
|
jaroslav@1646
|
32 |
import java.util.IdentityHashMap;
|
jaroslav@1646
|
33 |
import java.util.Map;
|
jaroslav@1646
|
34 |
|
jaroslav@1646
|
35 |
import static sun.invoke.anon.ConstantPoolVisitor.*;
|
jaroslav@1646
|
36 |
|
jaroslav@1646
|
37 |
/** A class and its patched constant pool.
|
jaroslav@1646
|
38 |
*
|
jaroslav@1646
|
39 |
* This class allow to modify (patch) a constant pool
|
jaroslav@1646
|
40 |
* by changing the value of its entry.
|
jaroslav@1646
|
41 |
* Entry are referenced using index that can be get
|
jaroslav@1646
|
42 |
* by parsing the constant pool using
|
jaroslav@1646
|
43 |
* {@link ConstantPoolParser#parse(ConstantPoolVisitor)}.
|
jaroslav@1646
|
44 |
*
|
jaroslav@1646
|
45 |
* @see ConstantPoolVisitor
|
jaroslav@1646
|
46 |
* @see ConstantPoolParser#createPatch()
|
jaroslav@1646
|
47 |
*/
|
jaroslav@1646
|
48 |
public class ConstantPoolPatch {
|
jaroslav@1646
|
49 |
final ConstantPoolParser outer;
|
jaroslav@1646
|
50 |
final Object[] patchArray;
|
jaroslav@1646
|
51 |
|
jaroslav@1646
|
52 |
ConstantPoolPatch(ConstantPoolParser outer) {
|
jaroslav@1646
|
53 |
this.outer = outer;
|
jaroslav@1646
|
54 |
this.patchArray = new Object[outer.getLength()];
|
jaroslav@1646
|
55 |
}
|
jaroslav@1646
|
56 |
|
jaroslav@1646
|
57 |
/** Create a {@link ConstantPoolParser} and
|
jaroslav@1646
|
58 |
* a {@link ConstantPoolPatch} in one step.
|
jaroslav@1646
|
59 |
* Equivalent to {@code new ConstantPoolParser(classFile).createPatch()}.
|
jaroslav@1646
|
60 |
*
|
jaroslav@1646
|
61 |
* @param classFile an array of bytes containing a class.
|
jaroslav@1646
|
62 |
* @see #ConstantPoolParser(Class)
|
jaroslav@1646
|
63 |
*/
|
jaroslav@1646
|
64 |
public ConstantPoolPatch(byte[] classFile) throws InvalidConstantPoolFormatException {
|
jaroslav@1646
|
65 |
this(new ConstantPoolParser(classFile));
|
jaroslav@1646
|
66 |
}
|
jaroslav@1646
|
67 |
|
jaroslav@1646
|
68 |
/** Create a {@link ConstantPoolParser} and
|
jaroslav@1646
|
69 |
* a {@link ConstantPoolPatch} in one step.
|
jaroslav@1646
|
70 |
* Equivalent to {@code new ConstantPoolParser(templateClass).createPatch()}.
|
jaroslav@1646
|
71 |
*
|
jaroslav@1646
|
72 |
* @param templateClass the class to parse.
|
jaroslav@1646
|
73 |
* @see #ConstantPoolParser(Class)
|
jaroslav@1646
|
74 |
*/
|
jaroslav@1646
|
75 |
public ConstantPoolPatch(Class<?> templateClass) throws IOException, InvalidConstantPoolFormatException {
|
jaroslav@1646
|
76 |
this(new ConstantPoolParser(templateClass));
|
jaroslav@1646
|
77 |
}
|
jaroslav@1646
|
78 |
|
jaroslav@1646
|
79 |
|
jaroslav@1646
|
80 |
/** Creates a patch from an existing patch.
|
jaroslav@1646
|
81 |
* All changes are copied from that patch.
|
jaroslav@1646
|
82 |
* @param patch a patch
|
jaroslav@1646
|
83 |
*
|
jaroslav@1646
|
84 |
* @see ConstantPoolParser#createPatch()
|
jaroslav@1646
|
85 |
*/
|
jaroslav@1646
|
86 |
public ConstantPoolPatch(ConstantPoolPatch patch) {
|
jaroslav@1646
|
87 |
outer = patch.outer;
|
jaroslav@1646
|
88 |
patchArray = patch.patchArray.clone();
|
jaroslav@1646
|
89 |
}
|
jaroslav@1646
|
90 |
|
jaroslav@1646
|
91 |
/** Which parser built this patch? */
|
jaroslav@1646
|
92 |
public ConstantPoolParser getParser() {
|
jaroslav@1646
|
93 |
return outer;
|
jaroslav@1646
|
94 |
}
|
jaroslav@1646
|
95 |
|
jaroslav@1646
|
96 |
/** Report the tag at the given index in the constant pool. */
|
jaroslav@1646
|
97 |
public byte getTag(int index) {
|
jaroslav@1646
|
98 |
return outer.getTag(index);
|
jaroslav@1646
|
99 |
}
|
jaroslav@1646
|
100 |
|
jaroslav@1646
|
101 |
/** Report the current patch at the given index of the constant pool.
|
jaroslav@1646
|
102 |
* Null means no patch will be made.
|
jaroslav@1646
|
103 |
* To observe the unpatched entry at the given index, use
|
jaroslav@1646
|
104 |
* {@link #getParser()}{@code .}@link ConstantPoolParser#parse(ConstantPoolVisitor)}
|
jaroslav@1646
|
105 |
*/
|
jaroslav@1646
|
106 |
public Object getPatch(int index) {
|
jaroslav@1646
|
107 |
Object value = patchArray[index];
|
jaroslav@1646
|
108 |
if (value == null) return null;
|
jaroslav@1646
|
109 |
switch (getTag(index)) {
|
jaroslav@1646
|
110 |
case CONSTANT_Fieldref:
|
jaroslav@1646
|
111 |
case CONSTANT_Methodref:
|
jaroslav@1646
|
112 |
case CONSTANT_InterfaceMethodref:
|
jaroslav@1646
|
113 |
if (value instanceof String)
|
jaroslav@1646
|
114 |
value = stripSemis(2, (String) value);
|
jaroslav@1646
|
115 |
break;
|
jaroslav@1646
|
116 |
case CONSTANT_NameAndType:
|
jaroslav@1646
|
117 |
if (value instanceof String)
|
jaroslav@1646
|
118 |
value = stripSemis(1, (String) value);
|
jaroslav@1646
|
119 |
break;
|
jaroslav@1646
|
120 |
}
|
jaroslav@1646
|
121 |
return value;
|
jaroslav@1646
|
122 |
}
|
jaroslav@1646
|
123 |
|
jaroslav@1646
|
124 |
/** Clear all patches. */
|
jaroslav@1646
|
125 |
public void clear() {
|
jaroslav@1646
|
126 |
Arrays.fill(patchArray, null);
|
jaroslav@1646
|
127 |
}
|
jaroslav@1646
|
128 |
|
jaroslav@1646
|
129 |
/** Clear one patch. */
|
jaroslav@1646
|
130 |
public void clear(int index) {
|
jaroslav@1646
|
131 |
patchArray[index] = null;
|
jaroslav@1646
|
132 |
}
|
jaroslav@1646
|
133 |
|
jaroslav@1646
|
134 |
/** Produce the patches as an array. */
|
jaroslav@1646
|
135 |
public Object[] getPatches() {
|
jaroslav@1646
|
136 |
return patchArray.clone();
|
jaroslav@1646
|
137 |
}
|
jaroslav@1646
|
138 |
|
jaroslav@1646
|
139 |
/** Produce the original constant pool as an array. */
|
jaroslav@1646
|
140 |
public Object[] getOriginalCP() throws InvalidConstantPoolFormatException {
|
jaroslav@1646
|
141 |
return getOriginalCP(0, patchArray.length, -1);
|
jaroslav@1646
|
142 |
}
|
jaroslav@1646
|
143 |
|
jaroslav@1646
|
144 |
/** Walk the constant pool, applying patches using the given map.
|
jaroslav@1646
|
145 |
*
|
jaroslav@1646
|
146 |
* @param utf8Map Utf8 strings to modify, if encountered
|
jaroslav@1646
|
147 |
* @param classMap Classes (or their names) to modify, if encountered
|
jaroslav@1646
|
148 |
* @param valueMap Constant values to modify, if encountered
|
jaroslav@1646
|
149 |
* @param deleteUsedEntries if true, delete map entries that are used
|
jaroslav@1646
|
150 |
*/
|
jaroslav@1646
|
151 |
public void putPatches(final Map<String,String> utf8Map,
|
jaroslav@1646
|
152 |
final Map<String,Object> classMap,
|
jaroslav@1646
|
153 |
final Map<Object,Object> valueMap,
|
jaroslav@1646
|
154 |
boolean deleteUsedEntries) throws InvalidConstantPoolFormatException {
|
jaroslav@1646
|
155 |
final HashSet<String> usedUtf8Keys;
|
jaroslav@1646
|
156 |
final HashSet<String> usedClassKeys;
|
jaroslav@1646
|
157 |
final HashSet<Object> usedValueKeys;
|
jaroslav@1646
|
158 |
if (deleteUsedEntries) {
|
jaroslav@1646
|
159 |
usedUtf8Keys = (utf8Map == null) ? null : new HashSet<String>();
|
jaroslav@1646
|
160 |
usedClassKeys = (classMap == null) ? null : new HashSet<String>();
|
jaroslav@1646
|
161 |
usedValueKeys = (valueMap == null) ? null : new HashSet<Object>();
|
jaroslav@1646
|
162 |
} else {
|
jaroslav@1646
|
163 |
usedUtf8Keys = null;
|
jaroslav@1646
|
164 |
usedClassKeys = null;
|
jaroslav@1646
|
165 |
usedValueKeys = null;
|
jaroslav@1646
|
166 |
}
|
jaroslav@1646
|
167 |
|
jaroslav@1646
|
168 |
outer.parse(new ConstantPoolVisitor() {
|
jaroslav@1646
|
169 |
|
jaroslav@1646
|
170 |
@Override
|
jaroslav@1646
|
171 |
public void visitUTF8(int index, byte tag, String utf8) {
|
jaroslav@1646
|
172 |
putUTF8(index, utf8Map.get(utf8));
|
jaroslav@1646
|
173 |
if (usedUtf8Keys != null) usedUtf8Keys.add(utf8);
|
jaroslav@1646
|
174 |
}
|
jaroslav@1646
|
175 |
|
jaroslav@1646
|
176 |
@Override
|
jaroslav@1646
|
177 |
public void visitConstantValue(int index, byte tag, Object value) {
|
jaroslav@1646
|
178 |
putConstantValue(index, tag, valueMap.get(value));
|
jaroslav@1646
|
179 |
if (usedValueKeys != null) usedValueKeys.add(value);
|
jaroslav@1646
|
180 |
}
|
jaroslav@1646
|
181 |
|
jaroslav@1646
|
182 |
@Override
|
jaroslav@1646
|
183 |
public void visitConstantString(int index, byte tag, String name, int nameIndex) {
|
jaroslav@1646
|
184 |
if (tag == CONSTANT_Class) {
|
jaroslav@1646
|
185 |
putConstantValue(index, tag, classMap.get(name));
|
jaroslav@1646
|
186 |
if (usedClassKeys != null) usedClassKeys.add(name);
|
jaroslav@1646
|
187 |
} else {
|
jaroslav@1646
|
188 |
assert(tag == CONSTANT_String);
|
jaroslav@1646
|
189 |
visitConstantValue(index, tag, name);
|
jaroslav@1646
|
190 |
}
|
jaroslav@1646
|
191 |
}
|
jaroslav@1646
|
192 |
});
|
jaroslav@1646
|
193 |
if (usedUtf8Keys != null) utf8Map.keySet().removeAll(usedUtf8Keys);
|
jaroslav@1646
|
194 |
if (usedClassKeys != null) classMap.keySet().removeAll(usedClassKeys);
|
jaroslav@1646
|
195 |
if (usedValueKeys != null) valueMap.keySet().removeAll(usedValueKeys);
|
jaroslav@1646
|
196 |
}
|
jaroslav@1646
|
197 |
|
jaroslav@1646
|
198 |
Object[] getOriginalCP(final int startIndex,
|
jaroslav@1646
|
199 |
final int endIndex,
|
jaroslav@1646
|
200 |
final int tagMask) throws InvalidConstantPoolFormatException {
|
jaroslav@1646
|
201 |
final Object[] cpArray = new Object[endIndex - startIndex];
|
jaroslav@1646
|
202 |
outer.parse(new ConstantPoolVisitor() {
|
jaroslav@1646
|
203 |
|
jaroslav@1646
|
204 |
void show(int index, byte tag, Object value) {
|
jaroslav@1646
|
205 |
if (index < startIndex || index >= endIndex) return;
|
jaroslav@1646
|
206 |
if (((1 << tag) & tagMask) == 0) return;
|
jaroslav@1646
|
207 |
cpArray[index - startIndex] = value;
|
jaroslav@1646
|
208 |
}
|
jaroslav@1646
|
209 |
|
jaroslav@1646
|
210 |
@Override
|
jaroslav@1646
|
211 |
public void visitUTF8(int index, byte tag, String utf8) {
|
jaroslav@1646
|
212 |
show(index, tag, utf8);
|
jaroslav@1646
|
213 |
}
|
jaroslav@1646
|
214 |
|
jaroslav@1646
|
215 |
@Override
|
jaroslav@1646
|
216 |
public void visitConstantValue(int index, byte tag, Object value) {
|
jaroslav@1646
|
217 |
assert(tag != CONSTANT_String);
|
jaroslav@1646
|
218 |
show(index, tag, value);
|
jaroslav@1646
|
219 |
}
|
jaroslav@1646
|
220 |
|
jaroslav@1646
|
221 |
@Override
|
jaroslav@1646
|
222 |
public void visitConstantString(int index, byte tag,
|
jaroslav@1646
|
223 |
String value, int j) {
|
jaroslav@1646
|
224 |
show(index, tag, value);
|
jaroslav@1646
|
225 |
}
|
jaroslav@1646
|
226 |
|
jaroslav@1646
|
227 |
@Override
|
jaroslav@1646
|
228 |
public void visitMemberRef(int index, byte tag,
|
jaroslav@1646
|
229 |
String className, String memberName,
|
jaroslav@1646
|
230 |
String signature,
|
jaroslav@1646
|
231 |
int j, int k) {
|
jaroslav@1646
|
232 |
show(index, tag, new String[]{ className, memberName, signature });
|
jaroslav@1646
|
233 |
}
|
jaroslav@1646
|
234 |
|
jaroslav@1646
|
235 |
@Override
|
jaroslav@1646
|
236 |
public void visitDescriptor(int index, byte tag,
|
jaroslav@1646
|
237 |
String memberName, String signature,
|
jaroslav@1646
|
238 |
int j, int k) {
|
jaroslav@1646
|
239 |
show(index, tag, new String[]{ memberName, signature });
|
jaroslav@1646
|
240 |
}
|
jaroslav@1646
|
241 |
});
|
jaroslav@1646
|
242 |
return cpArray;
|
jaroslav@1646
|
243 |
}
|
jaroslav@1646
|
244 |
|
jaroslav@1646
|
245 |
/** Write the head (header plus constant pool)
|
jaroslav@1646
|
246 |
* of the patched class file to the indicated stream.
|
jaroslav@1646
|
247 |
*/
|
jaroslav@1646
|
248 |
void writeHead(OutputStream out) throws IOException {
|
jaroslav@1646
|
249 |
outer.writePatchedHead(out, patchArray);
|
jaroslav@1646
|
250 |
}
|
jaroslav@1646
|
251 |
|
jaroslav@1646
|
252 |
/** Write the tail (everything after the constant pool)
|
jaroslav@1646
|
253 |
* of the patched class file to the indicated stream.
|
jaroslav@1646
|
254 |
*/
|
jaroslav@1646
|
255 |
void writeTail(OutputStream out) throws IOException {
|
jaroslav@1646
|
256 |
outer.writeTail(out);
|
jaroslav@1646
|
257 |
}
|
jaroslav@1646
|
258 |
|
jaroslav@1646
|
259 |
private void checkConstantTag(byte tag, Object value) {
|
jaroslav@1646
|
260 |
if (value == null)
|
jaroslav@1646
|
261 |
throw new IllegalArgumentException(
|
jaroslav@1646
|
262 |
"invalid null constant value");
|
jaroslav@1646
|
263 |
if (classForTag(tag) != value.getClass())
|
jaroslav@1646
|
264 |
throw new IllegalArgumentException(
|
jaroslav@1646
|
265 |
"invalid constant value"
|
jaroslav@1646
|
266 |
+ (tag == CONSTANT_None ? ""
|
jaroslav@1646
|
267 |
: " for tag "+tagName(tag))
|
jaroslav@1646
|
268 |
+ " of class "+value.getClass());
|
jaroslav@1646
|
269 |
}
|
jaroslav@1646
|
270 |
|
jaroslav@1646
|
271 |
private void checkTag(int index, byte putTag) {
|
jaroslav@1646
|
272 |
byte tag = outer.tags[index];
|
jaroslav@1646
|
273 |
if (tag != putTag)
|
jaroslav@1646
|
274 |
throw new IllegalArgumentException(
|
jaroslav@1646
|
275 |
"invalid put operation"
|
jaroslav@1646
|
276 |
+ " for " + tagName(putTag)
|
jaroslav@1646
|
277 |
+ " at index " + index + " found " + tagName(tag));
|
jaroslav@1646
|
278 |
}
|
jaroslav@1646
|
279 |
|
jaroslav@1646
|
280 |
private void checkTagMask(int index, int tagBitMask) {
|
jaroslav@1646
|
281 |
byte tag = outer.tags[index];
|
jaroslav@1646
|
282 |
int tagBit = ((tag & 0x1F) == tag) ? (1 << tag) : 0;
|
jaroslav@1646
|
283 |
if ((tagBit & tagBitMask) == 0)
|
jaroslav@1646
|
284 |
throw new IllegalArgumentException(
|
jaroslav@1646
|
285 |
"invalid put operation"
|
jaroslav@1646
|
286 |
+ " at index " + index + " found " + tagName(tag));
|
jaroslav@1646
|
287 |
}
|
jaroslav@1646
|
288 |
|
jaroslav@1646
|
289 |
private static void checkMemberName(String memberName) {
|
jaroslav@1646
|
290 |
if (memberName.indexOf(';') >= 0)
|
jaroslav@1646
|
291 |
throw new IllegalArgumentException("memberName " + memberName + " contains a ';'");
|
jaroslav@1646
|
292 |
}
|
jaroslav@1646
|
293 |
|
jaroslav@1646
|
294 |
/** Set the entry of the constant pool indexed by index to
|
jaroslav@1646
|
295 |
* a new string.
|
jaroslav@1646
|
296 |
*
|
jaroslav@1646
|
297 |
* @param index an index to a constant pool entry containing a
|
jaroslav@1646
|
298 |
* {@link ConstantPoolVisitor#CONSTANT_Utf8} value.
|
jaroslav@1646
|
299 |
* @param utf8 a string
|
jaroslav@1646
|
300 |
*
|
jaroslav@1646
|
301 |
* @see ConstantPoolVisitor#visitUTF8(int, byte, String)
|
jaroslav@1646
|
302 |
*/
|
jaroslav@1646
|
303 |
public void putUTF8(int index, String utf8) {
|
jaroslav@1646
|
304 |
if (utf8 == null) { clear(index); return; }
|
jaroslav@1646
|
305 |
checkTag(index, CONSTANT_Utf8);
|
jaroslav@1646
|
306 |
patchArray[index] = utf8;
|
jaroslav@1646
|
307 |
}
|
jaroslav@1646
|
308 |
|
jaroslav@1646
|
309 |
/** Set the entry of the constant pool indexed by index to
|
jaroslav@1646
|
310 |
* a new value, depending on its dynamic type.
|
jaroslav@1646
|
311 |
*
|
jaroslav@1646
|
312 |
* @param index an index to a constant pool entry containing a
|
jaroslav@1646
|
313 |
* one of the following structures:
|
jaroslav@1646
|
314 |
* {@link ConstantPoolVisitor#CONSTANT_Integer},
|
jaroslav@1646
|
315 |
* {@link ConstantPoolVisitor#CONSTANT_Float},
|
jaroslav@1646
|
316 |
* {@link ConstantPoolVisitor#CONSTANT_Long},
|
jaroslav@1646
|
317 |
* {@link ConstantPoolVisitor#CONSTANT_Double},
|
jaroslav@1646
|
318 |
* {@link ConstantPoolVisitor#CONSTANT_String}, or
|
jaroslav@1646
|
319 |
* {@link ConstantPoolVisitor#CONSTANT_Class}
|
jaroslav@1646
|
320 |
* @param value a boxed int, float, long or double; or a string or class object
|
jaroslav@1646
|
321 |
* @throws IllegalArgumentException if the type of the constant does not
|
jaroslav@1646
|
322 |
* match the constant pool entry type,
|
jaroslav@1646
|
323 |
* as reported by {@link #getTag(int)}
|
jaroslav@1646
|
324 |
*
|
jaroslav@1646
|
325 |
* @see #putConstantValue(int, byte, Object)
|
jaroslav@1646
|
326 |
* @see ConstantPoolVisitor#visitConstantValue(int, byte, Object)
|
jaroslav@1646
|
327 |
* @see ConstantPoolVisitor#visitConstantString(int, byte, String, int)
|
jaroslav@1646
|
328 |
*/
|
jaroslav@1646
|
329 |
public void putConstantValue(int index, Object value) {
|
jaroslav@1646
|
330 |
if (value == null) { clear(index); return; }
|
jaroslav@1646
|
331 |
byte tag = tagForConstant(value.getClass());
|
jaroslav@1646
|
332 |
checkConstantTag(tag, value);
|
jaroslav@1646
|
333 |
checkTag(index, tag);
|
jaroslav@1646
|
334 |
patchArray[index] = value;
|
jaroslav@1646
|
335 |
}
|
jaroslav@1646
|
336 |
|
jaroslav@1646
|
337 |
/** Set the entry of the constant pool indexed by index to
|
jaroslav@1646
|
338 |
* a new value.
|
jaroslav@1646
|
339 |
*
|
jaroslav@1646
|
340 |
* @param index an index to a constant pool entry matching the given tag
|
jaroslav@1646
|
341 |
* @param tag one of the following values:
|
jaroslav@1646
|
342 |
* {@link ConstantPoolVisitor#CONSTANT_Integer},
|
jaroslav@1646
|
343 |
* {@link ConstantPoolVisitor#CONSTANT_Float},
|
jaroslav@1646
|
344 |
* {@link ConstantPoolVisitor#CONSTANT_Long},
|
jaroslav@1646
|
345 |
* {@link ConstantPoolVisitor#CONSTANT_Double},
|
jaroslav@1646
|
346 |
* {@link ConstantPoolVisitor#CONSTANT_String}, or
|
jaroslav@1646
|
347 |
* {@link ConstantPoolVisitor#CONSTANT_Class}
|
jaroslav@1646
|
348 |
* @param value a boxed number, string, or class object
|
jaroslav@1646
|
349 |
* @throws IllegalArgumentException if the type of the constant does not
|
jaroslav@1646
|
350 |
* match the constant pool entry type, or if a class name contains
|
jaroslav@1646
|
351 |
* '/' or ';'
|
jaroslav@1646
|
352 |
*
|
jaroslav@1646
|
353 |
* @see #putConstantValue(int, Object)
|
jaroslav@1646
|
354 |
* @see ConstantPoolVisitor#visitConstantValue(int, byte, Object)
|
jaroslav@1646
|
355 |
* @see ConstantPoolVisitor#visitConstantString(int, byte, String, int)
|
jaroslav@1646
|
356 |
*/
|
jaroslav@1646
|
357 |
public void putConstantValue(int index, byte tag, Object value) {
|
jaroslav@1646
|
358 |
if (value == null) { clear(index); return; }
|
jaroslav@1646
|
359 |
checkTag(index, tag);
|
jaroslav@1646
|
360 |
if (tag == CONSTANT_Class && value instanceof String) {
|
jaroslav@1646
|
361 |
checkClassName((String) value);
|
jaroslav@1646
|
362 |
} else if (tag == CONSTANT_String) {
|
jaroslav@1646
|
363 |
// the JVM accepts any object as a patch for a string
|
jaroslav@1646
|
364 |
} else {
|
jaroslav@1646
|
365 |
// make sure the incoming value is the right type
|
jaroslav@1646
|
366 |
checkConstantTag(tag, value);
|
jaroslav@1646
|
367 |
}
|
jaroslav@1646
|
368 |
checkTag(index, tag);
|
jaroslav@1646
|
369 |
patchArray[index] = value;
|
jaroslav@1646
|
370 |
}
|
jaroslav@1646
|
371 |
|
jaroslav@1646
|
372 |
/** Set the entry of the constant pool indexed by index to
|
jaroslav@1646
|
373 |
* a new {@link ConstantPoolVisitor#CONSTANT_NameAndType} value.
|
jaroslav@1646
|
374 |
*
|
jaroslav@1646
|
375 |
* @param index an index to a constant pool entry containing a
|
jaroslav@1646
|
376 |
* {@link ConstantPoolVisitor#CONSTANT_NameAndType} value.
|
jaroslav@1646
|
377 |
* @param memberName a memberName
|
jaroslav@1646
|
378 |
* @param signature a signature
|
jaroslav@1646
|
379 |
* @throws IllegalArgumentException if memberName contains the character ';'
|
jaroslav@1646
|
380 |
*
|
jaroslav@1646
|
381 |
* @see ConstantPoolVisitor#visitDescriptor(int, byte, String, String, int, int)
|
jaroslav@1646
|
382 |
*/
|
jaroslav@1646
|
383 |
public void putDescriptor(int index, String memberName, String signature) {
|
jaroslav@1646
|
384 |
checkTag(index, CONSTANT_NameAndType);
|
jaroslav@1646
|
385 |
checkMemberName(memberName);
|
jaroslav@1646
|
386 |
patchArray[index] = addSemis(memberName, signature);
|
jaroslav@1646
|
387 |
}
|
jaroslav@1646
|
388 |
|
jaroslav@1646
|
389 |
/** Set the entry of the constant pool indexed by index to
|
jaroslav@1646
|
390 |
* a new {@link ConstantPoolVisitor#CONSTANT_Fieldref},
|
jaroslav@1646
|
391 |
* {@link ConstantPoolVisitor#CONSTANT_Methodref}, or
|
jaroslav@1646
|
392 |
* {@link ConstantPoolVisitor#CONSTANT_InterfaceMethodref} value.
|
jaroslav@1646
|
393 |
*
|
jaroslav@1646
|
394 |
* @param index an index to a constant pool entry containing a member reference
|
jaroslav@1646
|
395 |
* @param className a class name
|
jaroslav@1646
|
396 |
* @param memberName a field or method name
|
jaroslav@1646
|
397 |
* @param signature a field or method signature
|
jaroslav@1646
|
398 |
* @throws IllegalArgumentException if memberName contains the character ';'
|
jaroslav@1646
|
399 |
* or signature is not a correct signature
|
jaroslav@1646
|
400 |
*
|
jaroslav@1646
|
401 |
* @see ConstantPoolVisitor#visitMemberRef(int, byte, String, String, String, int, int)
|
jaroslav@1646
|
402 |
*/
|
jaroslav@1646
|
403 |
public void putMemberRef(int index, byte tag,
|
jaroslav@1646
|
404 |
String className, String memberName, String signature) {
|
jaroslav@1646
|
405 |
checkTagMask(tag, CONSTANT_MemberRef_MASK);
|
jaroslav@1646
|
406 |
checkTag(index, tag);
|
jaroslav@1646
|
407 |
checkClassName(className);
|
jaroslav@1646
|
408 |
checkMemberName(memberName);
|
jaroslav@1646
|
409 |
if (signature.startsWith("(") == (tag == CONSTANT_Fieldref))
|
jaroslav@1646
|
410 |
throw new IllegalArgumentException("bad signature: "+signature);
|
jaroslav@1646
|
411 |
patchArray[index] = addSemis(className, memberName, signature);
|
jaroslav@1646
|
412 |
}
|
jaroslav@1646
|
413 |
|
jaroslav@1646
|
414 |
static private final int CONSTANT_MemberRef_MASK =
|
jaroslav@1646
|
415 |
CONSTANT_Fieldref
|
jaroslav@1646
|
416 |
| CONSTANT_Methodref
|
jaroslav@1646
|
417 |
| CONSTANT_InterfaceMethodref;
|
jaroslav@1646
|
418 |
|
jaroslav@1646
|
419 |
private static final Map<Class<?>, Byte> CONSTANT_VALUE_CLASS_TAG
|
jaroslav@1646
|
420 |
= new IdentityHashMap<Class<?>, Byte>();
|
jaroslav@1646
|
421 |
private static final Class<?>[] CONSTANT_VALUE_CLASS = new Class<?>[16];
|
jaroslav@1646
|
422 |
static {
|
jaroslav@1646
|
423 |
Object[][] values = {
|
jaroslav@1646
|
424 |
{Integer.class, CONSTANT_Integer},
|
jaroslav@1646
|
425 |
{Long.class, CONSTANT_Long},
|
jaroslav@1646
|
426 |
{Float.class, CONSTANT_Float},
|
jaroslav@1646
|
427 |
{Double.class, CONSTANT_Double},
|
jaroslav@1646
|
428 |
{String.class, CONSTANT_String},
|
jaroslav@1646
|
429 |
{Class.class, CONSTANT_Class}
|
jaroslav@1646
|
430 |
};
|
jaroslav@1646
|
431 |
for (Object[] value : values) {
|
jaroslav@1646
|
432 |
Class<?> cls = (Class<?>)value[0];
|
jaroslav@1646
|
433 |
Byte tag = (Byte) value[1];
|
jaroslav@1646
|
434 |
CONSTANT_VALUE_CLASS_TAG.put(cls, tag);
|
jaroslav@1646
|
435 |
CONSTANT_VALUE_CLASS[(byte)tag] = cls;
|
jaroslav@1646
|
436 |
}
|
jaroslav@1646
|
437 |
}
|
jaroslav@1646
|
438 |
|
jaroslav@1646
|
439 |
static Class<?> classForTag(byte tag) {
|
jaroslav@1646
|
440 |
if ((tag & 0xFF) >= CONSTANT_VALUE_CLASS.length)
|
jaroslav@1646
|
441 |
return null;
|
jaroslav@1646
|
442 |
return CONSTANT_VALUE_CLASS[tag];
|
jaroslav@1646
|
443 |
}
|
jaroslav@1646
|
444 |
|
jaroslav@1646
|
445 |
static byte tagForConstant(Class<?> cls) {
|
jaroslav@1646
|
446 |
Byte tag = CONSTANT_VALUE_CLASS_TAG.get(cls);
|
jaroslav@1646
|
447 |
return (tag == null) ? CONSTANT_None : (byte)tag;
|
jaroslav@1646
|
448 |
}
|
jaroslav@1646
|
449 |
|
jaroslav@1646
|
450 |
private static void checkClassName(String className) {
|
jaroslav@1646
|
451 |
if (className.indexOf('/') >= 0 || className.indexOf(';') >= 0)
|
jaroslav@1646
|
452 |
throw new IllegalArgumentException("invalid class name " + className);
|
jaroslav@1646
|
453 |
}
|
jaroslav@1646
|
454 |
|
jaroslav@1646
|
455 |
static String addSemis(String name, String... names) {
|
jaroslav@1646
|
456 |
StringBuilder buf = new StringBuilder(name.length() * 5);
|
jaroslav@1646
|
457 |
buf.append(name);
|
jaroslav@1646
|
458 |
for (String name2 : names) {
|
jaroslav@1646
|
459 |
buf.append(';').append(name2);
|
jaroslav@1646
|
460 |
}
|
jaroslav@1646
|
461 |
String res = buf.toString();
|
jaroslav@1646
|
462 |
assert(stripSemis(names.length, res)[0].equals(name));
|
jaroslav@1646
|
463 |
assert(stripSemis(names.length, res)[1].equals(names[0]));
|
jaroslav@1646
|
464 |
assert(names.length == 1 ||
|
jaroslav@1646
|
465 |
stripSemis(names.length, res)[2].equals(names[1]));
|
jaroslav@1646
|
466 |
return res;
|
jaroslav@1646
|
467 |
}
|
jaroslav@1646
|
468 |
|
jaroslav@1646
|
469 |
static String[] stripSemis(int count, String string) {
|
jaroslav@1646
|
470 |
String[] res = new String[count+1];
|
jaroslav@1646
|
471 |
int pos = 0;
|
jaroslav@1646
|
472 |
for (int i = 0; i < count; i++) {
|
jaroslav@1646
|
473 |
int pos2 = string.indexOf(';', pos);
|
jaroslav@1646
|
474 |
if (pos2 < 0) pos2 = string.length(); // yuck
|
jaroslav@1646
|
475 |
res[i] = string.substring(pos, pos2);
|
jaroslav@1646
|
476 |
pos = pos2;
|
jaroslav@1646
|
477 |
}
|
jaroslav@1646
|
478 |
res[count] = string.substring(pos);
|
jaroslav@1646
|
479 |
return res;
|
jaroslav@1646
|
480 |
}
|
jaroslav@1646
|
481 |
|
jaroslav@1646
|
482 |
public String toString() {
|
jaroslav@1646
|
483 |
StringBuilder buf = new StringBuilder(this.getClass().getName());
|
jaroslav@1646
|
484 |
buf.append("{");
|
jaroslav@1646
|
485 |
Object[] origCP = null;
|
jaroslav@1646
|
486 |
for (int i = 0; i < patchArray.length; i++) {
|
jaroslav@1646
|
487 |
if (patchArray[i] == null) continue;
|
jaroslav@1646
|
488 |
if (origCP != null) {
|
jaroslav@1646
|
489 |
buf.append(", ");
|
jaroslav@1646
|
490 |
} else {
|
jaroslav@1646
|
491 |
try {
|
jaroslav@1646
|
492 |
origCP = getOriginalCP();
|
jaroslav@1646
|
493 |
} catch (InvalidConstantPoolFormatException ee) {
|
jaroslav@1646
|
494 |
origCP = new Object[0];
|
jaroslav@1646
|
495 |
}
|
jaroslav@1646
|
496 |
}
|
jaroslav@1646
|
497 |
Object orig = (i < origCP.length) ? origCP[i] : "?";
|
jaroslav@1646
|
498 |
buf.append(orig).append("=").append(patchArray[i]);
|
jaroslav@1646
|
499 |
}
|
jaroslav@1646
|
500 |
buf.append("}");
|
jaroslav@1646
|
501 |
return buf.toString();
|
jaroslav@1646
|
502 |
}
|
jaroslav@1646
|
503 |
}
|