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