001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.javap;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: import static sun.tools.javap.RuntimeConstants.*;
032:
033: /* represents one entry of StackMapTable attribute
034: */
035: class StackMapTableData {
036: final int frameType;
037: int offsetDelta;
038:
039: StackMapTableData(int frameType) {
040: this .frameType = frameType;
041: }
042:
043: void print(JavapPrinter p) {
044: p.out.print(" frame_type = " + frameType);
045: }
046:
047: static class SameFrame extends StackMapTableData {
048: SameFrame(int frameType, int offsetDelta) {
049: super (frameType);
050: this .offsetDelta = offsetDelta;
051: }
052:
053: void print(JavapPrinter p) {
054: super .print(p);
055: if (frameType < SAME_FRAME_BOUND) {
056: p.out.println(" /* same */");
057: } else {
058: p.out.println(" /* same_frame_extended */");
059: p.out.println(" offset_delta = " + offsetDelta);
060: }
061: }
062: }
063:
064: static class SameLocals1StackItem extends StackMapTableData {
065: final int[] stack;
066:
067: SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
068: super (frameType);
069: this .offsetDelta = offsetDelta;
070: this .stack = stack;
071: }
072:
073: void print(JavapPrinter p) {
074: super .print(p);
075: if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
076: p.out
077: .println(" /* same_locals_1_stack_item_frame_extended */");
078: p.out.println(" offset_delta = " + offsetDelta);
079: } else {
080: p.out.println(" /* same_locals_1_stack_item */");
081: }
082: p.printMap(" stack = [", stack);
083: }
084: }
085:
086: static class ChopFrame extends StackMapTableData {
087: ChopFrame(int frameType, int offsetDelta) {
088: super (frameType);
089: this .offsetDelta = offsetDelta;
090: }
091:
092: void print(JavapPrinter p) {
093: super .print(p);
094: p.out.println(" /* chop */");
095: p.out.println(" offset_delta = " + offsetDelta);
096: }
097: }
098:
099: static class AppendFrame extends StackMapTableData {
100: final int[] locals;
101:
102: AppendFrame(int frameType, int offsetDelta, int[] locals) {
103: super (frameType);
104: this .offsetDelta = offsetDelta;
105: this .locals = locals;
106: }
107:
108: void print(JavapPrinter p) {
109: super .print(p);
110: p.out.println(" /* append */");
111: p.out.println(" offset_delta = " + offsetDelta);
112: p.printMap(" locals = [", locals);
113: }
114: }
115:
116: static class FullFrame extends StackMapTableData {
117: final int[] locals;
118: final int[] stack;
119:
120: FullFrame(int offsetDelta, int[] locals, int[] stack) {
121: super (FULL_FRAME);
122: this .offsetDelta = offsetDelta;
123: this .locals = locals;
124: this .stack = stack;
125: }
126:
127: void print(JavapPrinter p) {
128: super .print(p);
129: p.out.println(" /* full_frame */");
130: p.out.println(" offset_delta = " + offsetDelta);
131: p.printMap(" locals = [", locals);
132: p.printMap(" stack = [", stack);
133: }
134: }
135:
136: static StackMapTableData getInstance(DataInputStream in,
137: MethodData method) throws IOException {
138: int frameType = in.readUnsignedByte();
139:
140: if (frameType < SAME_FRAME_BOUND) {
141: // same_frame
142: return new SameFrame(frameType, frameType);
143: } else if (SAME_FRAME_BOUND <= frameType
144: && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
145: // same_locals_1_stack_item_frame
146: // read additional single stack element
147: return new SameLocals1StackItem(frameType,
148: (frameType - SAME_FRAME_BOUND), StackMapData
149: .readTypeArray(in, 1, method));
150: } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
151: // same_locals_1_stack_item_extended
152: return new SameLocals1StackItem(frameType, in
153: .readUnsignedShort(), StackMapData.readTypeArray(
154: in, 1, method));
155: } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType
156: && frameType < SAME_FRAME_EXTENDED) {
157: // chop_frame or same_frame_extended
158: return new ChopFrame(frameType, in.readUnsignedShort());
159: } else if (frameType == SAME_FRAME_EXTENDED) {
160: // chop_frame or same_frame_extended
161: return new SameFrame(frameType, in.readUnsignedShort());
162: } else if (SAME_FRAME_EXTENDED < frameType
163: && frameType < FULL_FRAME) {
164: // append_frame
165: return new AppendFrame(frameType, in.readUnsignedShort(),
166: StackMapData.readTypeArray(in, frameType
167: - SAME_FRAME_EXTENDED, method));
168: } else if (frameType == FULL_FRAME) {
169: // full_frame
170: int offsetDelta = in.readUnsignedShort();
171: int locals_size = in.readUnsignedShort();
172: int[] locals = StackMapData.readTypeArray(in, locals_size,
173: method);
174: int stack_size = in.readUnsignedShort();
175: int[] stack = StackMapData.readTypeArray(in, stack_size,
176: method);
177: return new FullFrame(offsetDelta, locals, stack);
178: } else {
179: throw new ClassFormatError(
180: "unrecognized frame_type in StackMapTable");
181: }
182: }
183: }
|