|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.classfile; |
|
27 |
|
28 import java.io.IOException; |
|
29 |
|
30 /** |
|
31 * See JVMS3, section 4.8.4. |
|
32 * |
|
33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
34 * you write code that depends on this, you do so at your own risk. |
|
35 * This code and its internal interfaces are subject to change or |
|
36 * deletion without notice.</b> |
|
37 */ |
|
38 public class StackMapTable_attribute extends Attribute { |
|
39 static class InvalidStackMap extends AttributeException { |
|
40 InvalidStackMap(String msg) { |
|
41 super(msg); |
|
42 } |
|
43 } |
|
44 |
|
45 StackMapTable_attribute(ClassReader cr, int name_index, int length) |
|
46 throws IOException, InvalidStackMap { |
|
47 super(name_index, length); |
|
48 number_of_entries = cr.readUnsignedShort(); |
|
49 entries = new stack_map_frame[number_of_entries]; |
|
50 for (int i = 0; i < number_of_entries; i++) |
|
51 entries[i] = stack_map_frame.read(cr); |
|
52 } |
|
53 |
|
54 public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries) |
|
55 throws ConstantPoolException { |
|
56 this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries); |
|
57 } |
|
58 |
|
59 public StackMapTable_attribute(int name_index, stack_map_frame[] entries) { |
|
60 super(name_index, length(entries)); |
|
61 this.number_of_entries = entries.length; |
|
62 this.entries = entries; |
|
63 } |
|
64 |
|
65 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
66 return visitor.visitStackMapTable(this, data); |
|
67 } |
|
68 |
|
69 static int length(stack_map_frame[] entries) { |
|
70 int n = 2; |
|
71 for (stack_map_frame entry: entries) |
|
72 n += entry.length(); |
|
73 return n; |
|
74 } |
|
75 |
|
76 public final int number_of_entries; |
|
77 public final stack_map_frame entries[]; |
|
78 |
|
79 public static abstract class stack_map_frame { |
|
80 static stack_map_frame read(ClassReader cr) |
|
81 throws IOException, InvalidStackMap { |
|
82 int frame_type = cr.readUnsignedByte(); |
|
83 if (frame_type <= 63) |
|
84 return new same_frame(frame_type); |
|
85 else if (frame_type <= 127) |
|
86 return new same_locals_1_stack_item_frame(frame_type, cr); |
|
87 else if (frame_type <= 246) |
|
88 throw new Error("unknown frame_type " + frame_type); |
|
89 else if (frame_type == 247) |
|
90 return new same_locals_1_stack_item_frame_extended(frame_type, cr); |
|
91 else if (frame_type <= 250) |
|
92 return new chop_frame(frame_type, cr); |
|
93 else if (frame_type == 251) |
|
94 return new same_frame_extended(frame_type, cr); |
|
95 else if (frame_type <= 254) |
|
96 return new append_frame(frame_type, cr); |
|
97 else |
|
98 return new full_frame(frame_type, cr); |
|
99 } |
|
100 |
|
101 protected stack_map_frame(int frame_type) { |
|
102 this.frame_type = frame_type; |
|
103 } |
|
104 |
|
105 public int length() { |
|
106 return 1; |
|
107 } |
|
108 |
|
109 public abstract <R,D> R accept(Visitor<R,D> visitor, D data); |
|
110 |
|
111 public final int frame_type; |
|
112 |
|
113 public static interface Visitor<R,P> { |
|
114 R visit_same_frame(same_frame frame, P p); |
|
115 R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p); |
|
116 R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p); |
|
117 R visit_chop_frame(chop_frame frame, P p); |
|
118 R visit_same_frame_extended(same_frame_extended frame, P p); |
|
119 R visit_append_frame(append_frame frame, P p); |
|
120 R visit_full_frame(full_frame frame, P p); |
|
121 } |
|
122 } |
|
123 |
|
124 public static class same_frame extends stack_map_frame { |
|
125 same_frame(int frame_type) { |
|
126 super(frame_type); |
|
127 } |
|
128 |
|
129 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
130 return visitor.visit_same_frame(this, data); |
|
131 } |
|
132 } |
|
133 |
|
134 public static class same_locals_1_stack_item_frame extends stack_map_frame { |
|
135 same_locals_1_stack_item_frame(int frame_type, ClassReader cr) |
|
136 throws IOException, InvalidStackMap { |
|
137 super(frame_type); |
|
138 stack = new verification_type_info[1]; |
|
139 stack[0] = verification_type_info.read(cr); |
|
140 } |
|
141 |
|
142 @Override |
|
143 public int length() { |
|
144 return super.length() + stack[0].length(); |
|
145 } |
|
146 |
|
147 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
148 return visitor.visit_same_locals_1_stack_item_frame(this, data); |
|
149 } |
|
150 |
|
151 public final verification_type_info[] stack; |
|
152 } |
|
153 |
|
154 public static class same_locals_1_stack_item_frame_extended extends stack_map_frame { |
|
155 same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr) |
|
156 throws IOException, InvalidStackMap { |
|
157 super(frame_type); |
|
158 offset_delta = cr.readUnsignedShort(); |
|
159 stack = new verification_type_info[1]; |
|
160 stack[0] = verification_type_info.read(cr); |
|
161 } |
|
162 |
|
163 @Override |
|
164 public int length() { |
|
165 return super.length() + 2 + stack[0].length(); |
|
166 } |
|
167 |
|
168 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
169 return visitor.visit_same_locals_1_stack_item_frame_extended(this, data); |
|
170 } |
|
171 |
|
172 public final int offset_delta; |
|
173 public final verification_type_info[] stack; |
|
174 } |
|
175 |
|
176 public static class chop_frame extends stack_map_frame { |
|
177 chop_frame(int frame_type, ClassReader cr) throws IOException { |
|
178 super(frame_type); |
|
179 offset_delta = cr.readUnsignedShort(); |
|
180 } |
|
181 |
|
182 @Override |
|
183 public int length() { |
|
184 return super.length() + 2; |
|
185 } |
|
186 |
|
187 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
188 return visitor.visit_chop_frame(this, data); |
|
189 } |
|
190 |
|
191 public final int offset_delta; |
|
192 } |
|
193 |
|
194 public static class same_frame_extended extends stack_map_frame { |
|
195 same_frame_extended(int frame_type, ClassReader cr) throws IOException { |
|
196 super(frame_type); |
|
197 offset_delta = cr.readUnsignedShort(); |
|
198 } |
|
199 |
|
200 @Override |
|
201 public int length() { |
|
202 return super.length() + 2; |
|
203 } |
|
204 |
|
205 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
206 return visitor.visit_same_frame_extended(this, data); |
|
207 } |
|
208 |
|
209 public final int offset_delta; |
|
210 } |
|
211 |
|
212 public static class append_frame extends stack_map_frame { |
|
213 append_frame(int frame_type, ClassReader cr) |
|
214 throws IOException, InvalidStackMap { |
|
215 super(frame_type); |
|
216 offset_delta = cr.readUnsignedShort(); |
|
217 locals = new verification_type_info[frame_type - 251]; |
|
218 for (int i = 0; i < locals.length; i++) |
|
219 locals[i] = verification_type_info.read(cr); |
|
220 } |
|
221 |
|
222 @Override |
|
223 public int length() { |
|
224 int n = super.length() + 2; |
|
225 for (verification_type_info local: locals) |
|
226 n += local.length(); |
|
227 return n; |
|
228 } |
|
229 |
|
230 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
231 return visitor.visit_append_frame(this, data); |
|
232 } |
|
233 |
|
234 public final int offset_delta; |
|
235 public final verification_type_info[] locals; |
|
236 } |
|
237 |
|
238 public static class full_frame extends stack_map_frame { |
|
239 full_frame(int frame_type, ClassReader cr) |
|
240 throws IOException, InvalidStackMap { |
|
241 super(frame_type); |
|
242 offset_delta = cr.readUnsignedShort(); |
|
243 number_of_locals = cr.readUnsignedShort(); |
|
244 locals = new verification_type_info[number_of_locals]; |
|
245 for (int i = 0; i < locals.length; i++) |
|
246 locals[i] = verification_type_info.read(cr); |
|
247 number_of_stack_items = cr.readUnsignedShort(); |
|
248 stack = new verification_type_info[number_of_stack_items]; |
|
249 for (int i = 0; i < stack.length; i++) |
|
250 stack[i] = verification_type_info.read(cr); |
|
251 } |
|
252 |
|
253 @Override |
|
254 public int length() { |
|
255 int n = super.length() + 2; |
|
256 for (verification_type_info local: locals) |
|
257 n += local.length(); |
|
258 n += 2; |
|
259 for (verification_type_info item: stack) |
|
260 n += item.length(); |
|
261 return n; |
|
262 } |
|
263 |
|
264 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
265 return visitor.visit_full_frame(this, data); |
|
266 } |
|
267 |
|
268 public final int offset_delta; |
|
269 public final int number_of_locals; |
|
270 public final verification_type_info[] locals; |
|
271 public final int number_of_stack_items; |
|
272 public final verification_type_info[] stack; |
|
273 } |
|
274 |
|
275 public static class verification_type_info { |
|
276 public static final int ITEM_Top = 0; |
|
277 public static final int ITEM_Integer = 1; |
|
278 public static final int ITEM_Float = 2; |
|
279 public static final int ITEM_Long = 4; |
|
280 public static final int ITEM_Double = 3; |
|
281 public static final int ITEM_Null = 5; |
|
282 public static final int ITEM_UninitializedThis = 6; |
|
283 public static final int ITEM_Object = 7; |
|
284 public static final int ITEM_Uninitialized = 8; |
|
285 |
|
286 static verification_type_info read(ClassReader cr) |
|
287 throws IOException, InvalidStackMap { |
|
288 int tag = cr.readUnsignedByte(); |
|
289 switch (tag) { |
|
290 case ITEM_Top: |
|
291 case ITEM_Integer: |
|
292 case ITEM_Float: |
|
293 case ITEM_Long: |
|
294 case ITEM_Double: |
|
295 case ITEM_Null: |
|
296 case ITEM_UninitializedThis: |
|
297 return new verification_type_info(tag); |
|
298 |
|
299 case ITEM_Object: |
|
300 return new Object_variable_info(cr); |
|
301 |
|
302 case ITEM_Uninitialized: |
|
303 return new Uninitialized_variable_info(cr); |
|
304 |
|
305 default: |
|
306 throw new InvalidStackMap("unrecognized verification_type_info tag"); |
|
307 } |
|
308 } |
|
309 |
|
310 verification_type_info(int tag) { |
|
311 this.tag = tag; |
|
312 } |
|
313 |
|
314 public int length() { |
|
315 return 1; |
|
316 } |
|
317 |
|
318 public final int tag; |
|
319 } |
|
320 |
|
321 public static class Object_variable_info extends verification_type_info { |
|
322 Object_variable_info(ClassReader cr) throws IOException { |
|
323 super(ITEM_Object); |
|
324 cpool_index = cr.readUnsignedShort(); |
|
325 } |
|
326 |
|
327 @Override |
|
328 public int length() { |
|
329 return super.length() + 2; |
|
330 } |
|
331 |
|
332 public final int cpool_index; |
|
333 } |
|
334 |
|
335 public static class Uninitialized_variable_info extends verification_type_info { |
|
336 Uninitialized_variable_info(ClassReader cr) throws IOException { |
|
337 super(ITEM_Uninitialized); |
|
338 offset = cr.readUnsignedShort(); |
|
339 } |
|
340 |
|
341 @Override |
|
342 public int length() { |
|
343 return super.length() + 2; |
|
344 } |
|
345 |
|
346 public final int offset; |
|
347 |
|
348 } |
|
349 } |