0
|
1 /*
|
1385
|
2 * Copyright 2002-2010 Sun Microsystems, Inc. All Rights Reserved.
|
0
|
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:
|
1385
|
241 case JVM_CONSTANT_UnresolvedClassInError:
|
0
|
242 case JVM_CONSTANT_UnresolvedClass: {
|
|
243 dos.writeByte(JVM_CONSTANT_Class);
|
|
244 String klassName = cpool.getSymbolAt(ci).asString();
|
|
245
|
|
246 Short s = (Short) utf8ToIndex.get(klassName);
|
|
247 classToIndex.put(klassName, new Short((short) ci));
|
|
248
|
|
249 dos.writeShort(s.shortValue());
|
|
250 if (DEBUG) debugMessage("CP[" + ci + "] = class " + s);
|
|
251 break;
|
|
252 }
|
|
253
|
|
254 case JVM_CONSTANT_String: {
|
|
255 dos.writeByte(cpConstType);
|
|
256 String str = OopUtilities.stringOopToString(cpool.getObjAt(ci));
|
|
257 Short s = (Short) utf8ToIndex.get(str);
|
|
258 dos.writeShort(s.shortValue());
|
|
259 if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
|
|
260 break;
|
|
261 }
|
|
262
|
|
263 // case JVM_CONSTANT_StringIndex:
|
|
264 case JVM_CONSTANT_UnresolvedString: {
|
|
265 dos.writeByte(JVM_CONSTANT_String);
|
|
266 String val = cpool.getSymbolAt(ci).asString();
|
|
267
|
|
268 Short s = (Short) utf8ToIndex.get(val);
|
|
269 dos.writeShort(s.shortValue());
|
|
270 if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
|
|
271 break;
|
|
272 }
|
|
273
|
|
274 // all external, internal method/field references
|
|
275 case JVM_CONSTANT_Fieldref:
|
|
276 case JVM_CONSTANT_Methodref:
|
|
277 case JVM_CONSTANT_InterfaceMethodref: {
|
|
278 dos.writeByte(cpConstType);
|
|
279 int value = cpool.getIntAt(ci);
|
|
280 short klassIndex = (short) extractLowShortFromInt(value);
|
|
281 short nameAndTypeIndex = (short) extractHighShortFromInt(value);
|
|
282 dos.writeShort(klassIndex);
|
|
283 dos.writeShort(nameAndTypeIndex);
|
|
284 if (DEBUG) debugMessage("CP[" + ci + "] = ref klass = " +
|
|
285 klassIndex + ", N&T = " + nameAndTypeIndex);
|
|
286 break;
|
|
287 }
|
|
288
|
|
289 case JVM_CONSTANT_NameAndType: {
|
|
290 dos.writeByte(cpConstType);
|
|
291 int value = cpool.getIntAt(ci);
|
|
292 short nameIndex = (short) extractLowShortFromInt(value);
|
|
293 short signatureIndex = (short) extractHighShortFromInt(value);
|
|
294 dos.writeShort(nameIndex);
|
|
295 dos.writeShort(signatureIndex);
|
|
296 if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex
|
|
297 + ", type = " + signatureIndex);
|
|
298 break;
|
|
299 }
|
1385
|
300 default:
|
|
301 throw new InternalError("Unknown tag: " + cpConstType);
|
0
|
302 } // switch
|
|
303 }
|
|
304 }
|
|
305
|
|
306 protected void writeClassAccessFlags() throws IOException {
|
|
307 int flags = (int)(klass.getAccessFlags() & JVM_RECOGNIZED_CLASS_MODIFIERS);
|
|
308 dos.writeShort((short)flags);
|
|
309 }
|
|
310
|
|
311 protected void writeThisClass() throws IOException {
|
|
312 String klassName = klass.getName().asString();
|
|
313 Short index = (Short) classToIndex.get(klassName);
|
|
314 dos.writeShort(index.shortValue());
|
|
315 if (DEBUG) debugMessage("this class = " + index);
|
|
316 }
|
|
317
|
|
318 protected void writeSuperClass() throws IOException {
|
|
319 Klass superKlass = klass.getSuper();
|
|
320 if (superKlass != null) { // is not java.lang.Object
|
|
321 String superName = superKlass.getName().asString();
|
|
322 Short index = (Short) classToIndex.get(superName);
|
|
323 if (DEBUG) debugMessage("super class = " + index);
|
|
324 dos.writeShort(index.shortValue());
|
|
325 } else {
|
|
326 dos.writeShort(0); // no super class
|
|
327 }
|
|
328 }
|
|
329 protected void writeInterfaces() throws IOException {
|
|
330 ObjArray interfaces = klass.getLocalInterfaces();
|
|
331 final int len = (int) interfaces.getLength();
|
|
332
|
|
333 if (DEBUG) debugMessage("number of interfaces = " + len);
|
|
334
|
|
335 // write interfaces count
|
|
336 dos.writeShort((short) len);
|
|
337 for (int i = 0; i < len; i++) {
|
|
338 Klass k = (Klass) interfaces.getObjAt(i);
|
|
339 Short index = (Short) classToIndex.get(k.getName().asString());
|
|
340 dos.writeShort(index.shortValue());
|
|
341 if (DEBUG) debugMessage("\t" + index);
|
|
342 }
|
|
343 }
|
|
344
|
|
345 protected void writeFields() throws IOException {
|
|
346 TypeArray fields = klass.getFields();
|
|
347 final int length = (int) fields.getLength();
|
|
348
|
|
349 // write number of fields
|
|
350 dos.writeShort((short) (length / InstanceKlass.NEXT_OFFSET) );
|
|
351
|
|
352 if (DEBUG) debugMessage("number of fields = "
|
|
353 + length/InstanceKlass.NEXT_OFFSET);
|
|
354
|
|
355 for (int index = 0; index < length; index += InstanceKlass.NEXT_OFFSET) {
|
|
356 short accessFlags = fields.getShortAt(index + InstanceKlass.ACCESS_FLAGS_OFFSET);
|
|
357 dos.writeShort(accessFlags & (short) JVM_RECOGNIZED_FIELD_MODIFIERS);
|
|
358
|
|
359 short nameIndex = fields.getShortAt(index + InstanceKlass.NAME_INDEX_OFFSET);
|
|
360 dos.writeShort(nameIndex);
|
|
361
|
|
362 short signatureIndex = fields.getShortAt(index + InstanceKlass.SIGNATURE_INDEX_OFFSET);
|
|
363 dos.writeShort(signatureIndex);
|
|
364 if (DEBUG) debugMessage("\tfield name = " + nameIndex + ", signature = " + signatureIndex);
|
|
365
|
|
366 short fieldAttributeCount = 0;
|
705
|
367 boolean hasSyn = hasSyntheticAttribute(accessFlags);
|
|
368 if (hasSyn)
|
0
|
369 fieldAttributeCount++;
|
|
370
|
|
371 short initvalIndex = fields.getShortAt(index + InstanceKlass.INITVAL_INDEX_OFFSET);
|
|
372 if (initvalIndex != 0)
|
|
373 fieldAttributeCount++;
|
|
374
|
|
375 short genSigIndex = fields.getShortAt(index + InstanceKlass.GENERIC_SIGNATURE_INDEX_OFFSET);
|
|
376 if (genSigIndex != 0)
|
|
377 fieldAttributeCount++;
|
|
378
|
|
379 dos.writeShort(fieldAttributeCount);
|
|
380
|
|
381 // write synthetic, if applicable
|
705
|
382 if (hasSyn)
|
0
|
383 writeSynthetic();
|
|
384
|
|
385 if (initvalIndex != 0) {
|
705
|
386 writeIndex(_constantValueIndex);
|
0
|
387 dos.writeInt(2);
|
|
388 dos.writeShort(initvalIndex);
|
|
389 if (DEBUG) debugMessage("\tfield init value = " + initvalIndex);
|
|
390 }
|
|
391
|
|
392 if (genSigIndex != 0) {
|
705
|
393 writeIndex(_signatureIndex);
|
0
|
394 dos.writeInt(2);
|
|
395 dos.writeShort(genSigIndex);
|
|
396 if (DEBUG) debugMessage("\tfield generic signature index " + genSigIndex);
|
|
397 }
|
|
398 }
|
|
399 }
|
|
400
|
|
401 protected boolean isSynthetic(short accessFlags) {
|
|
402 return (accessFlags & (short) JVM_ACC_SYNTHETIC) != 0;
|
|
403 }
|
|
404
|
705
|
405 protected boolean hasSyntheticAttribute(short accessFlags) {
|
|
406 // Check if flags have the attribute and if the constant pool contains an entry for it.
|
|
407 return isSynthetic(accessFlags) && _syntheticIndex != 0;
|
|
408 }
|
|
409
|
0
|
410 protected void writeSynthetic() throws IOException {
|
705
|
411 writeIndex(_syntheticIndex);
|
0
|
412 dos.writeInt(0);
|
|
413 }
|
|
414
|
|
415 protected void writeMethods() throws IOException {
|
|
416 ObjArray methods = klass.getMethods();
|
|
417 final int len = (int) methods.getLength();
|
|
418 // write number of methods
|
|
419 dos.writeShort((short) len);
|
|
420 if (DEBUG) debugMessage("number of methods = " + len);
|
|
421 for (int m = 0; m < len; m++) {
|
|
422 writeMethod((Method) methods.getObjAt(m));
|
|
423 }
|
|
424 }
|
|
425
|
|
426 protected void writeMethod(Method m) throws IOException {
|
|
427 long accessFlags = m.getAccessFlags();
|
|
428 dos.writeShort((short) (accessFlags & JVM_RECOGNIZED_METHOD_MODIFIERS));
|
|
429 dos.writeShort((short) m.getNameIndex());
|
|
430 dos.writeShort((short) m.getSignatureIndex());
|
|
431 if (DEBUG) debugMessage("\tmethod name = " + m.getNameIndex() + ", signature = "
|
|
432 + m.getSignatureIndex());
|
|
433
|
|
434 final boolean isNative = ((accessFlags & JVM_ACC_NATIVE) != 0);
|
|
435 final boolean isAbstract = ((accessFlags & JVM_ACC_ABSTRACT) != 0);
|
|
436
|
|
437 short methodAttributeCount = 0;
|
|
438
|
705
|
439 final boolean hasSyn = hasSyntheticAttribute((short)accessFlags);
|
|
440 if (hasSyn)
|
0
|
441 methodAttributeCount++;
|
|
442
|
|
443 final boolean hasCheckedExceptions = m.hasCheckedExceptions();
|
|
444 if (hasCheckedExceptions)
|
|
445 methodAttributeCount++;
|
|
446
|
|
447 final boolean isCodeAvailable = (!isNative) && (!isAbstract);
|
|
448 if (isCodeAvailable)
|
|
449 methodAttributeCount++;
|
|
450
|
|
451 final boolean isGeneric = (m.getGenericSignature() != null);
|
|
452 if (isGeneric)
|
|
453 methodAttributeCount++;
|
|
454
|
|
455 dos.writeShort(methodAttributeCount);
|
|
456 if (DEBUG) debugMessage("\tmethod attribute count = " + methodAttributeCount);
|
|
457
|
705
|
458 if (hasSyn) {
|
0
|
459 if (DEBUG) debugMessage("\tmethod is synthetic");
|
|
460 writeSynthetic();
|
|
461 }
|
|
462
|
|
463 if (isCodeAvailable) {
|
|
464 byte[] code = m.getByteCode();
|
|
465 short codeAttrCount = 0;
|
|
466 int codeSize = 2 /* max_stack */ +
|
|
467 2 /* max_locals */ +
|
|
468 4 /* code_length */ +
|
|
469 code.length /* code */ +
|
|
470 2 /* exp. table len. */ +
|
|
471 2 /* code attr. count */;
|
|
472
|
|
473 TypeArray exceptionTable = m.getExceptionTable();
|
|
474 final int exceptionTableLen = (int) exceptionTable.getLength();
|
|
475 if (exceptionTableLen != 0) {
|
|
476 if (DEBUG) debugMessage("\tmethod has exception table");
|
|
477 codeSize += (exceptionTableLen / 4) /* exception table is 4-tuple array */
|
|
478 * (2 /* start_pc */ +
|
|
479 2 /* end_pc */ +
|
|
480 2 /* handler_pc */ +
|
|
481 2 /* catch_type */);
|
|
482 }
|
|
483
|
|
484 boolean hasLineNumberTable = m.hasLineNumberTable();
|
|
485 LineNumberTableElement[] lineNumberTable = null;
|
|
486 int lineNumberAttrLen = 0;
|
|
487
|
|
488 if (hasLineNumberTable) {
|
|
489 if (DEBUG) debugMessage("\tmethod has line number table");
|
|
490 lineNumberTable = m.getLineNumberTable();
|
|
491 if (DEBUG) debugMessage("\t\tline table length = " + lineNumberTable.length);
|
|
492
|
|
493 lineNumberAttrLen = 2 /* line number table length */ +
|
|
494 lineNumberTable.length * (2 /* start_pc */ + 2 /* line_number */);
|
|
495
|
|
496 codeSize += 2 /* line number table attr index */ +
|
|
497 4 /* line number table attr length */ +
|
|
498 lineNumberAttrLen;
|
|
499
|
|
500 if (DEBUG) debugMessage("\t\tline number table attr size = " +
|
|
501 lineNumberAttrLen);
|
|
502
|
|
503 codeAttrCount++;
|
|
504 }
|
|
505
|
|
506 boolean hasLocalVariableTable = m.hasLocalVariableTable();
|
|
507 LocalVariableTableElement[] localVariableTable = null;
|
|
508 int localVarAttrLen = 0;
|
|
509
|
|
510 if (hasLocalVariableTable) {
|
|
511 if (DEBUG) debugMessage("\tmethod has local variable table");
|
|
512 localVariableTable = m.getLocalVariableTable();
|
|
513 if (DEBUG) debugMessage("\t\tlocal variable table length = "
|
|
514 + localVariableTable.length);
|
|
515 localVarAttrLen =
|
|
516 2 /* local variable table length */ +
|
|
517 localVariableTable.length * ( 2 /* start_pc */ +
|
|
518 2 /* length */ +
|
|
519 2 /* name_index */ +
|
|
520 2 /* signature_index */ +
|
|
521 2 /* variable index */ );
|
|
522
|
|
523 if (DEBUG) debugMessage("\t\tlocal variable attr size = " +
|
|
524 localVarAttrLen);
|
|
525
|
|
526 codeSize += 2 /* local variable table attr index */ +
|
|
527 4 /* local variable table attr length */ +
|
|
528 localVarAttrLen;
|
|
529
|
|
530 codeAttrCount++;
|
|
531 }
|
|
532
|
|
533 // fix ConstantPoolCache indices to ConstantPool indices.
|
|
534 rewriteByteCode(m, code);
|
|
535
|
|
536 // start writing Code
|
|
537
|
705
|
538 writeIndex(_codeIndex);
|
0
|
539
|
|
540 dos.writeInt(codeSize);
|
|
541 if (DEBUG) debugMessage("\tcode attribute length = " + codeSize);
|
|
542
|
|
543 dos.writeShort((short) m.getMaxStack());
|
|
544 if (DEBUG) debugMessage("\tmax stack = " + m.getMaxStack());
|
|
545
|
|
546 dos.writeShort((short) m.getMaxLocals());
|
|
547 if (DEBUG) debugMessage("\tmax locals = " + m.getMaxLocals());
|
|
548
|
|
549 dos.writeInt(code.length);
|
|
550 if (DEBUG) debugMessage("\tcode size = " + code.length);
|
|
551
|
|
552 dos.write(code);
|
|
553
|
|
554 // write exception table size
|
|
555 dos.writeShort((short) (exceptionTableLen / 4));
|
|
556 if (DEBUG) debugMessage("\texception table length = " + (exceptionTableLen / 4));
|
|
557
|
|
558 if (exceptionTableLen != 0) {
|
|
559 for (int e = 0; e < exceptionTableLen; e += 4) {
|
|
560 dos.writeShort((short) exceptionTable.getIntAt(e));
|
|
561 dos.writeShort((short) exceptionTable.getIntAt(e + 1));
|
|
562 dos.writeShort((short) exceptionTable.getIntAt(e + 2));
|
|
563 dos.writeShort((short) exceptionTable.getIntAt(e + 3));
|
|
564 }
|
|
565 }
|
|
566
|
|
567 dos.writeShort((short)codeAttrCount);
|
|
568 if (DEBUG) debugMessage("\tcode attribute count = " + codeAttrCount);
|
|
569
|
|
570 // write LineNumberTable, if available.
|
|
571 if (hasLineNumberTable) {
|
705
|
572 writeIndex(_lineNumberTableIndex);
|
0
|
573 dos.writeInt(lineNumberAttrLen);
|
|
574 dos.writeShort((short) lineNumberTable.length);
|
|
575 for (int l = 0; l < lineNumberTable.length; l++) {
|
|
576 dos.writeShort((short) lineNumberTable[l].getStartBCI());
|
|
577 dos.writeShort((short) lineNumberTable[l].getLineNumber());
|
|
578 }
|
|
579 }
|
|
580
|
|
581 // write LocalVariableTable, if available.
|
|
582 if (hasLocalVariableTable) {
|
705
|
583 writeIndex((short) _localVariableTableIndex);
|
0
|
584 dos.writeInt(localVarAttrLen);
|
|
585 dos.writeShort((short) localVariableTable.length);
|
|
586 for (int l = 0; l < localVariableTable.length; l++) {
|
|
587 dos.writeShort((short) localVariableTable[l].getStartBCI());
|
|
588 dos.writeShort((short) localVariableTable[l].getLength());
|
|
589 dos.writeShort((short) localVariableTable[l].getNameCPIndex());
|
|
590 dos.writeShort((short) localVariableTable[l].getDescriptorCPIndex());
|
|
591 dos.writeShort((short) localVariableTable[l].getSlot());
|
|
592 }
|
|
593 }
|
|
594 }
|
|
595
|
705
|
596 if (hasCheckedExceptions) {
|
|
597 CheckedExceptionElement[] exceptions = m.getCheckedExceptions();
|
|
598 writeIndex(_exceptionsIndex);
|
|
599
|
|
600 int attrSize = 2 /* number_of_exceptions */ +
|
|
601 exceptions.length * 2 /* exception_index */;
|
|
602 dos.writeInt(attrSize);
|
|
603 dos.writeShort(exceptions.length);
|
|
604 if (DEBUG) debugMessage("\tmethod has " + exceptions.length
|
|
605 + " checked exception(s)");
|
|
606 for (int e = 0; e < exceptions.length; e++) {
|
|
607 short cpIndex = (short) exceptions[e].getClassCPIndex();
|
|
608 dos.writeShort(cpIndex);
|
|
609 }
|
|
610 }
|
|
611
|
0
|
612 if (isGeneric) {
|
|
613 writeGenericSignature(m.getGenericSignature().asString());
|
|
614 }
|
|
615 }
|
|
616
|
|
617 protected void rewriteByteCode(Method m, byte[] code) {
|
|
618 ByteCodeRewriter r = new ByteCodeRewriter(m, cpool, code);
|
|
619 r.rewrite();
|
|
620 }
|
|
621
|
|
622 protected void writeGenericSignature(String signature) throws IOException {
|
705
|
623 writeIndex(_signatureIndex);
|
0
|
624 if (DEBUG) debugMessage("signature attribute = " + _signatureIndex);
|
|
625 dos.writeInt(2);
|
|
626 Short index = (Short) utf8ToIndex.get(signature);
|
|
627 dos.writeShort(index.shortValue());
|
|
628 if (DEBUG) debugMessage("generic signature = " + index);
|
|
629 }
|
|
630
|
|
631 protected void writeClassAttributes() throws IOException {
|
|
632 final long flags = klass.getAccessFlags();
|
705
|
633 final boolean hasSyn = hasSyntheticAttribute((short) flags);
|
0
|
634
|
|
635 // check for source file
|
|
636 short classAttributeCount = 0;
|
|
637
|
705
|
638 if (hasSyn)
|
0
|
639 classAttributeCount++;
|
|
640
|
|
641 Symbol sourceFileName = klass.getSourceFileName();
|
|
642 if (sourceFileName != null)
|
|
643 classAttributeCount++;
|
|
644
|
|
645 Symbol genericSignature = klass.getGenericSignature();
|
|
646 if (genericSignature != null)
|
|
647 classAttributeCount++;
|
|
648
|
|
649 TypeArray innerClasses = klass.getInnerClasses();
|
|
650 final int numInnerClasses = (int) (innerClasses.getLength() / 4);
|
|
651 if (numInnerClasses != 0)
|
|
652 classAttributeCount++;
|
|
653
|
|
654 dos.writeShort(classAttributeCount);
|
|
655 if (DEBUG) debugMessage("class attribute count = " + classAttributeCount);
|
|
656
|
705
|
657 if (hasSyn)
|
0
|
658 writeSynthetic();
|
|
659
|
|
660 // write SourceFile, if any
|
|
661 if (sourceFileName != null) {
|
705
|
662 writeIndex(_sourceFileIndex);
|
0
|
663 if (DEBUG) debugMessage("source file attribute = " + _sourceFileIndex);
|
|
664 dos.writeInt(2);
|
|
665 Short index = (Short) utf8ToIndex.get(sourceFileName.asString());
|
|
666 dos.writeShort(index.shortValue());
|
|
667 if (DEBUG) debugMessage("source file name = " + index);
|
|
668 }
|
|
669
|
|
670 // write Signature, if any
|
|
671 if (genericSignature != null) {
|
|
672 writeGenericSignature(genericSignature.asString());
|
|
673 }
|
|
674
|
|
675 // write inner classes, if any
|
|
676 if (numInnerClasses != 0) {
|
705
|
677 writeIndex(_innerClassesIndex);
|
0
|
678 final int innerAttrLen = 2 /* number_of_inner_classes */ +
|
|
679 numInnerClasses * (
|
|
680 2 /* inner_class_info_index */ +
|
|
681 2 /* outer_class_info_index */ +
|
|
682 2 /* inner_class_name_index */ +
|
|
683 2 /* inner_class_access_flags */);
|
|
684 dos.writeInt(innerAttrLen);
|
|
685
|
|
686 dos.writeShort(numInnerClasses);
|
|
687 if (DEBUG) debugMessage("class has " + numInnerClasses + " inner class entries");
|
|
688
|
|
689 for (int index = 0; index < numInnerClasses * 4; index++) {
|
|
690 dos.writeShort(innerClasses.getShortAt(index));
|
|
691 }
|
|
692 }
|
|
693 }
|
|
694 }
|