22 * CA 95054 USA or visit www.sun.com if you need additional information or |
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javap; |
26 package com.sun.tools.javap; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
27 |
30 |
28 import com.sun.tools.classfile.AccessFlags; |
31 import com.sun.tools.classfile.AccessFlags; |
29 import com.sun.tools.classfile.Code_attribute; |
32 import com.sun.tools.classfile.Code_attribute; |
30 import com.sun.tools.classfile.ConstantPool; |
33 import com.sun.tools.classfile.ConstantPool; |
31 import com.sun.tools.classfile.ConstantPoolException; |
34 import com.sun.tools.classfile.ConstantPoolException; |
32 import com.sun.tools.classfile.DescriptorException; |
35 import com.sun.tools.classfile.DescriptorException; |
33 import com.sun.tools.classfile.Instruction; |
36 import com.sun.tools.classfile.Instruction; |
34 import com.sun.tools.classfile.Instruction.TypeKind; |
37 import com.sun.tools.classfile.Instruction.TypeKind; |
35 import com.sun.tools.classfile.Method; |
38 import com.sun.tools.classfile.Method; |
36 import com.sun.tools.classfile.Opcode; |
|
37 |
|
38 //import static com.sun.tools.classfile.OpCodes.*; |
|
39 |
39 |
40 /* |
40 /* |
41 * Write the contents of a Code attribute. |
41 * Write the contents of a Code attribute. |
42 * |
42 * |
43 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
43 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
57 super(context); |
57 super(context); |
58 context.put(CodeWriter.class, this); |
58 context.put(CodeWriter.class, this); |
59 attrWriter = AttributeWriter.instance(context); |
59 attrWriter = AttributeWriter.instance(context); |
60 classWriter = ClassWriter.instance(context); |
60 classWriter = ClassWriter.instance(context); |
61 constantWriter = ConstantWriter.instance(context); |
61 constantWriter = ConstantWriter.instance(context); |
|
62 sourceWriter = SourceWriter.instance(context); |
|
63 tryBlockWriter = TryBlockWriter.instance(context); |
|
64 stackMapWriter = StackMapWriter.instance(context); |
|
65 localVariableTableWriter = LocalVariableTableWriter.instance(context); |
|
66 localVariableTypeTableWriter = LocalVariableTypeTableWriter.instance(context); |
|
67 options = Options.instance(context); |
62 } |
68 } |
63 |
69 |
64 void write(Code_attribute attr, ConstantPool constant_pool) { |
70 void write(Code_attribute attr, ConstantPool constant_pool) { |
65 println(" Code:"); |
71 println(" Code:"); |
66 writeVerboseHeader(attr, constant_pool); |
72 writeVerboseHeader(attr, constant_pool); |
88 ", Args_size=" + argCount); |
94 ", Args_size=" + argCount); |
89 |
95 |
90 } |
96 } |
91 |
97 |
92 public void writeInstrs(Code_attribute attr) { |
98 public void writeInstrs(Code_attribute attr) { |
|
99 List<InstructionDetailWriter> detailWriters = getDetailWriters(attr); |
|
100 |
93 for (Instruction instr: attr.getInstructions()) { |
101 for (Instruction instr: attr.getInstructions()) { |
94 try { |
102 try { |
|
103 for (InstructionDetailWriter w: detailWriters) |
|
104 w.writeDetails(instr); |
95 writeInstr(instr); |
105 writeInstr(instr); |
96 } catch (ArrayIndexOutOfBoundsException e) { |
106 } catch (ArrayIndexOutOfBoundsException e) { |
97 println(report("error at or after byte " + instr.getPC())); |
107 println(report("error at or after byte " + instr.getPC())); |
98 break; |
108 break; |
99 } |
109 } |
100 } |
110 } |
|
111 |
|
112 for (InstructionDetailWriter w: detailWriters) |
|
113 w.flush(); |
101 } |
114 } |
102 |
115 |
103 public void writeInstr(Instruction instr) { |
116 public void writeInstr(Instruction instr) { |
104 print(" " + instr.getPC() + ":\t"); |
117 print(" " + instr.getPC() + ":\t"); |
105 print(instr.getMnemonic()); |
118 print(instr.getMnemonic()); |
209 for (int i = s.length(); i < width; i++) |
222 for (int i = s.length(); i < width; i++) |
210 print(" "); |
223 print(" "); |
211 print(s); |
224 print(s); |
212 } |
225 } |
213 |
226 |
214 private static int align(int n) { |
227 private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) { |
215 return (n + 3) & ~3; |
228 List<InstructionDetailWriter> detailWriters = |
|
229 new ArrayList<InstructionDetailWriter>(); |
|
230 if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) { |
|
231 sourceWriter.reset(classWriter.getClassFile(), attr); |
|
232 detailWriters.add(sourceWriter); |
|
233 } |
|
234 |
|
235 if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) { |
|
236 localVariableTableWriter.reset(attr); |
|
237 detailWriters.add(localVariableTableWriter); |
|
238 } |
|
239 |
|
240 if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VAR_TYPES)) { |
|
241 localVariableTypeTableWriter.reset(attr); |
|
242 detailWriters.add(localVariableTypeTableWriter); |
|
243 } |
|
244 |
|
245 if (options.details.contains(InstructionDetailWriter.Kind.STACKMAPS)) { |
|
246 stackMapWriter.reset(attr); |
|
247 stackMapWriter.writeInitialDetails(); |
|
248 detailWriters.add(stackMapWriter); |
|
249 } |
|
250 |
|
251 if (options.details.contains(InstructionDetailWriter.Kind.TRY_BLOCKS)) { |
|
252 tryBlockWriter.reset(attr); |
|
253 detailWriters.add(tryBlockWriter); |
|
254 } |
|
255 |
|
256 return detailWriters; |
216 } |
257 } |
217 |
258 |
218 private AttributeWriter attrWriter; |
259 private AttributeWriter attrWriter; |
219 private ClassWriter classWriter; |
260 private ClassWriter classWriter; |
220 private ConstantWriter constantWriter; |
261 private ConstantWriter constantWriter; |
|
262 private LocalVariableTableWriter localVariableTableWriter; |
|
263 private LocalVariableTypeTableWriter localVariableTypeTableWriter; |
|
264 private SourceWriter sourceWriter; |
|
265 private StackMapWriter stackMapWriter; |
|
266 private TryBlockWriter tryBlockWriter; |
|
267 private Options options; |
221 } |
268 } |