1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javap/src/main/java/sun/tools/javap/StackMapTableData.java Fri Nov 09 21:33:22 2012 +0100
1.3 @@ -0,0 +1,168 @@
1.4 +/*
1.5 + * Copyright (c) 2005, Oracle and/or its affiliates. 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. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +
1.30 +package sun.tools.javap;
1.31 +
1.32 +import java.util.*;
1.33 +import java.io.*;
1.34 +
1.35 +import static sun.tools.javap.RuntimeConstants.*;
1.36 +
1.37 +/* represents one entry of StackMapTable attribute
1.38 + */
1.39 +class StackMapTableData {
1.40 + final int frameType;
1.41 + int offsetDelta;
1.42 +
1.43 + StackMapTableData(int frameType) {
1.44 + this.frameType = frameType;
1.45 + }
1.46 +
1.47 + void print(JavapPrinter p) {
1.48 + p.out.print(" frame_type = " + frameType);
1.49 + }
1.50 +
1.51 + static class SameFrame extends StackMapTableData {
1.52 + SameFrame(int frameType, int offsetDelta) {
1.53 + super(frameType);
1.54 + this.offsetDelta = offsetDelta;
1.55 + }
1.56 + void print(JavapPrinter p) {
1.57 + super.print(p);
1.58 + if (frameType < SAME_FRAME_BOUND) {
1.59 + p.out.println(" /* same */");
1.60 + } else {
1.61 + p.out.println(" /* same_frame_extended */");
1.62 + p.out.println(" offset_delta = " + offsetDelta);
1.63 + }
1.64 + }
1.65 + }
1.66 +
1.67 + static class SameLocals1StackItem extends StackMapTableData {
1.68 + final int[] stack;
1.69 + SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
1.70 + super(frameType);
1.71 + this.offsetDelta = offsetDelta;
1.72 + this.stack = stack;
1.73 + }
1.74 + void print(JavapPrinter p) {
1.75 + super.print(p);
1.76 + if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1.77 + p.out.println(" /* same_locals_1_stack_item_frame_extended */");
1.78 + p.out.println(" offset_delta = " + offsetDelta);
1.79 + } else {
1.80 + p.out.println(" /* same_locals_1_stack_item */");
1.81 + }
1.82 + p.printMap(" stack = [", stack);
1.83 + }
1.84 + }
1.85 +
1.86 + static class ChopFrame extends StackMapTableData {
1.87 + ChopFrame(int frameType, int offsetDelta) {
1.88 + super(frameType);
1.89 + this.offsetDelta = offsetDelta;
1.90 + }
1.91 + void print(JavapPrinter p) {
1.92 + super.print(p);
1.93 + p.out.println(" /* chop */");
1.94 + p.out.println(" offset_delta = " + offsetDelta);
1.95 + }
1.96 + }
1.97 +
1.98 + static class AppendFrame extends StackMapTableData {
1.99 + final int[] locals;
1.100 + AppendFrame(int frameType, int offsetDelta, int[] locals) {
1.101 + super(frameType);
1.102 + this.offsetDelta = offsetDelta;
1.103 + this.locals = locals;
1.104 + }
1.105 + void print(JavapPrinter p) {
1.106 + super.print(p);
1.107 + p.out.println(" /* append */");
1.108 + p.out.println(" offset_delta = " + offsetDelta);
1.109 + p.printMap(" locals = [", locals);
1.110 + }
1.111 + }
1.112 +
1.113 + static class FullFrame extends StackMapTableData {
1.114 + final int[] locals;
1.115 + final int[] stack;
1.116 + FullFrame(int offsetDelta, int[] locals, int[] stack) {
1.117 + super(FULL_FRAME);
1.118 + this.offsetDelta = offsetDelta;
1.119 + this.locals = locals;
1.120 + this.stack = stack;
1.121 + }
1.122 + void print(JavapPrinter p) {
1.123 + super.print(p);
1.124 + p.out.println(" /* full_frame */");
1.125 + p.out.println(" offset_delta = " + offsetDelta);
1.126 + p.printMap(" locals = [", locals);
1.127 + p.printMap(" stack = [", stack);
1.128 + }
1.129 + }
1.130 +
1.131 + static StackMapTableData getInstance(DataInputStream in, MethodData method)
1.132 + throws IOException {
1.133 + int frameType = in.readUnsignedByte();
1.134 +
1.135 + if (frameType < SAME_FRAME_BOUND) {
1.136 + // same_frame
1.137 + return new SameFrame(frameType, frameType);
1.138 + } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
1.139 + // same_locals_1_stack_item_frame
1.140 + // read additional single stack element
1.141 + return new SameLocals1StackItem(frameType,
1.142 + (frameType - SAME_FRAME_BOUND),
1.143 + StackMapData.readTypeArray(in, 1, method));
1.144 + } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1.145 + // same_locals_1_stack_item_extended
1.146 + return new SameLocals1StackItem(frameType,
1.147 + in.readUnsignedShort(),
1.148 + StackMapData.readTypeArray(in, 1, method));
1.149 + } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
1.150 + // chop_frame or same_frame_extended
1.151 + return new ChopFrame(frameType, in.readUnsignedShort());
1.152 + } else if (frameType == SAME_FRAME_EXTENDED) {
1.153 + // chop_frame or same_frame_extended
1.154 + return new SameFrame(frameType, in.readUnsignedShort());
1.155 + } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
1.156 + // append_frame
1.157 + return new AppendFrame(frameType, in.readUnsignedShort(),
1.158 + StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
1.159 + } else if (frameType == FULL_FRAME) {
1.160 + // full_frame
1.161 + int offsetDelta = in.readUnsignedShort();
1.162 + int locals_size = in.readUnsignedShort();
1.163 + int[] locals = StackMapData.readTypeArray(in, locals_size, method);
1.164 + int stack_size = in.readUnsignedShort();
1.165 + int[] stack = StackMapData.readTypeArray(in, stack_size, method);
1.166 + return new FullFrame(offsetDelta, locals, stack);
1.167 + } else {
1.168 + throw new ClassFormatError("unrecognized frame_type in StackMapTable");
1.169 + }
1.170 + }
1.171 +}