author | jrose |
Sat, 30 Oct 2010 13:08:23 -0700 | |
changeset 7114 | 65d21c4c6337 |
parent 6062 | bab93afe9df7 |
child 7118 | aa8f53fcb28f |
permissions | -rw-r--r-- |
1 | 1 |
/* |
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5341
diff
changeset
|
2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5341
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5341
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5341
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
25 |
package sun.jvm.hotspot.tools.jcore; |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.util.*; |
|
29 |
import sun.jvm.hotspot.oops.*; |
|
30 |
import sun.jvm.hotspot.runtime.*; |
|
31 |
||
32 |
public class ClassWriter implements /* imports */ ClassConstants |
|
33 |
{ |
|
34 |
public static final boolean DEBUG = false; |
|
35 |
||
36 |
protected void debugMessage(String message) { |
|
37 |
System.out.println(message); |
|
38 |
} |
|
39 |
||
40 |
protected InstanceKlass klass; |
|
41 |
protected DataOutputStream dos; |
|
42 |
protected ConstantPool cpool; |
|
43 |
||
44 |
// Map between class name to index of type CONSTANT_Class |
|
45 |
protected Map classToIndex = new HashMap(); |
|
46 |
||
47 |
// Map between any modified UTF-8 and it's constant pool index. |
|
48 |
protected Map utf8ToIndex = new HashMap(); |
|
49 |
||
50 |
// constant pool index for attribute names. |
|
51 |
||
52 |
protected short _sourceFileIndex; |
|
53 |
protected short _innerClassesIndex; |
|
54 |
protected short _syntheticIndex; |
|
55 |
protected short _deprecatedIndex; |
|
56 |
protected short _constantValueIndex; |
|
57 |
protected short _codeIndex; |
|
58 |
protected short _exceptionsIndex; |
|
59 |
protected short _lineNumberTableIndex; |
|
60 |
protected short _localVariableTableIndex; |
|
61 |
protected short _signatureIndex; |
|
62 |
||
63 |
protected static int extractHighShortFromInt(int val) { |
|
5882 | 64 |
// must stay in sync with constantPoolOopDesc::name_and_type_at_put, method_at_put, etc. |
1 | 65 |
return (val >> 16) & 0xFFFF; |
66 |
} |
|
67 |
||
68 |
protected static int extractLowShortFromInt(int val) { |
|
5882 | 69 |
// must stay in sync with constantPoolOopDesc::name_and_type_at_put, method_at_put, etc. |
1 | 70 |
return val & 0xFFFF; |
71 |
} |
|
72 |
||
73 |
public ClassWriter(InstanceKlass kls, OutputStream os) { |
|
74 |
klass = kls; |
|
75 |
dos = new DataOutputStream(os); |
|
76 |
cpool = klass.getConstants(); |
|
77 |
} |
|
78 |
||
79 |
public void write() throws IOException { |
|
80 |
if (DEBUG) debugMessage("class name = " + klass.getName().asString()); |
|
81 |
||
82 |
// write magic |
|
83 |
dos.writeInt(0xCAFEBABE); |
|
84 |
||
2529 | 85 |
writeVersion(); |
1 | 86 |
writeConstantPool(); |
87 |
writeClassAccessFlags(); |
|
88 |
writeThisClass(); |
|
89 |
writeSuperClass(); |
|
90 |
writeInterfaces(); |
|
91 |
writeFields(); |
|
92 |
writeMethods(); |
|
93 |
writeClassAttributes(); |
|
94 |
||
95 |
// flush output |
|
96 |
dos.flush(); |
|
97 |
} |
|
98 |
||
2529 | 99 |
protected void writeVersion() throws IOException { |
100 |
dos.writeShort((short)klass.minorVersion()); |
|
101 |
dos.writeShort((short)klass.majorVersion()); |
|
1 | 102 |
} |
103 |
||
2529 | 104 |
protected void writeIndex(int index) throws IOException { |
105 |
if (index == 0) throw new InternalError(); |
|
106 |
dos.writeShort(index); |
|
1 | 107 |
} |
108 |
||
109 |
protected void writeConstantPool() throws IOException { |
|
110 |
final TypeArray tags = cpool.getTags(); |
|
111 |
final long len = tags.getLength(); |
|
112 |
dos.writeShort((short) len); |
|
113 |
||
114 |
if (DEBUG) debugMessage("constant pool length = " + len); |
|
115 |
||
116 |
int ci = 0; // constant pool index |
|
117 |
||
118 |
// collect all modified UTF-8 Strings from Constant Pool |
|
119 |
||
120 |
for (ci = 1; ci < len; ci++) { |
|
121 |
byte cpConstType = tags.getByteAt(ci); |
|
122 |
if(cpConstType == JVM_CONSTANT_Utf8) { |
|
123 |
Symbol sym = cpool.getSymbolAt(ci); |
|
124 |
utf8ToIndex.put(sym.asString(), new Short((short) ci)); |
|
125 |
} |
|
126 |
else if(cpConstType == JVM_CONSTANT_Long || |
|
127 |
cpConstType == JVM_CONSTANT_Double) { |
|
128 |
ci++; |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
// remember index of attribute name modified UTF-8 strings |
|
133 |
||
134 |
// class attributes |
|
135 |
Short sourceFileIndex = (Short) utf8ToIndex.get("SourceFile"); |
|
136 |
_sourceFileIndex = (sourceFileIndex != null)? sourceFileIndex.shortValue() : 0; |
|
137 |
if (DEBUG) debugMessage("SourceFile index = " + _sourceFileIndex); |
|
138 |
||
139 |
Short innerClassesIndex = (Short) utf8ToIndex.get("InnerClasses"); |
|
140 |
_innerClassesIndex = (innerClassesIndex != null)? innerClassesIndex.shortValue() : 0; |
|
141 |
if (DEBUG) debugMessage("InnerClasses index = " + _innerClassesIndex); |
|
142 |
||
143 |
// field attributes |
|
144 |
Short constantValueIndex = (Short) utf8ToIndex.get("ConstantValue"); |
|
145 |
_constantValueIndex = (constantValueIndex != null)? |
|
146 |
constantValueIndex.shortValue() : 0; |
|
147 |
if (DEBUG) debugMessage("ConstantValue index = " + _constantValueIndex); |
|
148 |
||
149 |
Short syntheticIndex = (Short) utf8ToIndex.get("Synthetic"); |
|
150 |
_syntheticIndex = (syntheticIndex != null)? syntheticIndex.shortValue() : 0; |
|
151 |
if (DEBUG) debugMessage("Synthetic index = " + _syntheticIndex); |
|
152 |
||
153 |
Short deprecatedIndex = (Short) utf8ToIndex.get("Deprecated"); |
|
154 |
_deprecatedIndex = (deprecatedIndex != null)? deprecatedIndex.shortValue() : 0; |
|
155 |
if (DEBUG) debugMessage("Deprecated index = " + _deprecatedIndex); |
|
156 |
||
157 |
// method attributes |
|
158 |
Short codeIndex = (Short) utf8ToIndex.get("Code"); |
|
159 |
_codeIndex = (codeIndex != null)? codeIndex.shortValue() : 0; |
|
160 |
if (DEBUG) debugMessage("Code index = " + _codeIndex); |
|
161 |
||
162 |
Short exceptionsIndex = (Short) utf8ToIndex.get("Exceptions"); |
|
163 |
_exceptionsIndex = (exceptionsIndex != null)? exceptionsIndex.shortValue() : 0; |
|
164 |
if (DEBUG) debugMessage("Exceptions index = " + _exceptionsIndex); |
|
165 |
||
166 |
// Short syntheticIndex = (Short) utf8ToIndex.get("Synthetic"); |
|
167 |
// Short deprecatedIndex = (Short) utf8ToIndex.get("Deprecated"); |
|
168 |
||
169 |
// Code attributes |
|
170 |
Short lineNumberTableIndex = (Short) utf8ToIndex.get("LineNumberTable"); |
|
171 |
_lineNumberTableIndex = (lineNumberTableIndex != null)? |
|
172 |
lineNumberTableIndex.shortValue() : 0; |
|
173 |
if (DEBUG) debugMessage("LineNumberTable index = " + _lineNumberTableIndex); |
|
174 |
||
175 |
Short localVariableTableIndex = (Short) utf8ToIndex.get("LocalVariableTable"); |
|
176 |
_localVariableTableIndex = (localVariableTableIndex != null)? |
|
177 |
localVariableTableIndex.shortValue() : 0; |
|
178 |
if (DEBUG) debugMessage("LocalVariableTable index = " + _localVariableTableIndex); |
|
179 |
||
180 |
Short signatureIdx = (Short) utf8ToIndex.get("Signature"); |
|
181 |
_signatureIndex = (signatureIdx != null)? signatureIdx.shortValue() : 0; |
|
182 |
if (DEBUG) debugMessage("Signature index = " + _signatureIndex); |
|
183 |
||
184 |
for(ci = 1; ci < len; ci++) { |
|
185 |
// write cp_info |
|
186 |
// write constant type |
|
187 |
byte cpConstType = tags.getByteAt(ci); |
|
188 |
switch(cpConstType) { |
|
189 |
case JVM_CONSTANT_Utf8: { |
|
190 |
dos.writeByte(cpConstType); |
|
191 |
Symbol sym = cpool.getSymbolAt(ci); |
|
192 |
dos.writeShort((short)sym.getLength()); |
|
193 |
dos.write(sym.asByteArray()); |
|
194 |
if (DEBUG) debugMessage("CP[" + ci + "] = modified UTF-8 " + sym.asString()); |
|
195 |
break; |
|
196 |
} |
|
197 |
||
198 |
case JVM_CONSTANT_Unicode: |
|
199 |
throw new IllegalArgumentException("Unicode constant!"); |
|
200 |
||
201 |
case JVM_CONSTANT_Integer: |
|
202 |
dos.writeByte(cpConstType); |
|
203 |
dos.writeInt(cpool.getIntAt(ci)); |
|
204 |
if (DEBUG) debugMessage("CP[" + ci + "] = int " + cpool.getIntAt(ci)); |
|
205 |
break; |
|
206 |
||
207 |
case JVM_CONSTANT_Float: |
|
208 |
dos.writeByte(cpConstType); |
|
209 |
dos.writeFloat(cpool.getFloatAt(ci)); |
|
210 |
if (DEBUG) debugMessage("CP[" + ci + "] = float " + cpool.getFloatAt(ci)); |
|
211 |
break; |
|
212 |
||
213 |
case JVM_CONSTANT_Long: { |
|
214 |
dos.writeByte(cpConstType); |
|
215 |
long l = cpool.getLongAt(ci); |
|
216 |
// long entries occupy two pool entries |
|
217 |
ci++; |
|
218 |
dos.writeLong(l); |
|
219 |
break; |
|
220 |
} |
|
221 |
||
222 |
case JVM_CONSTANT_Double: |
|
223 |
dos.writeByte(cpConstType); |
|
224 |
dos.writeDouble(cpool.getDoubleAt(ci)); |
|
225 |
// double entries occupy two pool entries |
|
226 |
ci++; |
|
227 |
break; |
|
228 |
||
229 |
case JVM_CONSTANT_Class: { |
|
230 |
dos.writeByte(cpConstType); |
|
231 |
// Klass already resolved. ConstantPool constains klassOop. |
|
232 |
Klass refKls = (Klass) cpool.getObjAt(ci); |
|
233 |
String klassName = refKls.getName().asString(); |
|
234 |
||
235 |
Short s = (Short) utf8ToIndex.get(klassName); |
|
236 |
classToIndex.put(klassName, new Short((short)ci)); |
|
237 |
dos.writeShort(s.shortValue()); |
|
238 |
if (DEBUG) debugMessage("CP[" + ci + "] = class " + s); |
|
239 |
break; |
|
240 |
} |
|
241 |
||
242 |
// case JVM_CONSTANT_ClassIndex: |
|
5341 | 243 |
case JVM_CONSTANT_UnresolvedClassInError: |
1 | 244 |
case JVM_CONSTANT_UnresolvedClass: { |
245 |
dos.writeByte(JVM_CONSTANT_Class); |
|
246 |
String klassName = cpool.getSymbolAt(ci).asString(); |
|
247 |
||
248 |
Short s = (Short) utf8ToIndex.get(klassName); |
|
249 |
classToIndex.put(klassName, new Short((short) ci)); |
|
250 |
||
251 |
dos.writeShort(s.shortValue()); |
|
252 |
if (DEBUG) debugMessage("CP[" + ci + "] = class " + s); |
|
253 |
break; |
|
254 |
} |
|
255 |
||
256 |
case JVM_CONSTANT_String: { |
|
257 |
dos.writeByte(cpConstType); |
|
258 |
String str = OopUtilities.stringOopToString(cpool.getObjAt(ci)); |
|
259 |
Short s = (Short) utf8ToIndex.get(str); |
|
260 |
dos.writeShort(s.shortValue()); |
|
261 |
if (DEBUG) debugMessage("CP[" + ci + "] = string " + s); |
|
262 |
break; |
|
263 |
} |
|
264 |
||
265 |
// case JVM_CONSTANT_StringIndex: |
|
266 |
case JVM_CONSTANT_UnresolvedString: { |
|
267 |
dos.writeByte(JVM_CONSTANT_String); |
|
268 |
String val = cpool.getSymbolAt(ci).asString(); |
|
269 |
||
270 |
Short s = (Short) utf8ToIndex.get(val); |
|
271 |
dos.writeShort(s.shortValue()); |
|
272 |
if (DEBUG) debugMessage("CP[" + ci + "] = string " + s); |
|
273 |
break; |
|
274 |
} |
|
275 |
||
276 |
// all external, internal method/field references |
|
277 |
case JVM_CONSTANT_Fieldref: |
|
278 |
case JVM_CONSTANT_Methodref: |
|
279 |
case JVM_CONSTANT_InterfaceMethodref: { |
|
280 |
dos.writeByte(cpConstType); |
|
281 |
int value = cpool.getIntAt(ci); |
|
282 |
short klassIndex = (short) extractLowShortFromInt(value); |
|
283 |
short nameAndTypeIndex = (short) extractHighShortFromInt(value); |
|
284 |
dos.writeShort(klassIndex); |
|
285 |
dos.writeShort(nameAndTypeIndex); |
|
286 |
if (DEBUG) debugMessage("CP[" + ci + "] = ref klass = " + |
|
287 |
klassIndex + ", N&T = " + nameAndTypeIndex); |
|
288 |
break; |
|
289 |
} |
|
290 |
||
291 |
case JVM_CONSTANT_NameAndType: { |
|
292 |
dos.writeByte(cpConstType); |
|
293 |
int value = cpool.getIntAt(ci); |
|
294 |
short nameIndex = (short) extractLowShortFromInt(value); |
|
295 |
short signatureIndex = (short) extractHighShortFromInt(value); |
|
296 |
dos.writeShort(nameIndex); |
|
297 |
dos.writeShort(signatureIndex); |
|
298 |
if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex |
|
299 |
+ ", type = " + signatureIndex); |
|
300 |
break; |
|
301 |
} |
|
5882 | 302 |
|
303 |
case JVM_CONSTANT_MethodHandle: { |
|
304 |
dos.writeByte(cpConstType); |
|
305 |
int value = cpool.getIntAt(ci); |
|
6062
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
306 |
short bootstrapMethodIndex = (short) extractLowShortFromInt(value); |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
307 |
short nameAndTypeIndex = (short) extractHighShortFromInt(value); |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
308 |
dos.writeShort(bootstrapMethodIndex); |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
309 |
dos.writeShort(nameAndTypeIndex); |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
310 |
if (DEBUG) debugMessage("CP[" + ci + "] = indy BSM = " + |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
311 |
bootstrapMethodIndex + ", N&T = " + nameAndTypeIndex); |
5882 | 312 |
break; |
313 |
} |
|
314 |
||
315 |
case JVM_CONSTANT_MethodType: { |
|
316 |
dos.writeByte(cpConstType); |
|
317 |
int value = cpool.getIntAt(ci); |
|
318 |
short refIndex = (short) value; |
|
319 |
dos.writeShort(refIndex); |
|
320 |
if (DEBUG) debugMessage("CP[" + ci + "] = MT index = " + refIndex); |
|
321 |
break; |
|
322 |
} |
|
323 |
||
6062
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
324 |
case JVM_CONSTANT_InvokeDynamic: { |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
325 |
dos.writeByte(cpConstType); |
7114
65d21c4c6337
6984311: JSR 292 needs optional bootstrap method parameters
jrose
parents:
6062
diff
changeset
|
326 |
int[] values = cpool.getMultiOperandsAt(ci); |
65d21c4c6337
6984311: JSR 292 needs optional bootstrap method parameters
jrose
parents:
6062
diff
changeset
|
327 |
for (int vn = 0; vn < values.length; vn++) { |
65d21c4c6337
6984311: JSR 292 needs optional bootstrap method parameters
jrose
parents:
6062
diff
changeset
|
328 |
dos.writeShort(values[vn]); |
65d21c4c6337
6984311: JSR 292 needs optional bootstrap method parameters
jrose
parents:
6062
diff
changeset
|
329 |
} |
65d21c4c6337
6984311: JSR 292 needs optional bootstrap method parameters
jrose
parents:
6062
diff
changeset
|
330 |
if (DEBUG) debugMessage("CP[" + ci + "] = INDY indexes = " + Arrays.toString(values)); |
6062
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
331 |
break; |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
332 |
} |
bab93afe9df7
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5882
diff
changeset
|
333 |
|
5341 | 334 |
default: |
335 |
throw new InternalError("Unknown tag: " + cpConstType); |
|
1 | 336 |
} // switch |
337 |
} |
|
338 |
} |
|
339 |
||
340 |
protected void writeClassAccessFlags() throws IOException { |
|
341 |
int flags = (int)(klass.getAccessFlags() & JVM_RECOGNIZED_CLASS_MODIFIERS); |
|
342 |
dos.writeShort((short)flags); |
|
343 |
} |
|
344 |
||
345 |
protected void writeThisClass() throws IOException { |
|
346 |
String klassName = klass.getName().asString(); |
|
347 |
Short index = (Short) classToIndex.get(klassName); |
|
348 |
dos.writeShort(index.shortValue()); |
|
349 |
if (DEBUG) debugMessage("this class = " + index); |
|
350 |
} |
|
351 |
||
352 |
protected void writeSuperClass() throws IOException { |
|
353 |
Klass superKlass = klass.getSuper(); |
|
354 |
if (superKlass != null) { // is not java.lang.Object |
|
355 |
String superName = superKlass.getName().asString(); |
|
356 |
Short index = (Short) classToIndex.get(superName); |
|
357 |
if (DEBUG) debugMessage("super class = " + index); |
|
358 |
dos.writeShort(index.shortValue()); |
|
359 |
} else { |
|
360 |
dos.writeShort(0); // no super class |
|
361 |
} |
|
362 |
} |
|
363 |
protected void writeInterfaces() throws IOException { |
|
364 |
ObjArray interfaces = klass.getLocalInterfaces(); |
|
365 |
final int len = (int) interfaces.getLength(); |
|
366 |
||
367 |
if (DEBUG) debugMessage("number of interfaces = " + len); |
|
368 |
||
369 |
// write interfaces count |
|
370 |
dos.writeShort((short) len); |
|
371 |
for (int i = 0; i < len; i++) { |
|
372 |
Klass k = (Klass) interfaces.getObjAt(i); |
|
373 |
Short index = (Short) classToIndex.get(k.getName().asString()); |
|
374 |
dos.writeShort(index.shortValue()); |
|
375 |
if (DEBUG) debugMessage("\t" + index); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
protected void writeFields() throws IOException { |
|
380 |
TypeArray fields = klass.getFields(); |
|
381 |
final int length = (int) fields.getLength(); |
|
382 |
||
383 |
// write number of fields |
|
384 |
dos.writeShort((short) (length / InstanceKlass.NEXT_OFFSET) ); |
|
385 |
||
386 |
if (DEBUG) debugMessage("number of fields = " |
|
387 |
+ length/InstanceKlass.NEXT_OFFSET); |
|
388 |
||
389 |
for (int index = 0; index < length; index += InstanceKlass.NEXT_OFFSET) { |
|
390 |
short accessFlags = fields.getShortAt(index + InstanceKlass.ACCESS_FLAGS_OFFSET); |
|
391 |
dos.writeShort(accessFlags & (short) JVM_RECOGNIZED_FIELD_MODIFIERS); |
|
392 |
||
393 |
short nameIndex = fields.getShortAt(index + InstanceKlass.NAME_INDEX_OFFSET); |
|
394 |
dos.writeShort(nameIndex); |
|
395 |
||
396 |
short signatureIndex = fields.getShortAt(index + InstanceKlass.SIGNATURE_INDEX_OFFSET); |
|
397 |
dos.writeShort(signatureIndex); |
|
398 |
if (DEBUG) debugMessage("\tfield name = " + nameIndex + ", signature = " + signatureIndex); |
|
399 |
||
400 |
short fieldAttributeCount = 0; |
|
2529 | 401 |
boolean hasSyn = hasSyntheticAttribute(accessFlags); |
402 |
if (hasSyn) |
|
1 | 403 |
fieldAttributeCount++; |
404 |
||
405 |
short initvalIndex = fields.getShortAt(index + InstanceKlass.INITVAL_INDEX_OFFSET); |
|
406 |
if (initvalIndex != 0) |
|
407 |
fieldAttributeCount++; |
|
408 |
||
409 |
short genSigIndex = fields.getShortAt(index + InstanceKlass.GENERIC_SIGNATURE_INDEX_OFFSET); |
|
410 |
if (genSigIndex != 0) |
|
411 |
fieldAttributeCount++; |
|
412 |
||
413 |
dos.writeShort(fieldAttributeCount); |
|
414 |
||
415 |
// write synthetic, if applicable |
|
2529 | 416 |
if (hasSyn) |
1 | 417 |
writeSynthetic(); |
418 |
||
419 |
if (initvalIndex != 0) { |
|
2529 | 420 |
writeIndex(_constantValueIndex); |
1 | 421 |
dos.writeInt(2); |
422 |
dos.writeShort(initvalIndex); |
|
423 |
if (DEBUG) debugMessage("\tfield init value = " + initvalIndex); |
|
424 |
} |
|
425 |
||
426 |
if (genSigIndex != 0) { |
|
2529 | 427 |
writeIndex(_signatureIndex); |
1 | 428 |
dos.writeInt(2); |
429 |
dos.writeShort(genSigIndex); |
|
430 |
if (DEBUG) debugMessage("\tfield generic signature index " + genSigIndex); |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
protected boolean isSynthetic(short accessFlags) { |
|
436 |
return (accessFlags & (short) JVM_ACC_SYNTHETIC) != 0; |
|
437 |
} |
|
438 |
||
2529 | 439 |
protected boolean hasSyntheticAttribute(short accessFlags) { |
440 |
// Check if flags have the attribute and if the constant pool contains an entry for it. |
|
441 |
return isSynthetic(accessFlags) && _syntheticIndex != 0; |
|
442 |
} |
|
443 |
||
1 | 444 |
protected void writeSynthetic() throws IOException { |
2529 | 445 |
writeIndex(_syntheticIndex); |
1 | 446 |
dos.writeInt(0); |
447 |
} |
|
448 |
||
449 |
protected void writeMethods() throws IOException { |
|
450 |
ObjArray methods = klass.getMethods(); |
|
451 |
final int len = (int) methods.getLength(); |
|
452 |
// write number of methods |
|
453 |
dos.writeShort((short) len); |
|
454 |
if (DEBUG) debugMessage("number of methods = " + len); |
|
455 |
for (int m = 0; m < len; m++) { |
|
456 |
writeMethod((Method) methods.getObjAt(m)); |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
protected void writeMethod(Method m) throws IOException { |
|
461 |
long accessFlags = m.getAccessFlags(); |
|
462 |
dos.writeShort((short) (accessFlags & JVM_RECOGNIZED_METHOD_MODIFIERS)); |
|
463 |
dos.writeShort((short) m.getNameIndex()); |
|
464 |
dos.writeShort((short) m.getSignatureIndex()); |
|
465 |
if (DEBUG) debugMessage("\tmethod name = " + m.getNameIndex() + ", signature = " |
|
466 |
+ m.getSignatureIndex()); |
|
467 |
||
468 |
final boolean isNative = ((accessFlags & JVM_ACC_NATIVE) != 0); |
|
469 |
final boolean isAbstract = ((accessFlags & JVM_ACC_ABSTRACT) != 0); |
|
470 |
||
471 |
short methodAttributeCount = 0; |
|
472 |
||
2529 | 473 |
final boolean hasSyn = hasSyntheticAttribute((short)accessFlags); |
474 |
if (hasSyn) |
|
1 | 475 |
methodAttributeCount++; |
476 |
||
477 |
final boolean hasCheckedExceptions = m.hasCheckedExceptions(); |
|
478 |
if (hasCheckedExceptions) |
|
479 |
methodAttributeCount++; |
|
480 |
||
481 |
final boolean isCodeAvailable = (!isNative) && (!isAbstract); |
|
482 |
if (isCodeAvailable) |
|
483 |
methodAttributeCount++; |
|
484 |
||
485 |
final boolean isGeneric = (m.getGenericSignature() != null); |
|
486 |
if (isGeneric) |
|
487 |
methodAttributeCount++; |
|
488 |
||
489 |
dos.writeShort(methodAttributeCount); |
|
490 |
if (DEBUG) debugMessage("\tmethod attribute count = " + methodAttributeCount); |
|
491 |
||
2529 | 492 |
if (hasSyn) { |
1 | 493 |
if (DEBUG) debugMessage("\tmethod is synthetic"); |
494 |
writeSynthetic(); |
|
495 |
} |
|
496 |
||
497 |
if (isCodeAvailable) { |
|
498 |
byte[] code = m.getByteCode(); |
|
499 |
short codeAttrCount = 0; |
|
500 |
int codeSize = 2 /* max_stack */ + |
|
501 |
2 /* max_locals */ + |
|
502 |
4 /* code_length */ + |
|
503 |
code.length /* code */ + |
|
504 |
2 /* exp. table len. */ + |
|
505 |
2 /* code attr. count */; |
|
506 |
||
507 |
TypeArray exceptionTable = m.getExceptionTable(); |
|
508 |
final int exceptionTableLen = (int) exceptionTable.getLength(); |
|
509 |
if (exceptionTableLen != 0) { |
|
510 |
if (DEBUG) debugMessage("\tmethod has exception table"); |
|
511 |
codeSize += (exceptionTableLen / 4) /* exception table is 4-tuple array */ |
|
512 |
* (2 /* start_pc */ + |
|
513 |
2 /* end_pc */ + |
|
514 |
2 /* handler_pc */ + |
|
515 |
2 /* catch_type */); |
|
516 |
} |
|
517 |
||
518 |
boolean hasLineNumberTable = m.hasLineNumberTable(); |
|
519 |
LineNumberTableElement[] lineNumberTable = null; |
|
520 |
int lineNumberAttrLen = 0; |
|
521 |
||
522 |
if (hasLineNumberTable) { |
|
523 |
if (DEBUG) debugMessage("\tmethod has line number table"); |
|
524 |
lineNumberTable = m.getLineNumberTable(); |
|
525 |
if (DEBUG) debugMessage("\t\tline table length = " + lineNumberTable.length); |
|
526 |
||
527 |
lineNumberAttrLen = 2 /* line number table length */ + |
|
528 |
lineNumberTable.length * (2 /* start_pc */ + 2 /* line_number */); |
|
529 |
||
530 |
codeSize += 2 /* line number table attr index */ + |
|
531 |
4 /* line number table attr length */ + |
|
532 |
lineNumberAttrLen; |
|
533 |
||
534 |
if (DEBUG) debugMessage("\t\tline number table attr size = " + |
|
535 |
lineNumberAttrLen); |
|
536 |
||
537 |
codeAttrCount++; |
|
538 |
} |
|
539 |
||
540 |
boolean hasLocalVariableTable = m.hasLocalVariableTable(); |
|
541 |
LocalVariableTableElement[] localVariableTable = null; |
|
542 |
int localVarAttrLen = 0; |
|
543 |
||
544 |
if (hasLocalVariableTable) { |
|
545 |
if (DEBUG) debugMessage("\tmethod has local variable table"); |
|
546 |
localVariableTable = m.getLocalVariableTable(); |
|
547 |
if (DEBUG) debugMessage("\t\tlocal variable table length = " |
|
548 |
+ localVariableTable.length); |
|
549 |
localVarAttrLen = |
|
550 |
2 /* local variable table length */ + |
|
551 |
localVariableTable.length * ( 2 /* start_pc */ + |
|
552 |
2 /* length */ + |
|
553 |
2 /* name_index */ + |
|
554 |
2 /* signature_index */ + |
|
555 |
2 /* variable index */ ); |
|
556 |
||
557 |
if (DEBUG) debugMessage("\t\tlocal variable attr size = " + |
|
558 |
localVarAttrLen); |
|
559 |
||
560 |
codeSize += 2 /* local variable table attr index */ + |
|
561 |
4 /* local variable table attr length */ + |
|
562 |
localVarAttrLen; |
|
563 |
||
564 |
codeAttrCount++; |
|
565 |
} |
|
566 |
||
567 |
// fix ConstantPoolCache indices to ConstantPool indices. |
|
568 |
rewriteByteCode(m, code); |
|
569 |
||
570 |
// start writing Code |
|
571 |
||
2529 | 572 |
writeIndex(_codeIndex); |
1 | 573 |
|
574 |
dos.writeInt(codeSize); |
|
575 |
if (DEBUG) debugMessage("\tcode attribute length = " + codeSize); |
|
576 |
||
577 |
dos.writeShort((short) m.getMaxStack()); |
|
578 |
if (DEBUG) debugMessage("\tmax stack = " + m.getMaxStack()); |
|
579 |
||
580 |
dos.writeShort((short) m.getMaxLocals()); |
|
581 |
if (DEBUG) debugMessage("\tmax locals = " + m.getMaxLocals()); |
|
582 |
||
583 |
dos.writeInt(code.length); |
|
584 |
if (DEBUG) debugMessage("\tcode size = " + code.length); |
|
585 |
||
586 |
dos.write(code); |
|
587 |
||
588 |
// write exception table size |
|
589 |
dos.writeShort((short) (exceptionTableLen / 4)); |
|
590 |
if (DEBUG) debugMessage("\texception table length = " + (exceptionTableLen / 4)); |
|
591 |
||
592 |
if (exceptionTableLen != 0) { |
|
593 |
for (int e = 0; e < exceptionTableLen; e += 4) { |
|
594 |
dos.writeShort((short) exceptionTable.getIntAt(e)); |
|
595 |
dos.writeShort((short) exceptionTable.getIntAt(e + 1)); |
|
596 |
dos.writeShort((short) exceptionTable.getIntAt(e + 2)); |
|
597 |
dos.writeShort((short) exceptionTable.getIntAt(e + 3)); |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
dos.writeShort((short)codeAttrCount); |
|
602 |
if (DEBUG) debugMessage("\tcode attribute count = " + codeAttrCount); |
|
603 |
||
604 |
// write LineNumberTable, if available. |
|
605 |
if (hasLineNumberTable) { |
|
2529 | 606 |
writeIndex(_lineNumberTableIndex); |
1 | 607 |
dos.writeInt(lineNumberAttrLen); |
608 |
dos.writeShort((short) lineNumberTable.length); |
|
609 |
for (int l = 0; l < lineNumberTable.length; l++) { |
|
610 |
dos.writeShort((short) lineNumberTable[l].getStartBCI()); |
|
611 |
dos.writeShort((short) lineNumberTable[l].getLineNumber()); |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
// write LocalVariableTable, if available. |
|
616 |
if (hasLocalVariableTable) { |
|
2529 | 617 |
writeIndex((short) _localVariableTableIndex); |
1 | 618 |
dos.writeInt(localVarAttrLen); |
619 |
dos.writeShort((short) localVariableTable.length); |
|
620 |
for (int l = 0; l < localVariableTable.length; l++) { |
|
621 |
dos.writeShort((short) localVariableTable[l].getStartBCI()); |
|
622 |
dos.writeShort((short) localVariableTable[l].getLength()); |
|
623 |
dos.writeShort((short) localVariableTable[l].getNameCPIndex()); |
|
624 |
dos.writeShort((short) localVariableTable[l].getDescriptorCPIndex()); |
|
625 |
dos.writeShort((short) localVariableTable[l].getSlot()); |
|
626 |
} |
|
627 |
} |
|
628 |
} |
|
629 |
||
2529 | 630 |
if (hasCheckedExceptions) { |
631 |
CheckedExceptionElement[] exceptions = m.getCheckedExceptions(); |
|
632 |
writeIndex(_exceptionsIndex); |
|
633 |
||
634 |
int attrSize = 2 /* number_of_exceptions */ + |
|
635 |
exceptions.length * 2 /* exception_index */; |
|
636 |
dos.writeInt(attrSize); |
|
637 |
dos.writeShort(exceptions.length); |
|
638 |
if (DEBUG) debugMessage("\tmethod has " + exceptions.length |
|
639 |
+ " checked exception(s)"); |
|
640 |
for (int e = 0; e < exceptions.length; e++) { |
|
641 |
short cpIndex = (short) exceptions[e].getClassCPIndex(); |
|
642 |
dos.writeShort(cpIndex); |
|
643 |
} |
|
644 |
} |
|
645 |
||
1 | 646 |
if (isGeneric) { |
647 |
writeGenericSignature(m.getGenericSignature().asString()); |
|
648 |
} |
|
649 |
} |
|
650 |
||
651 |
protected void rewriteByteCode(Method m, byte[] code) { |
|
652 |
ByteCodeRewriter r = new ByteCodeRewriter(m, cpool, code); |
|
653 |
r.rewrite(); |
|
654 |
} |
|
655 |
||
656 |
protected void writeGenericSignature(String signature) throws IOException { |
|
2529 | 657 |
writeIndex(_signatureIndex); |
1 | 658 |
if (DEBUG) debugMessage("signature attribute = " + _signatureIndex); |
659 |
dos.writeInt(2); |
|
660 |
Short index = (Short) utf8ToIndex.get(signature); |
|
661 |
dos.writeShort(index.shortValue()); |
|
662 |
if (DEBUG) debugMessage("generic signature = " + index); |
|
663 |
} |
|
664 |
||
665 |
protected void writeClassAttributes() throws IOException { |
|
666 |
final long flags = klass.getAccessFlags(); |
|
2529 | 667 |
final boolean hasSyn = hasSyntheticAttribute((short) flags); |
1 | 668 |
|
669 |
// check for source file |
|
670 |
short classAttributeCount = 0; |
|
671 |
||
2529 | 672 |
if (hasSyn) |
1 | 673 |
classAttributeCount++; |
674 |
||
675 |
Symbol sourceFileName = klass.getSourceFileName(); |
|
676 |
if (sourceFileName != null) |
|
677 |
classAttributeCount++; |
|
678 |
||
679 |
Symbol genericSignature = klass.getGenericSignature(); |
|
680 |
if (genericSignature != null) |
|
681 |
classAttributeCount++; |
|
682 |
||
683 |
TypeArray innerClasses = klass.getInnerClasses(); |
|
684 |
final int numInnerClasses = (int) (innerClasses.getLength() / 4); |
|
685 |
if (numInnerClasses != 0) |
|
686 |
classAttributeCount++; |
|
687 |
||
688 |
dos.writeShort(classAttributeCount); |
|
689 |
if (DEBUG) debugMessage("class attribute count = " + classAttributeCount); |
|
690 |
||
2529 | 691 |
if (hasSyn) |
1 | 692 |
writeSynthetic(); |
693 |
||
694 |
// write SourceFile, if any |
|
695 |
if (sourceFileName != null) { |
|
2529 | 696 |
writeIndex(_sourceFileIndex); |
1 | 697 |
if (DEBUG) debugMessage("source file attribute = " + _sourceFileIndex); |
698 |
dos.writeInt(2); |
|
699 |
Short index = (Short) utf8ToIndex.get(sourceFileName.asString()); |
|
700 |
dos.writeShort(index.shortValue()); |
|
701 |
if (DEBUG) debugMessage("source file name = " + index); |
|
702 |
} |
|
703 |
||
704 |
// write Signature, if any |
|
705 |
if (genericSignature != null) { |
|
706 |
writeGenericSignature(genericSignature.asString()); |
|
707 |
} |
|
708 |
||
709 |
// write inner classes, if any |
|
710 |
if (numInnerClasses != 0) { |
|
2529 | 711 |
writeIndex(_innerClassesIndex); |
1 | 712 |
final int innerAttrLen = 2 /* number_of_inner_classes */ + |
713 |
numInnerClasses * ( |
|
714 |
2 /* inner_class_info_index */ + |
|
715 |
2 /* outer_class_info_index */ + |
|
716 |
2 /* inner_class_name_index */ + |
|
717 |
2 /* inner_class_access_flags */); |
|
718 |
dos.writeInt(innerAttrLen); |
|
719 |
||
720 |
dos.writeShort(numInnerClasses); |
|
721 |
if (DEBUG) debugMessage("class has " + numInnerClasses + " inner class entries"); |
|
722 |
||
723 |
for (int index = 0; index < numInnerClasses * 4; index++) { |
|
724 |
dos.writeShort(innerClasses.getShortAt(index)); |
|
725 |
} |
|
726 |
} |
|
727 |
} |
|
728 |
} |