jjg@46: /*
xdono@54: * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46: *
jjg@46: * This code is free software; you can redistribute it and/or modify it
jjg@46: * under the terms of the GNU General Public License version 2 only, as
jjg@46: * published by the Free Software Foundation. Sun designates this
jjg@46: * particular file as subject to the "Classpath" exception as provided
jjg@46: * by Sun in the LICENSE file that accompanied this code.
jjg@46: *
jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46: * version 2 for more details (a copy is included in the LICENSE file that
jjg@46: * accompanied this code).
jjg@46: *
jjg@46: * You should have received a copy of the GNU General Public License version
jjg@46: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46: *
jjg@46: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@46: * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@46: * have any questions.
jjg@46: */
jjg@46:
jjg@46: package com.sun.tools.javap;
jjg@46:
jjg@283: import java.util.ArrayList;
jjg@283: import java.util.List;
jjg@283:
jjg@46: import com.sun.tools.classfile.AccessFlags;
jjg@46: import com.sun.tools.classfile.Code_attribute;
jjg@46: import com.sun.tools.classfile.ConstantPool;
jjg@46: import com.sun.tools.classfile.ConstantPoolException;
jjg@46: import com.sun.tools.classfile.DescriptorException;
jjg@255: import com.sun.tools.classfile.Instruction;
jjg@255: import com.sun.tools.classfile.Instruction.TypeKind;
jjg@46: import com.sun.tools.classfile.Method;
jjg@46:
jjg@46: /*
jjg@46: * Write the contents of a Code attribute.
jjg@46: *
jjg@46: *
This is NOT part of any API supported by Sun Microsystems. If
jjg@46: * you write code that depends on this, you do so at your own risk.
jjg@46: * This code and its internal interfaces are subject to change or
jjg@46: * deletion without notice.
jjg@46: */
jjg@46: class CodeWriter extends BasicWriter {
jjg@46: static CodeWriter instance(Context context) {
jjg@46: CodeWriter instance = context.get(CodeWriter.class);
jjg@46: if (instance == null)
jjg@46: instance = new CodeWriter(context);
jjg@46: return instance;
jjg@46: }
jjg@46:
jjg@46: protected CodeWriter(Context context) {
jjg@46: super(context);
jjg@46: context.put(CodeWriter.class, this);
jjg@46: attrWriter = AttributeWriter.instance(context);
jjg@46: classWriter = ClassWriter.instance(context);
jjg@46: constantWriter = ConstantWriter.instance(context);
jjg@283: sourceWriter = SourceWriter.instance(context);
jjg@283: tryBlockWriter = TryBlockWriter.instance(context);
jjg@283: stackMapWriter = StackMapWriter.instance(context);
jjg@283: localVariableTableWriter = LocalVariableTableWriter.instance(context);
jjg@283: localVariableTypeTableWriter = LocalVariableTypeTableWriter.instance(context);
jjg@338: typeAnnotationWriter = TypeAnnotationWriter.instance(context);
jjg@283: options = Options.instance(context);
jjg@46: }
jjg@46:
jjg@46: void write(Code_attribute attr, ConstantPool constant_pool) {
jjg@348: println("Code:");
jjg@348: indent(+1);
jjg@46: writeVerboseHeader(attr, constant_pool);
jjg@46: writeInstrs(attr);
jjg@46: writeExceptionTable(attr);
jjg@46: attrWriter.write(attr, attr.attributes, constant_pool);
jjg@348: indent(-1);
jjg@46: }
jjg@46:
jjg@46: public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
jjg@46: Method method = classWriter.getMethod();
jjg@46: String argCount;
jjg@46: try {
jjg@46: int n = method.descriptor.getParameterCount(constant_pool);
jjg@46: if (!method.access_flags.is(AccessFlags.ACC_STATIC))
jjg@46: ++n; // for 'this'
jjg@46: argCount = Integer.toString(n);
jjg@46: } catch (ConstantPoolException e) {
jjg@46: argCount = report(e);
jjg@46: } catch (DescriptorException e) {
jjg@46: argCount = report(e);
jjg@46: }
jjg@46:
jjg@348: println("stack=" + attr.max_stack +
jjg@348: ", locals=" + attr.max_locals +
jjg@348: ", args_size=" + argCount);
jjg@46:
jjg@46: }
jjg@46:
jjg@46: public void writeInstrs(Code_attribute attr) {
jjg@283: List detailWriters = getDetailWriters(attr);
jjg@283:
jjg@255: for (Instruction instr: attr.getInstructions()) {
jjg@255: try {
jjg@283: for (InstructionDetailWriter w: detailWriters)
jjg@283: w.writeDetails(instr);
jjg@255: writeInstr(instr);
jjg@255: } catch (ArrayIndexOutOfBoundsException e) {
jjg@255: println(report("error at or after byte " + instr.getPC()));
jjg@255: break;
jjg@46: }
jjg@46: }
jjg@283:
jjg@283: for (InstructionDetailWriter w: detailWriters)
jjg@283: w.flush();
jjg@46: }
jjg@46:
jjg@255: public void writeInstr(Instruction instr) {
jjg@355: print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));
jjg@437: // compute the number of indentations for the body of multi-line instructions
jjg@437: // This is 6 (the width of "%4d: "), divided by the width of each indentation level,
jjg@437: // and rounded up to the next integer.
jjg@437: int indentWidth = options.indentWidth;
jjg@437: int indent = (6 + indentWidth - 1) / indentWidth;
jjg@437: instr.accept(instructionPrinter, indent);
jjg@255: println();
jjg@255: }
jjg@255: // where
jjg@437: Instruction.KindVisitor instructionPrinter =
jjg@437: new Instruction.KindVisitor() {
jjg@255:
jjg@437: public Void visitNoOperands(Instruction instr, Integer indent) {
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitArrayType(Instruction instr, TypeKind kind, Integer indent) {
jjg@255: print(" " + kind.name);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitBranch(Instruction instr, int offset, Integer indent) {
jjg@348: print((instr.getPC() + offset));
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitConstantPoolRef(Instruction instr, int index, Integer indent) {
jjg@354: print("#" + index);
jjg@348: tab();
jjg@348: print("// ");
jjg@255: printConstant(index);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Integer indent) {
jjg@354: print("#" + index + ", " + value);
jjg@348: tab();
jjg@348: print("// ");
jjg@255: printConstant(index);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitLocal(Instruction instr, int index, Integer indent) {
jjg@348: print(index);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitLocalAndValue(Instruction instr, int index, int value, Integer indent) {
jjg@348: print(index + ", " + value);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitLookupSwitch(Instruction instr,
jjg@437: int default_, int npairs, int[] matches, int[] offsets, Integer indent) {
jjg@255: int pc = instr.getPC();
jjg@348: print("{ // " + npairs);
jjg@437: indent(indent);
jjg@255: for (int i = 0; i < npairs; i++) {
jjg@437: print(String.format("%n%12d: %d", matches[i], (pc + offsets[i])));
jjg@46: }
jjg@437: print("\n default: " + (pc + default_) + "\n}");
jjg@437: indent(-indent);
jjg@255: return null;
jjg@255: }
jjg@255:
jjg@437: public Void visitTableSwitch(Instruction instr,
jjg@437: int default_, int low, int high, int[] offsets, Integer indent) {
jjg@255: int pc = instr.getPC();
jjg@437: print("{ // " + low + " to " + high);
jjg@437: indent(indent);
jjg@255: for (int i = 0; i < offsets.length; i++) {
jjg@437: print(String.format("%n%12d: %d", (low + i), (pc + offsets[i])));
jjg@46: }
jjg@437: print("\n default: " + (pc + default_) + "\n}");
jjg@437: indent(-indent);
jjg@255: return null;
jjg@46: }
jjg@255:
jjg@437: public Void visitValue(Instruction instr, int value, Integer indent) {
jjg@348: print(value);
jjg@255: return null;
jjg@46: }
jjg@255:
jjg@437: public Void visitUnknown(Instruction instr, Integer indent) {
jjg@255: return null;
jjg@46: }
jjg@255: };
jjg@255:
jjg@46:
jjg@46: public void writeExceptionTable(Code_attribute attr) {
jjg@46: if (attr.exception_table_langth > 0) {
jjg@348: println("Exception table:");
jjg@348: indent(+1);
jjg@348: println(" from to target type");
jjg@46: for (int i = 0; i < attr.exception_table.length; i++) {
jjg@46: Code_attribute.Exception_data handler = attr.exception_table[i];
jjg@348: print(String.format(" %5d %5d %5d",
jjg@348: handler.start_pc, handler.end_pc, handler.handler_pc));
jjg@46: print(" ");
jjg@46: int catch_type = handler.catch_type;
jjg@46: if (catch_type == 0) {
jjg@46: println("any");
jjg@46: } else {
jjg@46: print("Class ");
jjg@46: println(constantWriter.stringValue(catch_type));
jjg@46: }
jjg@46: }
jjg@348: indent(-1);
jjg@46: }
jjg@46:
jjg@46: }
jjg@46:
jjg@46: private void printConstant(int index) {
jjg@46: constantWriter.write(index);
jjg@46: }
jjg@46:
jjg@283: private List getDetailWriters(Code_attribute attr) {
jjg@283: List detailWriters =
jjg@283: new ArrayList();
jjg@283: if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) {
jjg@283: sourceWriter.reset(classWriter.getClassFile(), attr);
jjg@283: detailWriters.add(sourceWriter);
jjg@283: }
jjg@283:
jjg@283: if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) {
jjg@283: localVariableTableWriter.reset(attr);
jjg@283: detailWriters.add(localVariableTableWriter);
jjg@283: }
jjg@283:
jjg@283: if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VAR_TYPES)) {
jjg@283: localVariableTypeTableWriter.reset(attr);
jjg@283: detailWriters.add(localVariableTypeTableWriter);
jjg@283: }
jjg@283:
jjg@283: if (options.details.contains(InstructionDetailWriter.Kind.STACKMAPS)) {
jjg@283: stackMapWriter.reset(attr);
jjg@283: stackMapWriter.writeInitialDetails();
jjg@283: detailWriters.add(stackMapWriter);
jjg@283: }
jjg@283:
jjg@283: if (options.details.contains(InstructionDetailWriter.Kind.TRY_BLOCKS)) {
jjg@283: tryBlockWriter.reset(attr);
jjg@283: detailWriters.add(tryBlockWriter);
jjg@283: }
jjg@283:
jjg@338: if (options.details.contains(InstructionDetailWriter.Kind.TYPE_ANNOS)) {
jjg@338: typeAnnotationWriter.reset(attr);
jjg@338: detailWriters.add(typeAnnotationWriter);
jjg@338: }
jjg@338:
jjg@283: return detailWriters;
jjg@46: }
jjg@46:
jjg@46: private AttributeWriter attrWriter;
jjg@46: private ClassWriter classWriter;
jjg@46: private ConstantWriter constantWriter;
jjg@283: private LocalVariableTableWriter localVariableTableWriter;
jjg@283: private LocalVariableTypeTableWriter localVariableTypeTableWriter;
jjg@338: private TypeAnnotationWriter typeAnnotationWriter;
jjg@283: private SourceWriter sourceWriter;
jjg@283: private StackMapWriter stackMapWriter;
jjg@283: private TryBlockWriter tryBlockWriter;
jjg@283: private Options options;
jjg@46: }