comparison agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassWriter.java @ 0:a61af66fc99e jdk7-b24

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