src/share/classes/com/sun/tools/javap/CodeWriter.java

changeset 348
743f17b55b44
parent 338
777a3efad0d5
child 354
cba827f72977
equal deleted inserted replaced
347:dbf8a2816201 348:743f17b55b44
67 typeAnnotationWriter = TypeAnnotationWriter.instance(context); 67 typeAnnotationWriter = TypeAnnotationWriter.instance(context);
68 options = Options.instance(context); 68 options = Options.instance(context);
69 } 69 }
70 70
71 void write(Code_attribute attr, ConstantPool constant_pool) { 71 void write(Code_attribute attr, ConstantPool constant_pool) {
72 println(" Code:"); 72 println("Code:");
73 indent(+1);
73 writeVerboseHeader(attr, constant_pool); 74 writeVerboseHeader(attr, constant_pool);
74 writeInstrs(attr); 75 writeInstrs(attr);
75 writeExceptionTable(attr); 76 writeExceptionTable(attr);
76 attrWriter.write(attr, attr.attributes, constant_pool); 77 attrWriter.write(attr, attr.attributes, constant_pool);
78 indent(-1);
77 } 79 }
78 80
79 public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) { 81 public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
80 Method method = classWriter.getMethod(); 82 Method method = classWriter.getMethod();
81 String argCount; 83 String argCount;
88 argCount = report(e); 90 argCount = report(e);
89 } catch (DescriptorException e) { 91 } catch (DescriptorException e) {
90 argCount = report(e); 92 argCount = report(e);
91 } 93 }
92 94
93 println(" Stack=" + attr.max_stack + 95 println("stack=" + attr.max_stack +
94 ", Locals=" + attr.max_locals + 96 ", locals=" + attr.max_locals +
95 ", Args_size=" + argCount); 97 ", args_size=" + argCount);
96 98
97 } 99 }
98 100
99 public void writeInstrs(Code_attribute attr) { 101 public void writeInstrs(Code_attribute attr) {
100 List<InstructionDetailWriter> detailWriters = getDetailWriters(attr); 102 List<InstructionDetailWriter> detailWriters = getDetailWriters(attr);
113 for (InstructionDetailWriter w: detailWriters) 115 for (InstructionDetailWriter w: detailWriters)
114 w.flush(); 116 w.flush();
115 } 117 }
116 118
117 public void writeInstr(Instruction instr) { 119 public void writeInstr(Instruction instr) {
118 print(" " + instr.getPC() + ":\t"); 120 print(String.format("%4d: %-12s ", instr.getPC(), instr.getMnemonic()));
119 print(instr.getMnemonic());
120 instr.accept(instructionPrinter, null); 121 instr.accept(instructionPrinter, null);
121 println(); 122 println();
122 } 123 }
123 // where 124 // where
124 Instruction.KindVisitor<Void,Void> instructionPrinter = 125 Instruction.KindVisitor<Void,Void> instructionPrinter =
132 print(" " + kind.name); 133 print(" " + kind.name);
133 return null; 134 return null;
134 } 135 }
135 136
136 public Void visitBranch(Instruction instr, int offset, Void p) { 137 public Void visitBranch(Instruction instr, int offset, Void p) {
137 print("\t" + (instr.getPC() + offset)); 138 print((instr.getPC() + offset));
138 return null; 139 return null;
139 } 140 }
140 141
141 public Void visitConstantPoolRef(Instruction instr, int index, Void p) { 142 public Void visitConstantPoolRef(Instruction instr, int index, Void p) {
142 print("\t#" + index + "; //"); 143 print("#" + index + ";");
144 tab();
145 print("// ");
143 printConstant(index); 146 printConstant(index);
144 return null; 147 return null;
145 } 148 }
146 149
147 public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) { 150 public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) {
148 print("\t#" + index + ", " + value + "; //"); 151 print("#" + index + ", " + value + ";");
152 tab();
153 print("// ");
149 printConstant(index); 154 printConstant(index);
150 return null; 155 return null;
151 } 156 }
152 157
153 public Void visitLocal(Instruction instr, int index, Void p) { 158 public Void visitLocal(Instruction instr, int index, Void p) {
154 print("\t" + index); 159 print(index);
155 return null; 160 return null;
156 } 161 }
157 162
158 public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) { 163 public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) {
159 print("\t" + index + ", " + value); 164 print(index + ", " + value);
160 return null; 165 return null;
161 } 166 }
162 167
163 public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets) { 168 public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets) {
164 int pc = instr.getPC(); 169 int pc = instr.getPC();
165 print("{ //" + npairs); 170 print("{ // " + npairs);
171 indent(+1);
166 for (int i = 0; i < npairs; i++) { 172 for (int i = 0; i < npairs; i++) {
167 print("\n\t\t" + matches[i] + ": " + (pc + offsets[i]) + ";"); 173 print("\n" + matches[i] + ": " + (pc + offsets[i]) + ";");
168 } 174 }
169 print("\n\t\tdefault: " + (pc + default_) + " }"); 175 print("\ndefault: " + (pc + default_) + " }");
176 indent(-1);
170 return null; 177 return null;
171 } 178 }
172 179
173 public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets) { 180 public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets) {
174 int pc = instr.getPC(); 181 int pc = instr.getPC();
175 print("{ //" + low + " to " + high); 182 print("{ //" + low + " to " + high);
183 indent(+1);
176 for (int i = 0; i < offsets.length; i++) { 184 for (int i = 0; i < offsets.length; i++) {
177 print("\n\t\t" + (low + i) + ": " + (pc + offsets[i]) + ";"); 185 print("\n" + (low + i) + ": " + (pc + offsets[i]) + ";");
178 } 186 }
179 print("\n\t\tdefault: " + (pc + default_) + " }"); 187 print("\ndefault: " + (pc + default_) + " }");
188 indent(-1);
180 return null; 189 return null;
181 } 190 }
182 191
183 public Void visitValue(Instruction instr, int value, Void p) { 192 public Void visitValue(Instruction instr, int value, Void p) {
184 print("\t" + value); 193 print(value);
185 return null; 194 return null;
186 } 195 }
187 196
188 public Void visitUnknown(Instruction instr, Void p) { 197 public Void visitUnknown(Instruction instr, Void p) {
189 return null; 198 return null;
191 }; 200 };
192 201
193 202
194 public void writeExceptionTable(Code_attribute attr) { 203 public void writeExceptionTable(Code_attribute attr) {
195 if (attr.exception_table_langth > 0) { 204 if (attr.exception_table_langth > 0) {
196 println(" Exception table:"); 205 println("Exception table:");
197 println(" from to target type"); 206 indent(+1);
207 println(" from to target type");
198 for (int i = 0; i < attr.exception_table.length; i++) { 208 for (int i = 0; i < attr.exception_table.length; i++) {
199 Code_attribute.Exception_data handler = attr.exception_table[i]; 209 Code_attribute.Exception_data handler = attr.exception_table[i];
200 printFixedWidthInt(handler.start_pc, 6); 210 print(String.format(" %5d %5d %5d",
201 printFixedWidthInt(handler.end_pc, 6); 211 handler.start_pc, handler.end_pc, handler.handler_pc));
202 printFixedWidthInt(handler.handler_pc, 6);
203 print(" "); 212 print(" ");
204 int catch_type = handler.catch_type; 213 int catch_type = handler.catch_type;
205 if (catch_type == 0) { 214 if (catch_type == 0) {
206 println("any"); 215 println("any");
207 } else { 216 } else {
208 print("Class "); 217 print("Class ");
209 println(constantWriter.stringValue(catch_type)); 218 println(constantWriter.stringValue(catch_type));
210 println("");
211 } 219 }
212 } 220 }
221 indent(-1);
213 } 222 }
214 223
215 } 224 }
216 225
217 private void printConstant(int index) { 226 private void printConstant(int index) {
218 constantWriter.write(index); 227 constantWriter.write(index);
219 }
220
221 private void printFixedWidthInt(int n, int width) {
222 String s = String.valueOf(n);
223 for (int i = s.length(); i < width; i++)
224 print(" ");
225 print(s);
226 } 228 }
227 229
228 private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) { 230 private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) {
229 List<InstructionDetailWriter> detailWriters = 231 List<InstructionDetailWriter> detailWriters =
230 new ArrayList<InstructionDetailWriter>(); 232 new ArrayList<InstructionDetailWriter>();

mercurial