annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ClassWriter.java @ 705:1f2abec69714

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