2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 package org.apidesign.javap;
31 import static org.apidesign.javap.RuntimeConstants.*;
33 /* represents one entry of StackMapTable attribute
35 abstract class StackMapTableData {
39 StackMapTableData(int frameType) {
40 this.frameType = frameType;
43 abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
45 protected static String toString(
46 final String frameType,
48 final int[] localTypes,
49 final int[] stackTypes) {
50 final StringBuilder sb = new StringBuilder(frameType);
52 sb.append("(off: +").append(offset);
53 if (localTypes != null) {
54 sb.append(", locals: ");
55 appendTypes(sb, localTypes);
57 if (stackTypes != null) {
58 sb.append(", stack: ");
59 appendTypes(sb, stackTypes);
66 private static void appendTypes(final StringBuilder sb, final int[] types) {
68 if (types.length > 0) {
69 appendType(sb, types[0]);
70 for (int i = 1; i < types.length; ++i) {
72 appendType(sb, types[i]);
78 private static void appendType(final StringBuilder sb, final int type) {
79 switch (type & 0xff) {
90 sb.append("_double_");
98 case ITEM_InitObject: // UninitializedThis
102 sb.append("_object_");
104 case ITEM_NewObject: // Uninitialized
108 sb.append("_unknown_");
113 static class SameFrame extends StackMapTableData {
114 SameFrame(int frameType, int offsetDelta) {
116 this.offsetDelta = offsetDelta;
120 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
125 public String toString() {
126 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
127 ? "_FRAME_EXTENDED" : ""),
133 static class SameLocals1StackItem extends StackMapTableData {
135 SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
137 this.offsetDelta = offsetDelta;
142 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
143 stackTypes.setAll(stack);
147 public String toString() {
149 "SAME_LOCALS_1_STACK_ITEM"
150 + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
157 static class ChopFrame extends StackMapTableData {
158 ChopFrame(int frameType, int offsetDelta) {
160 this.offsetDelta = offsetDelta;
164 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
165 localTypes.setSize(localTypes.getSize()
166 - (SAME_FRAME_EXTENDED - frameType));
171 public String toString() {
172 return toString("CHOP", offsetDelta, null, null);
176 static class AppendFrame extends StackMapTableData {
178 AppendFrame(int frameType, int offsetDelta, int[] locals) {
180 this.offsetDelta = offsetDelta;
181 this.locals = locals;
185 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
186 localTypes.addAll(locals);
191 public String toString() {
192 return toString("APPEND", offsetDelta, locals, null);
196 static class FullFrame extends StackMapTableData {
199 FullFrame(int offsetDelta, int[] locals, int[] stack) {
201 this.offsetDelta = offsetDelta;
202 this.locals = locals;
207 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
208 localTypes.setAll(locals);
209 stackTypes.setAll(stack);
213 public String toString() {
214 return toString("FULL", offsetDelta, locals, stack);
218 static StackMapTableData getInstance(DataInputStream in, MethodData method)
220 int frameType = in.readUnsignedByte();
222 if (frameType < SAME_FRAME_BOUND) {
224 return new SameFrame(frameType, frameType);
225 } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
226 // same_locals_1_stack_item_frame
227 // read additional single stack element
228 return new SameLocals1StackItem(frameType,
229 (frameType - SAME_FRAME_BOUND),
230 StackMapData.readTypeArray(in, 1, method));
231 } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
232 // same_locals_1_stack_item_extended
233 return new SameLocals1StackItem(frameType,
234 in.readUnsignedShort(),
235 StackMapData.readTypeArray(in, 1, method));
236 } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
237 // chop_frame or same_frame_extended
238 return new ChopFrame(frameType, in.readUnsignedShort());
239 } else if (frameType == SAME_FRAME_EXTENDED) {
240 // chop_frame or same_frame_extended
241 return new SameFrame(frameType, in.readUnsignedShort());
242 } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
244 return new AppendFrame(frameType, in.readUnsignedShort(),
245 StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
246 } else if (frameType == FULL_FRAME) {
248 int offsetDelta = in.readUnsignedShort();
249 int locals_size = in.readUnsignedShort();
250 int[] locals = StackMapData.readTypeArray(in, locals_size, method);
251 int stack_size = in.readUnsignedShort();
252 int[] stack = StackMapData.readTypeArray(in, stack_size, method);
253 return new FullFrame(offsetDelta, locals, stack);
255 throw new ClassFormatError("unrecognized frame_type in StackMapTable");