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

Wed, 18 Jun 2008 16:53:08 -0700

author
jjg
date
Wed, 18 Jun 2008 16:53:08 -0700
changeset 52
3cb4fb6e0720
parent 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

6715767: javap on java.lang.ClassLoader crashes
Reviewed-by: ksrini

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

mercurial