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