src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
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.classfile;
jjg@46 27
jjg@46 28 import java.io.IOException;
jjg@46 29
jjg@46 30 /**
jjg@46 31 * See JVMS3, section 4.8.4.
jjg@46 32 *
jjg@46 33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 34 * you write code that depends on this, you do so at your own risk.
jjg@46 35 * This code and its internal interfaces are subject to change or
jjg@46 36 * deletion without notice.</b>
jjg@46 37 */
jjg@46 38 public class StackMapTable_attribute extends Attribute {
jjg@46 39 static class InvalidStackMap extends AttributeException {
jjg@46 40 InvalidStackMap(String msg) {
jjg@46 41 super(msg);
jjg@46 42 }
jjg@46 43 }
jjg@46 44
jjg@46 45 StackMapTable_attribute(ClassReader cr, int name_index, int length)
jjg@46 46 throws IOException, InvalidStackMap {
jjg@46 47 super(name_index, length);
jjg@46 48 number_of_entries = cr.readUnsignedShort();
jjg@46 49 entries = new stack_map_frame[number_of_entries];
jjg@46 50 for (int i = 0; i < number_of_entries; i++)
jjg@46 51 entries[i] = stack_map_frame.read(cr);
jjg@46 52 }
jjg@46 53
jjg@46 54 public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries)
jjg@46 55 throws ConstantPoolException {
jjg@46 56 this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries);
jjg@46 57 }
jjg@46 58
jjg@46 59 public StackMapTable_attribute(int name_index, stack_map_frame[] entries) {
jjg@46 60 super(name_index, length(entries));
jjg@46 61 this.number_of_entries = entries.length;
jjg@46 62 this.entries = entries;
jjg@46 63 }
jjg@46 64
jjg@46 65 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 66 return visitor.visitStackMapTable(this, data);
jjg@46 67 }
jjg@46 68
jjg@46 69 static int length(stack_map_frame[] entries) {
jjg@46 70 int n = 2;
jjg@46 71 for (stack_map_frame entry: entries)
jjg@46 72 n += entry.length();
jjg@46 73 return n;
jjg@46 74 }
jjg@46 75
jjg@46 76 public final int number_of_entries;
jjg@46 77 public final stack_map_frame entries[];
jjg@46 78
jjg@46 79 public static abstract class stack_map_frame {
jjg@46 80 static stack_map_frame read(ClassReader cr)
jjg@46 81 throws IOException, InvalidStackMap {
jjg@46 82 int frame_type = cr.readUnsignedByte();
jjg@46 83 if (frame_type <= 63)
jjg@46 84 return new same_frame(frame_type);
jjg@46 85 else if (frame_type <= 127)
jjg@46 86 return new same_locals_1_stack_item_frame(frame_type, cr);
jjg@46 87 else if (frame_type <= 246)
jjg@46 88 throw new Error("unknown frame_type " + frame_type);
jjg@46 89 else if (frame_type == 247)
jjg@46 90 return new same_locals_1_stack_item_frame_extended(frame_type, cr);
jjg@46 91 else if (frame_type <= 250)
jjg@46 92 return new chop_frame(frame_type, cr);
jjg@46 93 else if (frame_type == 251)
jjg@46 94 return new same_frame_extended(frame_type, cr);
jjg@46 95 else if (frame_type <= 254)
jjg@46 96 return new append_frame(frame_type, cr);
jjg@46 97 else
jjg@46 98 return new full_frame(frame_type, cr);
jjg@46 99 }
jjg@46 100
jjg@46 101 protected stack_map_frame(int frame_type) {
jjg@46 102 this.frame_type = frame_type;
jjg@46 103 }
jjg@46 104
jjg@46 105 public int length() {
jjg@46 106 return 1;
jjg@46 107 }
jjg@46 108
jjg@46 109 public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
jjg@46 110
jjg@46 111 public final int frame_type;
jjg@46 112
jjg@46 113 public static interface Visitor<R,P> {
jjg@46 114 R visit_same_frame(same_frame frame, P p);
jjg@46 115 R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p);
jjg@46 116 R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p);
jjg@46 117 R visit_chop_frame(chop_frame frame, P p);
jjg@46 118 R visit_same_frame_extended(same_frame_extended frame, P p);
jjg@46 119 R visit_append_frame(append_frame frame, P p);
jjg@46 120 R visit_full_frame(full_frame frame, P p);
jjg@46 121 }
jjg@46 122 }
jjg@46 123
jjg@46 124 public static class same_frame extends stack_map_frame {
jjg@46 125 same_frame(int frame_type) {
jjg@46 126 super(frame_type);
jjg@46 127 }
jjg@46 128
jjg@46 129 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 130 return visitor.visit_same_frame(this, data);
jjg@46 131 }
jjg@46 132 }
jjg@46 133
jjg@46 134 public static class same_locals_1_stack_item_frame extends stack_map_frame {
jjg@46 135 same_locals_1_stack_item_frame(int frame_type, ClassReader cr)
jjg@46 136 throws IOException, InvalidStackMap {
jjg@46 137 super(frame_type);
jjg@46 138 stack = new verification_type_info[1];
jjg@46 139 stack[0] = verification_type_info.read(cr);
jjg@46 140 }
jjg@46 141
jjg@46 142 @Override
jjg@46 143 public int length() {
jjg@46 144 return super.length() + stack[0].length();
jjg@46 145 }
jjg@46 146
jjg@46 147 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 148 return visitor.visit_same_locals_1_stack_item_frame(this, data);
jjg@46 149 }
jjg@46 150
jjg@46 151 public final verification_type_info[] stack;
jjg@46 152 }
jjg@46 153
jjg@46 154 public static class same_locals_1_stack_item_frame_extended extends stack_map_frame {
jjg@46 155 same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr)
jjg@46 156 throws IOException, InvalidStackMap {
jjg@46 157 super(frame_type);
jjg@46 158 offset_delta = cr.readUnsignedShort();
jjg@46 159 stack = new verification_type_info[1];
jjg@46 160 stack[0] = verification_type_info.read(cr);
jjg@46 161 }
jjg@46 162
jjg@46 163 @Override
jjg@46 164 public int length() {
jjg@46 165 return super.length() + 2 + stack[0].length();
jjg@46 166 }
jjg@46 167
jjg@46 168 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 169 return visitor.visit_same_locals_1_stack_item_frame_extended(this, data);
jjg@46 170 }
jjg@46 171
jjg@46 172 public final int offset_delta;
jjg@46 173 public final verification_type_info[] stack;
jjg@46 174 }
jjg@46 175
jjg@46 176 public static class chop_frame extends stack_map_frame {
jjg@46 177 chop_frame(int frame_type, ClassReader cr) throws IOException {
jjg@46 178 super(frame_type);
jjg@46 179 offset_delta = cr.readUnsignedShort();
jjg@46 180 }
jjg@46 181
jjg@46 182 @Override
jjg@46 183 public int length() {
jjg@46 184 return super.length() + 2;
jjg@46 185 }
jjg@46 186
jjg@46 187 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 188 return visitor.visit_chop_frame(this, data);
jjg@46 189 }
jjg@46 190
jjg@46 191 public final int offset_delta;
jjg@46 192 }
jjg@46 193
jjg@46 194 public static class same_frame_extended extends stack_map_frame {
jjg@46 195 same_frame_extended(int frame_type, ClassReader cr) throws IOException {
jjg@46 196 super(frame_type);
jjg@46 197 offset_delta = cr.readUnsignedShort();
jjg@46 198 }
jjg@46 199
jjg@46 200 @Override
jjg@46 201 public int length() {
jjg@46 202 return super.length() + 2;
jjg@46 203 }
jjg@46 204
jjg@46 205 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 206 return visitor.visit_same_frame_extended(this, data);
jjg@46 207 }
jjg@46 208
jjg@46 209 public final int offset_delta;
jjg@46 210 }
jjg@46 211
jjg@46 212 public static class append_frame extends stack_map_frame {
jjg@46 213 append_frame(int frame_type, ClassReader cr)
jjg@46 214 throws IOException, InvalidStackMap {
jjg@46 215 super(frame_type);
jjg@46 216 offset_delta = cr.readUnsignedShort();
jjg@46 217 locals = new verification_type_info[frame_type - 251];
jjg@46 218 for (int i = 0; i < locals.length; i++)
jjg@46 219 locals[i] = verification_type_info.read(cr);
jjg@46 220 }
jjg@46 221
jjg@46 222 @Override
jjg@46 223 public int length() {
jjg@46 224 int n = super.length() + 2;
jjg@46 225 for (verification_type_info local: locals)
jjg@46 226 n += local.length();
jjg@46 227 return n;
jjg@46 228 }
jjg@46 229
jjg@46 230 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 231 return visitor.visit_append_frame(this, data);
jjg@46 232 }
jjg@46 233
jjg@46 234 public final int offset_delta;
jjg@46 235 public final verification_type_info[] locals;
jjg@46 236 }
jjg@46 237
jjg@46 238 public static class full_frame extends stack_map_frame {
jjg@46 239 full_frame(int frame_type, ClassReader cr)
jjg@46 240 throws IOException, InvalidStackMap {
jjg@46 241 super(frame_type);
jjg@46 242 offset_delta = cr.readUnsignedShort();
jjg@46 243 number_of_locals = cr.readUnsignedShort();
jjg@46 244 locals = new verification_type_info[number_of_locals];
jjg@46 245 for (int i = 0; i < locals.length; i++)
jjg@46 246 locals[i] = verification_type_info.read(cr);
jjg@46 247 number_of_stack_items = cr.readUnsignedShort();
jjg@46 248 stack = new verification_type_info[number_of_stack_items];
jjg@46 249 for (int i = 0; i < stack.length; i++)
jjg@46 250 stack[i] = verification_type_info.read(cr);
jjg@46 251 }
jjg@46 252
jjg@46 253 @Override
jjg@46 254 public int length() {
jjg@46 255 int n = super.length() + 2;
jjg@46 256 for (verification_type_info local: locals)
jjg@46 257 n += local.length();
jjg@46 258 n += 2;
jjg@46 259 for (verification_type_info item: stack)
jjg@46 260 n += item.length();
jjg@46 261 return n;
jjg@46 262 }
jjg@46 263
jjg@46 264 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 265 return visitor.visit_full_frame(this, data);
jjg@46 266 }
jjg@46 267
jjg@46 268 public final int offset_delta;
jjg@46 269 public final int number_of_locals;
jjg@46 270 public final verification_type_info[] locals;
jjg@46 271 public final int number_of_stack_items;
jjg@46 272 public final verification_type_info[] stack;
jjg@46 273 }
jjg@46 274
jjg@46 275 public static class verification_type_info {
jjg@46 276 public static final int ITEM_Top = 0;
jjg@46 277 public static final int ITEM_Integer = 1;
jjg@46 278 public static final int ITEM_Float = 2;
jjg@46 279 public static final int ITEM_Long = 4;
jjg@46 280 public static final int ITEM_Double = 3;
jjg@46 281 public static final int ITEM_Null = 5;
jjg@46 282 public static final int ITEM_UninitializedThis = 6;
jjg@46 283 public static final int ITEM_Object = 7;
jjg@46 284 public static final int ITEM_Uninitialized = 8;
jjg@46 285
jjg@46 286 static verification_type_info read(ClassReader cr)
jjg@46 287 throws IOException, InvalidStackMap {
jjg@46 288 int tag = cr.readUnsignedByte();
jjg@46 289 switch (tag) {
jjg@46 290 case ITEM_Top:
jjg@46 291 case ITEM_Integer:
jjg@46 292 case ITEM_Float:
jjg@46 293 case ITEM_Long:
jjg@46 294 case ITEM_Double:
jjg@46 295 case ITEM_Null:
jjg@46 296 case ITEM_UninitializedThis:
jjg@46 297 return new verification_type_info(tag);
jjg@46 298
jjg@46 299 case ITEM_Object:
jjg@46 300 return new Object_variable_info(cr);
jjg@46 301
jjg@46 302 case ITEM_Uninitialized:
jjg@46 303 return new Uninitialized_variable_info(cr);
jjg@46 304
jjg@46 305 default:
jjg@46 306 throw new InvalidStackMap("unrecognized verification_type_info tag");
jjg@46 307 }
jjg@46 308 }
jjg@46 309
jjg@46 310 verification_type_info(int tag) {
jjg@46 311 this.tag = tag;
jjg@46 312 }
jjg@46 313
jjg@46 314 public int length() {
jjg@46 315 return 1;
jjg@46 316 }
jjg@46 317
jjg@46 318 public final int tag;
jjg@46 319 }
jjg@46 320
jjg@46 321 public static class Object_variable_info extends verification_type_info {
jjg@46 322 Object_variable_info(ClassReader cr) throws IOException {
jjg@46 323 super(ITEM_Object);
jjg@46 324 cpool_index = cr.readUnsignedShort();
jjg@46 325 }
jjg@46 326
jjg@46 327 @Override
jjg@46 328 public int length() {
jjg@46 329 return super.length() + 2;
jjg@46 330 }
jjg@46 331
jjg@46 332 public final int cpool_index;
jjg@46 333 }
jjg@46 334
jjg@46 335 public static class Uninitialized_variable_info extends verification_type_info {
jjg@46 336 Uninitialized_variable_info(ClassReader cr) throws IOException {
jjg@46 337 super(ITEM_Uninitialized);
jjg@46 338 offset = cr.readUnsignedShort();
jjg@46 339 }
jjg@46 340
jjg@46 341 @Override
jjg@46 342 public int length() {
jjg@46 343 return super.length() + 2;
jjg@46 344 }
jjg@46 345
jjg@46 346 public final int offset;
jjg@46 347
jjg@46 348 }
jjg@46 349 }

mercurial