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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2034
ac6ec071c2b2
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javap;
aoqi@0 27
aoqi@0 28 import java.util.ArrayList;
aoqi@0 29 import java.util.List;
aoqi@0 30
aoqi@0 31 import com.sun.tools.classfile.AccessFlags;
aoqi@0 32 import com.sun.tools.classfile.Code_attribute;
aoqi@0 33 import com.sun.tools.classfile.ConstantPool;
aoqi@0 34 import com.sun.tools.classfile.ConstantPoolException;
aoqi@0 35 import com.sun.tools.classfile.DescriptorException;
aoqi@0 36 import com.sun.tools.classfile.Instruction;
aoqi@0 37 import com.sun.tools.classfile.Instruction.TypeKind;
aoqi@0 38 import com.sun.tools.classfile.Method;
aoqi@0 39
aoqi@0 40 /*
aoqi@0 41 * Write the contents of a Code attribute.
aoqi@0 42 *
aoqi@0 43 * <p><b>This is NOT part of any supported API.
aoqi@0 44 * If you write code that depends on this, you do so at your own risk.
aoqi@0 45 * This code and its internal interfaces are subject to change or
aoqi@0 46 * deletion without notice.</b>
aoqi@0 47 */
aoqi@0 48 public class CodeWriter extends BasicWriter {
aoqi@0 49 public static CodeWriter instance(Context context) {
aoqi@0 50 CodeWriter instance = context.get(CodeWriter.class);
aoqi@0 51 if (instance == null)
aoqi@0 52 instance = new CodeWriter(context);
aoqi@0 53 return instance;
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 protected CodeWriter(Context context) {
aoqi@0 57 super(context);
aoqi@0 58 context.put(CodeWriter.class, this);
aoqi@0 59 attrWriter = AttributeWriter.instance(context);
aoqi@0 60 classWriter = ClassWriter.instance(context);
aoqi@0 61 constantWriter = ConstantWriter.instance(context);
aoqi@0 62 sourceWriter = SourceWriter.instance(context);
aoqi@0 63 tryBlockWriter = TryBlockWriter.instance(context);
aoqi@0 64 stackMapWriter = StackMapWriter.instance(context);
aoqi@0 65 localVariableTableWriter = LocalVariableTableWriter.instance(context);
aoqi@0 66 localVariableTypeTableWriter = LocalVariableTypeTableWriter.instance(context);
aoqi@0 67 typeAnnotationWriter = TypeAnnotationWriter.instance(context);
aoqi@0 68 options = Options.instance(context);
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 void write(Code_attribute attr, ConstantPool constant_pool) {
aoqi@0 72 println("Code:");
aoqi@0 73 indent(+1);
aoqi@0 74 writeVerboseHeader(attr, constant_pool);
aoqi@0 75 writeInstrs(attr);
aoqi@0 76 writeExceptionTable(attr);
aoqi@0 77 attrWriter.write(attr, attr.attributes, constant_pool);
aoqi@0 78 indent(-1);
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
aoqi@0 82 Method method = classWriter.getMethod();
aoqi@0 83 String argCount;
aoqi@0 84 try {
aoqi@0 85 int n = method.descriptor.getParameterCount(constant_pool);
aoqi@0 86 if (!method.access_flags.is(AccessFlags.ACC_STATIC))
aoqi@0 87 ++n; // for 'this'
aoqi@0 88 argCount = Integer.toString(n);
aoqi@0 89 } catch (ConstantPoolException e) {
aoqi@0 90 argCount = report(e);
aoqi@0 91 } catch (DescriptorException e) {
aoqi@0 92 argCount = report(e);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 println("stack=" + attr.max_stack +
aoqi@0 96 ", locals=" + attr.max_locals +
aoqi@0 97 ", args_size=" + argCount);
aoqi@0 98
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 public void writeInstrs(Code_attribute attr) {
aoqi@0 102 List<InstructionDetailWriter> detailWriters = getDetailWriters(attr);
aoqi@0 103
aoqi@0 104 for (Instruction instr: attr.getInstructions()) {
aoqi@0 105 try {
aoqi@0 106 for (InstructionDetailWriter w: detailWriters)
aoqi@0 107 w.writeDetails(instr);
aoqi@0 108 writeInstr(instr);
aoqi@0 109 } catch (ArrayIndexOutOfBoundsException e) {
aoqi@0 110 println(report("error at or after byte " + instr.getPC()));
aoqi@0 111 break;
aoqi@0 112 }
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 for (InstructionDetailWriter w: detailWriters)
aoqi@0 116 w.flush();
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 public void writeInstr(Instruction instr) {
aoqi@0 120 print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));
aoqi@0 121 // compute the number of indentations for the body of multi-line instructions
aoqi@0 122 // This is 6 (the width of "%4d: "), divided by the width of each indentation level,
aoqi@0 123 // and rounded up to the next integer.
aoqi@0 124 int indentWidth = options.indentWidth;
aoqi@0 125 int indent = (6 + indentWidth - 1) / indentWidth;
aoqi@0 126 instr.accept(instructionPrinter, indent);
aoqi@0 127 println();
aoqi@0 128 }
aoqi@0 129 // where
aoqi@0 130 Instruction.KindVisitor<Void,Integer> instructionPrinter =
aoqi@0 131 new Instruction.KindVisitor<Void,Integer>() {
aoqi@0 132
aoqi@0 133 public Void visitNoOperands(Instruction instr, Integer indent) {
aoqi@0 134 return null;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public Void visitArrayType(Instruction instr, TypeKind kind, Integer indent) {
aoqi@0 138 print(" " + kind.name);
aoqi@0 139 return null;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public Void visitBranch(Instruction instr, int offset, Integer indent) {
aoqi@0 143 print((instr.getPC() + offset));
aoqi@0 144 return null;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 public Void visitConstantPoolRef(Instruction instr, int index, Integer indent) {
aoqi@0 148 print("#" + index);
aoqi@0 149 tab();
aoqi@0 150 print("// ");
aoqi@0 151 printConstant(index);
aoqi@0 152 return null;
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Integer indent) {
aoqi@0 156 print("#" + index + ", " + value);
aoqi@0 157 tab();
aoqi@0 158 print("// ");
aoqi@0 159 printConstant(index);
aoqi@0 160 return null;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 public Void visitLocal(Instruction instr, int index, Integer indent) {
aoqi@0 164 print(index);
aoqi@0 165 return null;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 public Void visitLocalAndValue(Instruction instr, int index, int value, Integer indent) {
aoqi@0 169 print(index + ", " + value);
aoqi@0 170 return null;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 public Void visitLookupSwitch(Instruction instr,
aoqi@0 174 int default_, int npairs, int[] matches, int[] offsets, Integer indent) {
aoqi@0 175 int pc = instr.getPC();
aoqi@0 176 print("{ // " + npairs);
aoqi@0 177 indent(indent);
aoqi@0 178 for (int i = 0; i < npairs; i++) {
aoqi@0 179 print(String.format("%n%12d: %d", matches[i], (pc + offsets[i])));
aoqi@0 180 }
aoqi@0 181 print("\n default: " + (pc + default_) + "\n}");
aoqi@0 182 indent(-indent);
aoqi@0 183 return null;
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 public Void visitTableSwitch(Instruction instr,
aoqi@0 187 int default_, int low, int high, int[] offsets, Integer indent) {
aoqi@0 188 int pc = instr.getPC();
aoqi@0 189 print("{ // " + low + " to " + high);
aoqi@0 190 indent(indent);
aoqi@0 191 for (int i = 0; i < offsets.length; i++) {
aoqi@0 192 print(String.format("%n%12d: %d", (low + i), (pc + offsets[i])));
aoqi@0 193 }
aoqi@0 194 print("\n default: " + (pc + default_) + "\n}");
aoqi@0 195 indent(-indent);
aoqi@0 196 return null;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 public Void visitValue(Instruction instr, int value, Integer indent) {
aoqi@0 200 print(value);
aoqi@0 201 return null;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 public Void visitUnknown(Instruction instr, Integer indent) {
aoqi@0 205 return null;
aoqi@0 206 }
aoqi@0 207 };
aoqi@0 208
aoqi@0 209
aoqi@0 210 public void writeExceptionTable(Code_attribute attr) {
aoqi@0 211 if (attr.exception_table_length > 0) {
aoqi@0 212 println("Exception table:");
aoqi@0 213 indent(+1);
aoqi@0 214 println(" from to target type");
aoqi@0 215 for (int i = 0; i < attr.exception_table.length; i++) {
aoqi@0 216 Code_attribute.Exception_data handler = attr.exception_table[i];
aoqi@0 217 print(String.format(" %5d %5d %5d",
aoqi@0 218 handler.start_pc, handler.end_pc, handler.handler_pc));
aoqi@0 219 print(" ");
aoqi@0 220 int catch_type = handler.catch_type;
aoqi@0 221 if (catch_type == 0) {
aoqi@0 222 println("any");
aoqi@0 223 } else {
aoqi@0 224 print("Class ");
aoqi@0 225 println(constantWriter.stringValue(catch_type));
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228 indent(-1);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 private void printConstant(int index) {
aoqi@0 234 constantWriter.write(index);
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) {
aoqi@0 238 List<InstructionDetailWriter> detailWriters =
aoqi@0 239 new ArrayList<InstructionDetailWriter>();
aoqi@0 240 if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) {
aoqi@0 241 sourceWriter.reset(classWriter.getClassFile(), attr);
aoqi@0 242 if (sourceWriter.hasSource())
aoqi@0 243 detailWriters.add(sourceWriter);
aoqi@0 244 else
aoqi@0 245 println("(Source code not available)");
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) {
aoqi@0 249 localVariableTableWriter.reset(attr);
aoqi@0 250 detailWriters.add(localVariableTableWriter);
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VAR_TYPES)) {
aoqi@0 254 localVariableTypeTableWriter.reset(attr);
aoqi@0 255 detailWriters.add(localVariableTypeTableWriter);
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 if (options.details.contains(InstructionDetailWriter.Kind.STACKMAPS)) {
aoqi@0 259 stackMapWriter.reset(attr);
aoqi@0 260 stackMapWriter.writeInitialDetails();
aoqi@0 261 detailWriters.add(stackMapWriter);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 if (options.details.contains(InstructionDetailWriter.Kind.TRY_BLOCKS)) {
aoqi@0 265 tryBlockWriter.reset(attr);
aoqi@0 266 detailWriters.add(tryBlockWriter);
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 if (options.details.contains(InstructionDetailWriter.Kind.TYPE_ANNOS)) {
aoqi@0 270 typeAnnotationWriter.reset(attr);
aoqi@0 271 detailWriters.add(typeAnnotationWriter);
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 return detailWriters;
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 private AttributeWriter attrWriter;
aoqi@0 278 private ClassWriter classWriter;
aoqi@0 279 private ConstantWriter constantWriter;
aoqi@0 280 private LocalVariableTableWriter localVariableTableWriter;
aoqi@0 281 private LocalVariableTypeTableWriter localVariableTypeTableWriter;
aoqi@0 282 private TypeAnnotationWriter typeAnnotationWriter;
aoqi@0 283 private SourceWriter sourceWriter;
aoqi@0 284 private StackMapWriter stackMapWriter;
aoqi@0 285 private TryBlockWriter tryBlockWriter;
aoqi@0 286 private Options options;
aoqi@0 287 }

mercurial