1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java Tue Jun 03 13:26:47 2008 -0700 1.3 @@ -0,0 +1,349 @@ 1.4 +/* 1.5 + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.classfile; 1.30 + 1.31 +import java.io.IOException; 1.32 + 1.33 +/** 1.34 + * See JVMS3, section 4.8.4. 1.35 + * 1.36 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.37 + * you write code that depends on this, you do so at your own risk. 1.38 + * This code and its internal interfaces are subject to change or 1.39 + * deletion without notice.</b> 1.40 + */ 1.41 +public class StackMapTable_attribute extends Attribute { 1.42 + static class InvalidStackMap extends AttributeException { 1.43 + InvalidStackMap(String msg) { 1.44 + super(msg); 1.45 + } 1.46 + } 1.47 + 1.48 + StackMapTable_attribute(ClassReader cr, int name_index, int length) 1.49 + throws IOException, InvalidStackMap { 1.50 + super(name_index, length); 1.51 + number_of_entries = cr.readUnsignedShort(); 1.52 + entries = new stack_map_frame[number_of_entries]; 1.53 + for (int i = 0; i < number_of_entries; i++) 1.54 + entries[i] = stack_map_frame.read(cr); 1.55 + } 1.56 + 1.57 + public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries) 1.58 + throws ConstantPoolException { 1.59 + this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries); 1.60 + } 1.61 + 1.62 + public StackMapTable_attribute(int name_index, stack_map_frame[] entries) { 1.63 + super(name_index, length(entries)); 1.64 + this.number_of_entries = entries.length; 1.65 + this.entries = entries; 1.66 + } 1.67 + 1.68 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.69 + return visitor.visitStackMapTable(this, data); 1.70 + } 1.71 + 1.72 + static int length(stack_map_frame[] entries) { 1.73 + int n = 2; 1.74 + for (stack_map_frame entry: entries) 1.75 + n += entry.length(); 1.76 + return n; 1.77 + } 1.78 + 1.79 + public final int number_of_entries; 1.80 + public final stack_map_frame entries[]; 1.81 + 1.82 + public static abstract class stack_map_frame { 1.83 + static stack_map_frame read(ClassReader cr) 1.84 + throws IOException, InvalidStackMap { 1.85 + int frame_type = cr.readUnsignedByte(); 1.86 + if (frame_type <= 63) 1.87 + return new same_frame(frame_type); 1.88 + else if (frame_type <= 127) 1.89 + return new same_locals_1_stack_item_frame(frame_type, cr); 1.90 + else if (frame_type <= 246) 1.91 + throw new Error("unknown frame_type " + frame_type); 1.92 + else if (frame_type == 247) 1.93 + return new same_locals_1_stack_item_frame_extended(frame_type, cr); 1.94 + else if (frame_type <= 250) 1.95 + return new chop_frame(frame_type, cr); 1.96 + else if (frame_type == 251) 1.97 + return new same_frame_extended(frame_type, cr); 1.98 + else if (frame_type <= 254) 1.99 + return new append_frame(frame_type, cr); 1.100 + else 1.101 + return new full_frame(frame_type, cr); 1.102 + } 1.103 + 1.104 + protected stack_map_frame(int frame_type) { 1.105 + this.frame_type = frame_type; 1.106 + } 1.107 + 1.108 + public int length() { 1.109 + return 1; 1.110 + } 1.111 + 1.112 + public abstract <R,D> R accept(Visitor<R,D> visitor, D data); 1.113 + 1.114 + public final int frame_type; 1.115 + 1.116 + public static interface Visitor<R,P> { 1.117 + R visit_same_frame(same_frame frame, P p); 1.118 + R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p); 1.119 + R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p); 1.120 + R visit_chop_frame(chop_frame frame, P p); 1.121 + R visit_same_frame_extended(same_frame_extended frame, P p); 1.122 + R visit_append_frame(append_frame frame, P p); 1.123 + R visit_full_frame(full_frame frame, P p); 1.124 + } 1.125 + } 1.126 + 1.127 + public static class same_frame extends stack_map_frame { 1.128 + same_frame(int frame_type) { 1.129 + super(frame_type); 1.130 + } 1.131 + 1.132 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.133 + return visitor.visit_same_frame(this, data); 1.134 + } 1.135 + } 1.136 + 1.137 + public static class same_locals_1_stack_item_frame extends stack_map_frame { 1.138 + same_locals_1_stack_item_frame(int frame_type, ClassReader cr) 1.139 + throws IOException, InvalidStackMap { 1.140 + super(frame_type); 1.141 + stack = new verification_type_info[1]; 1.142 + stack[0] = verification_type_info.read(cr); 1.143 + } 1.144 + 1.145 + @Override 1.146 + public int length() { 1.147 + return super.length() + stack[0].length(); 1.148 + } 1.149 + 1.150 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.151 + return visitor.visit_same_locals_1_stack_item_frame(this, data); 1.152 + } 1.153 + 1.154 + public final verification_type_info[] stack; 1.155 + } 1.156 + 1.157 + public static class same_locals_1_stack_item_frame_extended extends stack_map_frame { 1.158 + same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr) 1.159 + throws IOException, InvalidStackMap { 1.160 + super(frame_type); 1.161 + offset_delta = cr.readUnsignedShort(); 1.162 + stack = new verification_type_info[1]; 1.163 + stack[0] = verification_type_info.read(cr); 1.164 + } 1.165 + 1.166 + @Override 1.167 + public int length() { 1.168 + return super.length() + 2 + stack[0].length(); 1.169 + } 1.170 + 1.171 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.172 + return visitor.visit_same_locals_1_stack_item_frame_extended(this, data); 1.173 + } 1.174 + 1.175 + public final int offset_delta; 1.176 + public final verification_type_info[] stack; 1.177 + } 1.178 + 1.179 + public static class chop_frame extends stack_map_frame { 1.180 + chop_frame(int frame_type, ClassReader cr) throws IOException { 1.181 + super(frame_type); 1.182 + offset_delta = cr.readUnsignedShort(); 1.183 + } 1.184 + 1.185 + @Override 1.186 + public int length() { 1.187 + return super.length() + 2; 1.188 + } 1.189 + 1.190 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.191 + return visitor.visit_chop_frame(this, data); 1.192 + } 1.193 + 1.194 + public final int offset_delta; 1.195 + } 1.196 + 1.197 + public static class same_frame_extended extends stack_map_frame { 1.198 + same_frame_extended(int frame_type, ClassReader cr) throws IOException { 1.199 + super(frame_type); 1.200 + offset_delta = cr.readUnsignedShort(); 1.201 + } 1.202 + 1.203 + @Override 1.204 + public int length() { 1.205 + return super.length() + 2; 1.206 + } 1.207 + 1.208 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.209 + return visitor.visit_same_frame_extended(this, data); 1.210 + } 1.211 + 1.212 + public final int offset_delta; 1.213 + } 1.214 + 1.215 + public static class append_frame extends stack_map_frame { 1.216 + append_frame(int frame_type, ClassReader cr) 1.217 + throws IOException, InvalidStackMap { 1.218 + super(frame_type); 1.219 + offset_delta = cr.readUnsignedShort(); 1.220 + locals = new verification_type_info[frame_type - 251]; 1.221 + for (int i = 0; i < locals.length; i++) 1.222 + locals[i] = verification_type_info.read(cr); 1.223 + } 1.224 + 1.225 + @Override 1.226 + public int length() { 1.227 + int n = super.length() + 2; 1.228 + for (verification_type_info local: locals) 1.229 + n += local.length(); 1.230 + return n; 1.231 + } 1.232 + 1.233 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.234 + return visitor.visit_append_frame(this, data); 1.235 + } 1.236 + 1.237 + public final int offset_delta; 1.238 + public final verification_type_info[] locals; 1.239 + } 1.240 + 1.241 + public static class full_frame extends stack_map_frame { 1.242 + full_frame(int frame_type, ClassReader cr) 1.243 + throws IOException, InvalidStackMap { 1.244 + super(frame_type); 1.245 + offset_delta = cr.readUnsignedShort(); 1.246 + number_of_locals = cr.readUnsignedShort(); 1.247 + locals = new verification_type_info[number_of_locals]; 1.248 + for (int i = 0; i < locals.length; i++) 1.249 + locals[i] = verification_type_info.read(cr); 1.250 + number_of_stack_items = cr.readUnsignedShort(); 1.251 + stack = new verification_type_info[number_of_stack_items]; 1.252 + for (int i = 0; i < stack.length; i++) 1.253 + stack[i] = verification_type_info.read(cr); 1.254 + } 1.255 + 1.256 + @Override 1.257 + public int length() { 1.258 + int n = super.length() + 2; 1.259 + for (verification_type_info local: locals) 1.260 + n += local.length(); 1.261 + n += 2; 1.262 + for (verification_type_info item: stack) 1.263 + n += item.length(); 1.264 + return n; 1.265 + } 1.266 + 1.267 + public <R, D> R accept(Visitor<R, D> visitor, D data) { 1.268 + return visitor.visit_full_frame(this, data); 1.269 + } 1.270 + 1.271 + public final int offset_delta; 1.272 + public final int number_of_locals; 1.273 + public final verification_type_info[] locals; 1.274 + public final int number_of_stack_items; 1.275 + public final verification_type_info[] stack; 1.276 + } 1.277 + 1.278 + public static class verification_type_info { 1.279 + public static final int ITEM_Top = 0; 1.280 + public static final int ITEM_Integer = 1; 1.281 + public static final int ITEM_Float = 2; 1.282 + public static final int ITEM_Long = 4; 1.283 + public static final int ITEM_Double = 3; 1.284 + public static final int ITEM_Null = 5; 1.285 + public static final int ITEM_UninitializedThis = 6; 1.286 + public static final int ITEM_Object = 7; 1.287 + public static final int ITEM_Uninitialized = 8; 1.288 + 1.289 + static verification_type_info read(ClassReader cr) 1.290 + throws IOException, InvalidStackMap { 1.291 + int tag = cr.readUnsignedByte(); 1.292 + switch (tag) { 1.293 + case ITEM_Top: 1.294 + case ITEM_Integer: 1.295 + case ITEM_Float: 1.296 + case ITEM_Long: 1.297 + case ITEM_Double: 1.298 + case ITEM_Null: 1.299 + case ITEM_UninitializedThis: 1.300 + return new verification_type_info(tag); 1.301 + 1.302 + case ITEM_Object: 1.303 + return new Object_variable_info(cr); 1.304 + 1.305 + case ITEM_Uninitialized: 1.306 + return new Uninitialized_variable_info(cr); 1.307 + 1.308 + default: 1.309 + throw new InvalidStackMap("unrecognized verification_type_info tag"); 1.310 + } 1.311 + } 1.312 + 1.313 + verification_type_info(int tag) { 1.314 + this.tag = tag; 1.315 + } 1.316 + 1.317 + public int length() { 1.318 + return 1; 1.319 + } 1.320 + 1.321 + public final int tag; 1.322 + } 1.323 + 1.324 + public static class Object_variable_info extends verification_type_info { 1.325 + Object_variable_info(ClassReader cr) throws IOException { 1.326 + super(ITEM_Object); 1.327 + cpool_index = cr.readUnsignedShort(); 1.328 + } 1.329 + 1.330 + @Override 1.331 + public int length() { 1.332 + return super.length() + 2; 1.333 + } 1.334 + 1.335 + public final int cpool_index; 1.336 + } 1.337 + 1.338 + public static class Uninitialized_variable_info extends verification_type_info { 1.339 + Uninitialized_variable_info(ClassReader cr) throws IOException { 1.340 + super(ITEM_Uninitialized); 1.341 + offset = cr.readUnsignedShort(); 1.342 + } 1.343 + 1.344 + @Override 1.345 + public int length() { 1.346 + return super.length() + 2; 1.347 + } 1.348 + 1.349 + public final int offset; 1.350 + 1.351 + } 1.352 +}