Thu, 31 Aug 2017 15:17:03 +0800
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.Formatter; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.tools.classfile.AccessFlags; |
aoqi@0 | 31 | import com.sun.tools.classfile.AnnotationDefault_attribute; |
aoqi@0 | 32 | import com.sun.tools.classfile.Attribute; |
aoqi@0 | 33 | import com.sun.tools.classfile.Attributes; |
aoqi@0 | 34 | import com.sun.tools.classfile.BootstrapMethods_attribute; |
aoqi@0 | 35 | import com.sun.tools.classfile.CharacterRangeTable_attribute; |
aoqi@0 | 36 | import com.sun.tools.classfile.Code_attribute; |
aoqi@0 | 37 | import com.sun.tools.classfile.CompilationID_attribute; |
aoqi@0 | 38 | import com.sun.tools.classfile.ConstantPool; |
aoqi@0 | 39 | import com.sun.tools.classfile.ConstantPoolException; |
aoqi@0 | 40 | import com.sun.tools.classfile.ConstantValue_attribute; |
aoqi@0 | 41 | import com.sun.tools.classfile.DefaultAttribute; |
aoqi@0 | 42 | import com.sun.tools.classfile.Deprecated_attribute; |
aoqi@0 | 43 | import com.sun.tools.classfile.EnclosingMethod_attribute; |
aoqi@0 | 44 | import com.sun.tools.classfile.Exceptions_attribute; |
aoqi@0 | 45 | import com.sun.tools.classfile.InnerClasses_attribute; |
aoqi@0 | 46 | import com.sun.tools.classfile.LineNumberTable_attribute; |
aoqi@0 | 47 | import com.sun.tools.classfile.LocalVariableTable_attribute; |
aoqi@0 | 48 | import com.sun.tools.classfile.LocalVariableTypeTable_attribute; |
aoqi@0 | 49 | import com.sun.tools.classfile.MethodParameters_attribute; |
aoqi@0 | 50 | import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute; |
aoqi@0 | 51 | import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute; |
aoqi@0 | 52 | import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute; |
aoqi@0 | 53 | import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute; |
aoqi@0 | 54 | import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute; |
aoqi@0 | 55 | import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute; |
aoqi@0 | 56 | import com.sun.tools.classfile.Signature_attribute; |
aoqi@0 | 57 | import com.sun.tools.classfile.SourceDebugExtension_attribute; |
aoqi@0 | 58 | import com.sun.tools.classfile.SourceFile_attribute; |
aoqi@0 | 59 | import com.sun.tools.classfile.SourceID_attribute; |
aoqi@0 | 60 | import com.sun.tools.classfile.StackMapTable_attribute; |
aoqi@0 | 61 | import com.sun.tools.classfile.StackMap_attribute; |
aoqi@0 | 62 | import com.sun.tools.classfile.Synthetic_attribute; |
aoqi@0 | 63 | |
aoqi@0 | 64 | import static com.sun.tools.classfile.AccessFlags.*; |
aoqi@0 | 65 | import com.sun.tools.javac.util.StringUtils; |
aoqi@0 | 66 | |
aoqi@0 | 67 | /* |
aoqi@0 | 68 | * A writer for writing Attributes as text. |
aoqi@0 | 69 | * |
aoqi@0 | 70 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 71 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 72 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 73 | * deletion without notice.</b> |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public class AttributeWriter extends BasicWriter |
aoqi@0 | 76 | implements Attribute.Visitor<Void,Void> |
aoqi@0 | 77 | { |
aoqi@0 | 78 | public static AttributeWriter instance(Context context) { |
aoqi@0 | 79 | AttributeWriter instance = context.get(AttributeWriter.class); |
aoqi@0 | 80 | if (instance == null) |
aoqi@0 | 81 | instance = new AttributeWriter(context); |
aoqi@0 | 82 | return instance; |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | protected AttributeWriter(Context context) { |
aoqi@0 | 86 | super(context); |
aoqi@0 | 87 | context.put(AttributeWriter.class, this); |
aoqi@0 | 88 | annotationWriter = AnnotationWriter.instance(context); |
aoqi@0 | 89 | codeWriter = CodeWriter.instance(context); |
aoqi@0 | 90 | constantWriter = ConstantWriter.instance(context); |
aoqi@0 | 91 | options = Options.instance(context); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | public void write(Object owner, Attribute attr, ConstantPool constant_pool) { |
aoqi@0 | 95 | if (attr != null) { |
aoqi@0 | 96 | // null checks |
aoqi@0 | 97 | owner.getClass(); |
aoqi@0 | 98 | constant_pool.getClass(); |
aoqi@0 | 99 | this.constant_pool = constant_pool; |
aoqi@0 | 100 | this.owner = owner; |
aoqi@0 | 101 | attr.accept(this, null); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | public void write(Object owner, Attributes attrs, ConstantPool constant_pool) { |
aoqi@0 | 106 | if (attrs != null) { |
aoqi@0 | 107 | // null checks |
aoqi@0 | 108 | owner.getClass(); |
aoqi@0 | 109 | constant_pool.getClass(); |
aoqi@0 | 110 | this.constant_pool = constant_pool; |
aoqi@0 | 111 | this.owner = owner; |
aoqi@0 | 112 | for (Attribute attr: attrs) |
aoqi@0 | 113 | attr.accept(this, null); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | public Void visitDefault(DefaultAttribute attr, Void ignore) { |
aoqi@0 | 118 | if (attr.reason != null) { |
aoqi@0 | 119 | report(attr.reason); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | byte[] data = attr.info; |
aoqi@0 | 122 | int i = 0; |
aoqi@0 | 123 | int j = 0; |
aoqi@0 | 124 | print(" "); |
aoqi@0 | 125 | try { |
aoqi@0 | 126 | print(attr.getName(constant_pool)); |
aoqi@0 | 127 | } catch (ConstantPoolException e) { |
aoqi@0 | 128 | report(e); |
aoqi@0 | 129 | print("attribute name = #" + attr.attribute_name_index); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | print(": "); |
aoqi@0 | 132 | println("length = 0x" + toHex(attr.info.length)); |
aoqi@0 | 133 | |
aoqi@0 | 134 | print(" "); |
aoqi@0 | 135 | |
aoqi@0 | 136 | while (i < data.length) { |
aoqi@0 | 137 | print(toHex(data[i], 2)); |
aoqi@0 | 138 | |
aoqi@0 | 139 | j++; |
aoqi@0 | 140 | if (j == 16) { |
aoqi@0 | 141 | println(); |
aoqi@0 | 142 | print(" "); |
aoqi@0 | 143 | j = 0; |
aoqi@0 | 144 | } else { |
aoqi@0 | 145 | print(" "); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | i++; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | println(); |
aoqi@0 | 150 | return null; |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | public Void visitAnnotationDefault(AnnotationDefault_attribute attr, Void ignore) { |
aoqi@0 | 154 | println("AnnotationDefault:"); |
aoqi@0 | 155 | indent(+1); |
aoqi@0 | 156 | print("default_value: "); |
aoqi@0 | 157 | annotationWriter.write(attr.default_value); |
aoqi@0 | 158 | indent(-1); |
aoqi@0 | 159 | return null; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | public Void visitBootstrapMethods(BootstrapMethods_attribute attr, Void p) { |
aoqi@0 | 163 | println(Attribute.BootstrapMethods + ":"); |
aoqi@0 | 164 | for (int i = 0; i < attr.bootstrap_method_specifiers.length ; i++) { |
aoqi@0 | 165 | BootstrapMethods_attribute.BootstrapMethodSpecifier bsm = attr.bootstrap_method_specifiers[i]; |
aoqi@0 | 166 | indent(+1); |
aoqi@0 | 167 | print(i + ": #" + bsm.bootstrap_method_ref + " "); |
aoqi@0 | 168 | println(constantWriter.stringValue(bsm.bootstrap_method_ref)); |
aoqi@0 | 169 | indent(+1); |
aoqi@0 | 170 | println("Method arguments:"); |
aoqi@0 | 171 | indent(+1); |
aoqi@0 | 172 | for (int j = 0; j < bsm.bootstrap_arguments.length; j++) { |
aoqi@0 | 173 | print("#" + bsm.bootstrap_arguments[j] + " "); |
aoqi@0 | 174 | println(constantWriter.stringValue(bsm.bootstrap_arguments[j])); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | indent(-3); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | return null; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, Void ignore) { |
aoqi@0 | 182 | println("CharacterRangeTable:"); |
aoqi@0 | 183 | indent(+1); |
aoqi@0 | 184 | for (int i = 0; i < attr.character_range_table.length; i++) { |
aoqi@0 | 185 | CharacterRangeTable_attribute.Entry e = attr.character_range_table[i]; |
aoqi@0 | 186 | print(String.format(" %2d, %2d, %6x, %6x, %4x", |
aoqi@0 | 187 | e.start_pc, e.end_pc, |
aoqi@0 | 188 | e.character_range_start, e.character_range_end, |
aoqi@0 | 189 | e.flags)); |
aoqi@0 | 190 | tab(); |
aoqi@0 | 191 | print(String.format("// %2d, %2d, %4d:%02d, %4d:%02d", |
aoqi@0 | 192 | e.start_pc, e.end_pc, |
aoqi@0 | 193 | (e.character_range_start >> 10), (e.character_range_start & 0x3ff), |
aoqi@0 | 194 | (e.character_range_end >> 10), (e.character_range_end & 0x3ff))); |
aoqi@0 | 195 | if ((e.flags & CharacterRangeTable_attribute.CRT_STATEMENT) != 0) |
aoqi@0 | 196 | print(", statement"); |
aoqi@0 | 197 | if ((e.flags & CharacterRangeTable_attribute.CRT_BLOCK) != 0) |
aoqi@0 | 198 | print(", block"); |
aoqi@0 | 199 | if ((e.flags & CharacterRangeTable_attribute.CRT_ASSIGNMENT) != 0) |
aoqi@0 | 200 | print(", assignment"); |
aoqi@0 | 201 | if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_CONTROLLER) != 0) |
aoqi@0 | 202 | print(", flow-controller"); |
aoqi@0 | 203 | if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_TARGET) != 0) |
aoqi@0 | 204 | print(", flow-target"); |
aoqi@0 | 205 | if ((e.flags & CharacterRangeTable_attribute.CRT_INVOKE) != 0) |
aoqi@0 | 206 | print(", invoke"); |
aoqi@0 | 207 | if ((e.flags & CharacterRangeTable_attribute.CRT_CREATE) != 0) |
aoqi@0 | 208 | print(", create"); |
aoqi@0 | 209 | if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_TRUE) != 0) |
aoqi@0 | 210 | print(", branch-true"); |
aoqi@0 | 211 | if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_FALSE) != 0) |
aoqi@0 | 212 | print(", branch-false"); |
aoqi@0 | 213 | println(); |
aoqi@0 | 214 | } |
aoqi@0 | 215 | indent(-1); |
aoqi@0 | 216 | return null; |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | public Void visitCode(Code_attribute attr, Void ignore) { |
aoqi@0 | 220 | codeWriter.write(attr, constant_pool); |
aoqi@0 | 221 | return null; |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | public Void visitCompilationID(CompilationID_attribute attr, Void ignore) { |
aoqi@0 | 225 | constantWriter.write(attr.compilationID_index); |
aoqi@0 | 226 | return null; |
aoqi@0 | 227 | } |
aoqi@0 | 228 | |
aoqi@0 | 229 | public Void visitConstantValue(ConstantValue_attribute attr, Void ignore) { |
aoqi@0 | 230 | print("ConstantValue: "); |
aoqi@0 | 231 | constantWriter.write(attr.constantvalue_index); |
aoqi@0 | 232 | println(); |
aoqi@0 | 233 | return null; |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | public Void visitDeprecated(Deprecated_attribute attr, Void ignore) { |
aoqi@0 | 237 | println("Deprecated: true"); |
aoqi@0 | 238 | return null; |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | public Void visitEnclosingMethod(EnclosingMethod_attribute attr, Void ignore) { |
aoqi@0 | 242 | print("EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index); |
aoqi@0 | 243 | tab(); |
aoqi@0 | 244 | print("// " + getJavaClassName(attr)); |
aoqi@0 | 245 | if (attr.method_index != 0) |
aoqi@0 | 246 | print("." + getMethodName(attr)); |
aoqi@0 | 247 | println(); |
aoqi@0 | 248 | return null; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | private String getJavaClassName(EnclosingMethod_attribute a) { |
aoqi@0 | 252 | try { |
aoqi@0 | 253 | return getJavaName(a.getClassName(constant_pool)); |
aoqi@0 | 254 | } catch (ConstantPoolException e) { |
aoqi@0 | 255 | return report(e); |
aoqi@0 | 256 | } |
aoqi@0 | 257 | } |
aoqi@0 | 258 | |
aoqi@0 | 259 | private String getMethodName(EnclosingMethod_attribute a) { |
aoqi@0 | 260 | try { |
aoqi@0 | 261 | return a.getMethodName(constant_pool); |
aoqi@0 | 262 | } catch (ConstantPoolException e) { |
aoqi@0 | 263 | return report(e); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | } |
aoqi@0 | 266 | |
aoqi@0 | 267 | public Void visitExceptions(Exceptions_attribute attr, Void ignore) { |
aoqi@0 | 268 | println("Exceptions:"); |
aoqi@0 | 269 | indent(+1); |
aoqi@0 | 270 | print("throws "); |
aoqi@0 | 271 | for (int i = 0; i < attr.number_of_exceptions; i++) { |
aoqi@0 | 272 | if (i > 0) |
aoqi@0 | 273 | print(", "); |
aoqi@0 | 274 | print(getJavaException(attr, i)); |
aoqi@0 | 275 | } |
aoqi@0 | 276 | println(); |
aoqi@0 | 277 | indent(-1); |
aoqi@0 | 278 | return null; |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | private String getJavaException(Exceptions_attribute attr, int index) { |
aoqi@0 | 282 | try { |
aoqi@0 | 283 | return getJavaName(attr.getException(index, constant_pool)); |
aoqi@0 | 284 | } catch (ConstantPoolException e) { |
aoqi@0 | 285 | return report(e); |
aoqi@0 | 286 | } |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) { |
aoqi@0 | 290 | boolean first = true; |
aoqi@0 | 291 | for (int i = 0 ; i < attr.classes.length; i++) { |
aoqi@0 | 292 | InnerClasses_attribute.Info info = attr.classes[i]; |
aoqi@0 | 293 | //access |
aoqi@0 | 294 | AccessFlags access_flags = info.inner_class_access_flags; |
aoqi@0 | 295 | if (options.checkAccess(access_flags)) { |
aoqi@0 | 296 | if (first) { |
aoqi@0 | 297 | writeInnerClassHeader(); |
aoqi@0 | 298 | first = false; |
aoqi@0 | 299 | } |
aoqi@0 | 300 | print(" "); |
aoqi@0 | 301 | for (String name: access_flags.getInnerClassModifiers()) |
aoqi@0 | 302 | print(name + " "); |
aoqi@0 | 303 | if (info.inner_name_index!=0) { |
aoqi@0 | 304 | print("#" + info.inner_name_index + "= "); |
aoqi@0 | 305 | } |
aoqi@0 | 306 | print("#" + info.inner_class_info_index); |
aoqi@0 | 307 | if (info.outer_class_info_index != 0) { |
aoqi@0 | 308 | print(" of #" + info.outer_class_info_index); |
aoqi@0 | 309 | } |
aoqi@0 | 310 | print("; //"); |
aoqi@0 | 311 | if (info.inner_name_index != 0) { |
aoqi@0 | 312 | print(getInnerName(constant_pool, info) + "="); |
aoqi@0 | 313 | } |
aoqi@0 | 314 | constantWriter.write(info.inner_class_info_index); |
aoqi@0 | 315 | if (info.outer_class_info_index != 0) { |
aoqi@0 | 316 | print(" of "); |
aoqi@0 | 317 | constantWriter.write(info.outer_class_info_index); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | println(); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | } |
aoqi@0 | 322 | if (!first) |
aoqi@0 | 323 | indent(-1); |
aoqi@0 | 324 | return null; |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | String getInnerName(ConstantPool constant_pool, InnerClasses_attribute.Info info) { |
aoqi@0 | 328 | try { |
aoqi@0 | 329 | return info.getInnerName(constant_pool); |
aoqi@0 | 330 | } catch (ConstantPoolException e) { |
aoqi@0 | 331 | return report(e); |
aoqi@0 | 332 | } |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | private void writeInnerClassHeader() { |
aoqi@0 | 336 | println("InnerClasses:"); |
aoqi@0 | 337 | indent(+1); |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | public Void visitLineNumberTable(LineNumberTable_attribute attr, Void ignore) { |
aoqi@0 | 341 | println("LineNumberTable:"); |
aoqi@0 | 342 | indent(+1); |
aoqi@0 | 343 | for (LineNumberTable_attribute.Entry entry: attr.line_number_table) { |
aoqi@0 | 344 | println("line " + entry.line_number + ": " + entry.start_pc); |
aoqi@0 | 345 | } |
aoqi@0 | 346 | indent(-1); |
aoqi@0 | 347 | return null; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | |
aoqi@0 | 350 | public Void visitLocalVariableTable(LocalVariableTable_attribute attr, Void ignore) { |
aoqi@0 | 351 | println("LocalVariableTable:"); |
aoqi@0 | 352 | indent(+1); |
aoqi@0 | 353 | println("Start Length Slot Name Signature"); |
aoqi@0 | 354 | for (LocalVariableTable_attribute.Entry entry : attr.local_variable_table) { |
aoqi@0 | 355 | println(String.format("%5d %7d %5d %5s %s", |
aoqi@0 | 356 | entry.start_pc, entry.length, entry.index, |
aoqi@0 | 357 | constantWriter.stringValue(entry.name_index), |
aoqi@0 | 358 | constantWriter.stringValue(entry.descriptor_index))); |
aoqi@0 | 359 | } |
aoqi@0 | 360 | indent(-1); |
aoqi@0 | 361 | return null; |
aoqi@0 | 362 | } |
aoqi@0 | 363 | |
aoqi@0 | 364 | public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, Void ignore) { |
aoqi@0 | 365 | println("LocalVariableTypeTable:"); |
aoqi@0 | 366 | indent(+1); |
aoqi@0 | 367 | println("Start Length Slot Name Signature"); |
aoqi@0 | 368 | for (LocalVariableTypeTable_attribute.Entry entry : attr.local_variable_table) { |
aoqi@0 | 369 | println(String.format("%5d %7d %5d %5s %s", |
aoqi@0 | 370 | entry.start_pc, entry.length, entry.index, |
aoqi@0 | 371 | constantWriter.stringValue(entry.name_index), |
aoqi@0 | 372 | constantWriter.stringValue(entry.signature_index))); |
aoqi@0 | 373 | } |
aoqi@0 | 374 | indent(-1); |
aoqi@0 | 375 | return null; |
aoqi@0 | 376 | } |
aoqi@0 | 377 | |
aoqi@0 | 378 | private static final String format = "%-31s%s"; |
aoqi@0 | 379 | |
aoqi@0 | 380 | public Void visitMethodParameters(MethodParameters_attribute attr, |
aoqi@0 | 381 | Void ignore) { |
aoqi@0 | 382 | |
aoqi@0 | 383 | final String header = String.format(format, "Name", "Flags"); |
aoqi@0 | 384 | println("MethodParameters:"); |
aoqi@0 | 385 | indent(+1); |
aoqi@0 | 386 | println(header); |
aoqi@0 | 387 | for (MethodParameters_attribute.Entry entry : |
aoqi@0 | 388 | attr.method_parameter_table) { |
aoqi@0 | 389 | String namestr = |
aoqi@0 | 390 | entry.name_index != 0 ? |
aoqi@0 | 391 | constantWriter.stringValue(entry.name_index) : "<no name>"; |
aoqi@0 | 392 | String flagstr = |
aoqi@0 | 393 | (0 != (entry.flags & ACC_FINAL) ? "final " : "") + |
aoqi@0 | 394 | (0 != (entry.flags & ACC_MANDATED) ? "mandated " : "") + |
aoqi@0 | 395 | (0 != (entry.flags & ACC_SYNTHETIC) ? "synthetic" : ""); |
aoqi@0 | 396 | println(String.format(format, namestr, flagstr)); |
aoqi@0 | 397 | } |
aoqi@0 | 398 | indent(-1); |
aoqi@0 | 399 | return null; |
aoqi@0 | 400 | } |
aoqi@0 | 401 | |
aoqi@0 | 402 | public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 403 | println("RuntimeVisibleAnnotations:"); |
aoqi@0 | 404 | indent(+1); |
aoqi@0 | 405 | for (int i = 0; i < attr.annotations.length; i++) { |
aoqi@0 | 406 | print(i + ": "); |
aoqi@0 | 407 | annotationWriter.write(attr.annotations[i]); |
aoqi@0 | 408 | println(); |
aoqi@0 | 409 | } |
aoqi@0 | 410 | indent(-1); |
aoqi@0 | 411 | return null; |
aoqi@0 | 412 | } |
aoqi@0 | 413 | |
aoqi@0 | 414 | public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 415 | println("RuntimeInvisibleAnnotations:"); |
aoqi@0 | 416 | indent(+1); |
aoqi@0 | 417 | for (int i = 0; i < attr.annotations.length; i++) { |
aoqi@0 | 418 | print(i + ": "); |
aoqi@0 | 419 | annotationWriter.write(attr.annotations[i]); |
aoqi@0 | 420 | println(); |
aoqi@0 | 421 | } |
aoqi@0 | 422 | indent(-1); |
aoqi@0 | 423 | return null; |
aoqi@0 | 424 | } |
aoqi@0 | 425 | |
aoqi@0 | 426 | public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 427 | println("RuntimeVisibleTypeAnnotations:"); |
aoqi@0 | 428 | indent(+1); |
aoqi@0 | 429 | for (int i = 0; i < attr.annotations.length; i++) { |
aoqi@0 | 430 | print(i + ": "); |
aoqi@0 | 431 | annotationWriter.write(attr.annotations[i]); |
aoqi@0 | 432 | println(); |
aoqi@0 | 433 | } |
aoqi@0 | 434 | indent(-1); |
aoqi@0 | 435 | return null; |
aoqi@0 | 436 | } |
aoqi@0 | 437 | |
aoqi@0 | 438 | public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 439 | println("RuntimeInvisibleTypeAnnotations:"); |
aoqi@0 | 440 | indent(+1); |
aoqi@0 | 441 | for (int i = 0; i < attr.annotations.length; i++) { |
aoqi@0 | 442 | print(i + ": "); |
aoqi@0 | 443 | annotationWriter.write(attr.annotations[i]); |
aoqi@0 | 444 | println(); |
aoqi@0 | 445 | } |
aoqi@0 | 446 | indent(-1); |
aoqi@0 | 447 | return null; |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 451 | println("RuntimeVisibleParameterAnnotations:"); |
aoqi@0 | 452 | indent(+1); |
aoqi@0 | 453 | for (int param = 0; param < attr.parameter_annotations.length; param++) { |
aoqi@0 | 454 | println("parameter " + param + ": "); |
aoqi@0 | 455 | indent(+1); |
aoqi@0 | 456 | for (int i = 0; i < attr.parameter_annotations[param].length; i++) { |
aoqi@0 | 457 | print(i + ": "); |
aoqi@0 | 458 | annotationWriter.write(attr.parameter_annotations[param][i]); |
aoqi@0 | 459 | println(); |
aoqi@0 | 460 | } |
aoqi@0 | 461 | indent(-1); |
aoqi@0 | 462 | } |
aoqi@0 | 463 | indent(-1); |
aoqi@0 | 464 | return null; |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) { |
aoqi@0 | 468 | println("RuntimeInvisibleParameterAnnotations:"); |
aoqi@0 | 469 | indent(+1); |
aoqi@0 | 470 | for (int param = 0; param < attr.parameter_annotations.length; param++) { |
aoqi@0 | 471 | println(param + ": "); |
aoqi@0 | 472 | indent(+1); |
aoqi@0 | 473 | for (int i = 0; i < attr.parameter_annotations[param].length; i++) { |
aoqi@0 | 474 | print(i + ": "); |
aoqi@0 | 475 | annotationWriter.write(attr.parameter_annotations[param][i]); |
aoqi@0 | 476 | println(); |
aoqi@0 | 477 | } |
aoqi@0 | 478 | indent(-1); |
aoqi@0 | 479 | } |
aoqi@0 | 480 | indent(-1); |
aoqi@0 | 481 | return null; |
aoqi@0 | 482 | } |
aoqi@0 | 483 | |
aoqi@0 | 484 | public Void visitSignature(Signature_attribute attr, Void ignore) { |
aoqi@0 | 485 | print("Signature: #" + attr.signature_index); |
aoqi@0 | 486 | tab(); |
aoqi@0 | 487 | println("// " + getSignature(attr)); |
aoqi@0 | 488 | return null; |
aoqi@0 | 489 | } |
aoqi@0 | 490 | |
aoqi@0 | 491 | String getSignature(Signature_attribute info) { |
aoqi@0 | 492 | try { |
aoqi@0 | 493 | return info.getSignature(constant_pool); |
aoqi@0 | 494 | } catch (ConstantPoolException e) { |
aoqi@0 | 495 | return report(e); |
aoqi@0 | 496 | } |
aoqi@0 | 497 | } |
aoqi@0 | 498 | |
aoqi@0 | 499 | public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, Void ignore) { |
aoqi@0 | 500 | println("SourceDebugExtension:"); |
aoqi@0 | 501 | indent(+1); |
aoqi@0 | 502 | for (String s: attr.getValue().split("[\r\n]+")) { |
aoqi@0 | 503 | println(s); |
aoqi@0 | 504 | } |
aoqi@0 | 505 | indent(-1); |
aoqi@0 | 506 | return null; |
aoqi@0 | 507 | } |
aoqi@0 | 508 | |
aoqi@0 | 509 | public Void visitSourceFile(SourceFile_attribute attr, Void ignore) { |
aoqi@0 | 510 | println("SourceFile: \"" + getSourceFile(attr) + "\""); |
aoqi@0 | 511 | return null; |
aoqi@0 | 512 | } |
aoqi@0 | 513 | |
aoqi@0 | 514 | private String getSourceFile(SourceFile_attribute attr) { |
aoqi@0 | 515 | try { |
aoqi@0 | 516 | return attr.getSourceFile(constant_pool); |
aoqi@0 | 517 | } catch (ConstantPoolException e) { |
aoqi@0 | 518 | return report(e); |
aoqi@0 | 519 | } |
aoqi@0 | 520 | } |
aoqi@0 | 521 | |
aoqi@0 | 522 | public Void visitSourceID(SourceID_attribute attr, Void ignore) { |
aoqi@0 | 523 | constantWriter.write(attr.sourceID_index); |
aoqi@0 | 524 | return null; |
aoqi@0 | 525 | } |
aoqi@0 | 526 | |
aoqi@0 | 527 | public Void visitStackMap(StackMap_attribute attr, Void ignore) { |
aoqi@0 | 528 | println("StackMap: number_of_entries = " + attr.number_of_entries); |
aoqi@0 | 529 | indent(+1); |
aoqi@0 | 530 | StackMapTableWriter w = new StackMapTableWriter(); |
aoqi@0 | 531 | for (StackMapTable_attribute.stack_map_frame entry : attr.entries) { |
aoqi@0 | 532 | w.write(entry); |
aoqi@0 | 533 | } |
aoqi@0 | 534 | indent(-1); |
aoqi@0 | 535 | return null; |
aoqi@0 | 536 | } |
aoqi@0 | 537 | |
aoqi@0 | 538 | public Void visitStackMapTable(StackMapTable_attribute attr, Void ignore) { |
aoqi@0 | 539 | println("StackMapTable: number_of_entries = " + attr.number_of_entries); |
aoqi@0 | 540 | indent(+1); |
aoqi@0 | 541 | StackMapTableWriter w = new StackMapTableWriter(); |
aoqi@0 | 542 | for (StackMapTable_attribute.stack_map_frame entry : attr.entries) { |
aoqi@0 | 543 | w.write(entry); |
aoqi@0 | 544 | } |
aoqi@0 | 545 | indent(-1); |
aoqi@0 | 546 | return null; |
aoqi@0 | 547 | } |
aoqi@0 | 548 | |
aoqi@0 | 549 | class StackMapTableWriter // also handles CLDC StackMap attributes |
aoqi@0 | 550 | implements StackMapTable_attribute.stack_map_frame.Visitor<Void,Void> { |
aoqi@0 | 551 | public void write(StackMapTable_attribute.stack_map_frame frame) { |
aoqi@0 | 552 | frame.accept(this, null); |
aoqi@0 | 553 | } |
aoqi@0 | 554 | |
aoqi@0 | 555 | public Void visit_same_frame(StackMapTable_attribute.same_frame frame, Void p) { |
aoqi@0 | 556 | printHeader(frame, "/* same */"); |
aoqi@0 | 557 | return null; |
aoqi@0 | 558 | } |
aoqi@0 | 559 | |
aoqi@0 | 560 | public Void visit_same_locals_1_stack_item_frame(StackMapTable_attribute.same_locals_1_stack_item_frame frame, Void p) { |
aoqi@0 | 561 | printHeader(frame, "/* same_locals_1_stack_item */"); |
aoqi@0 | 562 | indent(+1); |
aoqi@0 | 563 | printMap("stack", frame.stack); |
aoqi@0 | 564 | indent(-1); |
aoqi@0 | 565 | return null; |
aoqi@0 | 566 | } |
aoqi@0 | 567 | |
aoqi@0 | 568 | public Void visit_same_locals_1_stack_item_frame_extended(StackMapTable_attribute.same_locals_1_stack_item_frame_extended frame, Void p) { |
aoqi@0 | 569 | printHeader(frame, "/* same_locals_1_stack_item_frame_extended */"); |
aoqi@0 | 570 | indent(+1); |
aoqi@0 | 571 | println("offset_delta = " + frame.offset_delta); |
aoqi@0 | 572 | printMap("stack", frame.stack); |
aoqi@0 | 573 | indent(-1); |
aoqi@0 | 574 | return null; |
aoqi@0 | 575 | } |
aoqi@0 | 576 | |
aoqi@0 | 577 | public Void visit_chop_frame(StackMapTable_attribute.chop_frame frame, Void p) { |
aoqi@0 | 578 | printHeader(frame, "/* chop */"); |
aoqi@0 | 579 | indent(+1); |
aoqi@0 | 580 | println("offset_delta = " + frame.offset_delta); |
aoqi@0 | 581 | indent(-1); |
aoqi@0 | 582 | return null; |
aoqi@0 | 583 | } |
aoqi@0 | 584 | |
aoqi@0 | 585 | public Void visit_same_frame_extended(StackMapTable_attribute.same_frame_extended frame, Void p) { |
aoqi@0 | 586 | printHeader(frame, "/* same_frame_extended */"); |
aoqi@0 | 587 | indent(+1); |
aoqi@0 | 588 | println("offset_delta = " + frame.offset_delta); |
aoqi@0 | 589 | indent(-1); |
aoqi@0 | 590 | return null; |
aoqi@0 | 591 | } |
aoqi@0 | 592 | |
aoqi@0 | 593 | public Void visit_append_frame(StackMapTable_attribute.append_frame frame, Void p) { |
aoqi@0 | 594 | printHeader(frame, "/* append */"); |
aoqi@0 | 595 | indent(+1); |
aoqi@0 | 596 | println("offset_delta = " + frame.offset_delta); |
aoqi@0 | 597 | printMap("locals", frame.locals); |
aoqi@0 | 598 | indent(-1); |
aoqi@0 | 599 | return null; |
aoqi@0 | 600 | } |
aoqi@0 | 601 | |
aoqi@0 | 602 | public Void visit_full_frame(StackMapTable_attribute.full_frame frame, Void p) { |
aoqi@0 | 603 | if (frame instanceof StackMap_attribute.stack_map_frame) { |
aoqi@0 | 604 | printHeader(frame, "offset = " + frame.offset_delta); |
aoqi@0 | 605 | indent(+1); |
aoqi@0 | 606 | } else { |
aoqi@0 | 607 | printHeader(frame, "/* full_frame */"); |
aoqi@0 | 608 | indent(+1); |
aoqi@0 | 609 | println("offset_delta = " + frame.offset_delta); |
aoqi@0 | 610 | } |
aoqi@0 | 611 | printMap("locals", frame.locals); |
aoqi@0 | 612 | printMap("stack", frame.stack); |
aoqi@0 | 613 | indent(-1); |
aoqi@0 | 614 | return null; |
aoqi@0 | 615 | } |
aoqi@0 | 616 | |
aoqi@0 | 617 | void printHeader(StackMapTable_attribute.stack_map_frame frame, String extra) { |
aoqi@0 | 618 | print("frame_type = " + frame.frame_type + " "); |
aoqi@0 | 619 | println(extra); |
aoqi@0 | 620 | } |
aoqi@0 | 621 | |
aoqi@0 | 622 | void printMap(String name, StackMapTable_attribute.verification_type_info[] map) { |
aoqi@0 | 623 | print(name + " = ["); |
aoqi@0 | 624 | for (int i = 0; i < map.length; i++) { |
aoqi@0 | 625 | StackMapTable_attribute.verification_type_info info = map[i]; |
aoqi@0 | 626 | int tag = info.tag; |
aoqi@0 | 627 | switch (tag) { |
aoqi@0 | 628 | case StackMapTable_attribute.verification_type_info.ITEM_Object: |
aoqi@0 | 629 | print(" "); |
aoqi@0 | 630 | constantWriter.write(((StackMapTable_attribute.Object_variable_info) info).cpool_index); |
aoqi@0 | 631 | break; |
aoqi@0 | 632 | case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized: |
aoqi@0 | 633 | print(" " + mapTypeName(tag)); |
aoqi@0 | 634 | print(" " + ((StackMapTable_attribute.Uninitialized_variable_info) info).offset); |
aoqi@0 | 635 | break; |
aoqi@0 | 636 | default: |
aoqi@0 | 637 | print(" " + mapTypeName(tag)); |
aoqi@0 | 638 | } |
aoqi@0 | 639 | print(i == (map.length - 1) ? " " : ","); |
aoqi@0 | 640 | } |
aoqi@0 | 641 | println("]"); |
aoqi@0 | 642 | } |
aoqi@0 | 643 | |
aoqi@0 | 644 | String mapTypeName(int tag) { |
aoqi@0 | 645 | switch (tag) { |
aoqi@0 | 646 | case StackMapTable_attribute.verification_type_info.ITEM_Top: |
aoqi@0 | 647 | return "top"; |
aoqi@0 | 648 | |
aoqi@0 | 649 | case StackMapTable_attribute.verification_type_info.ITEM_Integer: |
aoqi@0 | 650 | return "int"; |
aoqi@0 | 651 | |
aoqi@0 | 652 | case StackMapTable_attribute.verification_type_info.ITEM_Float: |
aoqi@0 | 653 | return "float"; |
aoqi@0 | 654 | |
aoqi@0 | 655 | case StackMapTable_attribute.verification_type_info.ITEM_Long: |
aoqi@0 | 656 | return "long"; |
aoqi@0 | 657 | |
aoqi@0 | 658 | case StackMapTable_attribute.verification_type_info.ITEM_Double: |
aoqi@0 | 659 | return "double"; |
aoqi@0 | 660 | |
aoqi@0 | 661 | case StackMapTable_attribute.verification_type_info.ITEM_Null: |
aoqi@0 | 662 | return "null"; |
aoqi@0 | 663 | |
aoqi@0 | 664 | case StackMapTable_attribute.verification_type_info.ITEM_UninitializedThis: |
aoqi@0 | 665 | return "this"; |
aoqi@0 | 666 | |
aoqi@0 | 667 | case StackMapTable_attribute.verification_type_info.ITEM_Object: |
aoqi@0 | 668 | return "CP"; |
aoqi@0 | 669 | |
aoqi@0 | 670 | case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized: |
aoqi@0 | 671 | return "uninitialized"; |
aoqi@0 | 672 | |
aoqi@0 | 673 | default: |
aoqi@0 | 674 | report("unrecognized verification_type_info tag: " + tag); |
aoqi@0 | 675 | return "[tag:" + tag + "]"; |
aoqi@0 | 676 | } |
aoqi@0 | 677 | } |
aoqi@0 | 678 | } |
aoqi@0 | 679 | |
aoqi@0 | 680 | public Void visitSynthetic(Synthetic_attribute attr, Void ignore) { |
aoqi@0 | 681 | println("Synthetic: true"); |
aoqi@0 | 682 | return null; |
aoqi@0 | 683 | } |
aoqi@0 | 684 | |
aoqi@0 | 685 | static String getJavaName(String name) { |
aoqi@0 | 686 | return name.replace('/', '.'); |
aoqi@0 | 687 | } |
aoqi@0 | 688 | |
aoqi@0 | 689 | String toHex(byte b, int w) { |
aoqi@0 | 690 | return toHex(b & 0xff, w); |
aoqi@0 | 691 | } |
aoqi@0 | 692 | |
aoqi@0 | 693 | static String toHex(int i) { |
aoqi@0 | 694 | return StringUtils.toUpperCase(Integer.toString(i, 16)); |
aoqi@0 | 695 | } |
aoqi@0 | 696 | |
aoqi@0 | 697 | static String toHex(int i, int w) { |
aoqi@0 | 698 | String s = StringUtils.toUpperCase(Integer.toHexString(i)); |
aoqi@0 | 699 | while (s.length() < w) |
aoqi@0 | 700 | s = "0" + s; |
aoqi@0 | 701 | return StringUtils.toUpperCase(s); |
aoqi@0 | 702 | } |
aoqi@0 | 703 | |
aoqi@0 | 704 | private AnnotationWriter annotationWriter; |
aoqi@0 | 705 | private CodeWriter codeWriter; |
aoqi@0 | 706 | private ConstantWriter constantWriter; |
aoqi@0 | 707 | private Options options; |
aoqi@0 | 708 | |
aoqi@0 | 709 | private ConstantPool constant_pool; |
aoqi@0 | 710 | private Object owner; |
aoqi@0 | 711 | } |