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