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