1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/StackMapTableData.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,223 @@
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 org.apidesign.javap;
1.31 +
1.32 +import java.io.*;
1.33 +
1.34 +import static org.apidesign.javap.RuntimeConstants.*;
1.35 +
1.36 +/* represents one entry of StackMapTable attribute
1.37 + */
1.38 +abstract class StackMapTableData {
1.39 + final int frameType;
1.40 + int offsetDelta;
1.41 +
1.42 + StackMapTableData(int frameType) {
1.43 + this.frameType = frameType;
1.44 + }
1.45 +
1.46 + abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
1.47 +
1.48 + protected static String toString(
1.49 + final String frameType,
1.50 + final int offset,
1.51 + final int[] localTypes,
1.52 + final int[] stackTypes) {
1.53 + final StringBuilder sb = new StringBuilder(frameType);
1.54 +
1.55 + sb.append("(off: +").append(offset);
1.56 + if (localTypes != null) {
1.57 + sb.append(", locals: ");
1.58 + appendTypes(sb, localTypes);
1.59 + }
1.60 + if (stackTypes != null) {
1.61 + sb.append(", stack: ");
1.62 + appendTypes(sb, stackTypes);
1.63 + }
1.64 + sb.append(')');
1.65 +
1.66 + return sb.toString();
1.67 + }
1.68 +
1.69 + private static void appendTypes(final StringBuilder sb, final int[] types) {
1.70 + sb.append('[');
1.71 + if (types.length > 0) {
1.72 + sb.append(TypeArray.typeString(types[0]));
1.73 + for (int i = 1; i < types.length; ++i) {
1.74 + sb.append(", ");
1.75 + sb.append(TypeArray.typeString(types[i]));
1.76 + }
1.77 + }
1.78 + sb.append(']');
1.79 + }
1.80 +
1.81 + static class SameFrame extends StackMapTableData {
1.82 + SameFrame(int frameType, int offsetDelta) {
1.83 + super(frameType);
1.84 + this.offsetDelta = offsetDelta;
1.85 + }
1.86 +
1.87 + @Override
1.88 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
1.89 + stackTypes.clear();
1.90 + }
1.91 +
1.92 + @Override
1.93 + public String toString() {
1.94 + return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
1.95 + ? "_FRAME_EXTENDED" : ""),
1.96 + offsetDelta,
1.97 + null, null);
1.98 + }
1.99 + }
1.100 +
1.101 + static class SameLocals1StackItem extends StackMapTableData {
1.102 + final int[] stack;
1.103 + SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
1.104 + super(frameType);
1.105 + this.offsetDelta = offsetDelta;
1.106 + this.stack = stack;
1.107 + }
1.108 +
1.109 + @Override
1.110 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
1.111 + stackTypes.setAll(stack);
1.112 + }
1.113 +
1.114 + @Override
1.115 + public String toString() {
1.116 + return toString(
1.117 + "SAME_LOCALS_1_STACK_ITEM"
1.118 + + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
1.119 + ? "_EXTENDED" : ""),
1.120 + offsetDelta,
1.121 + null, stack);
1.122 + }
1.123 + }
1.124 +
1.125 + static class ChopFrame extends StackMapTableData {
1.126 + ChopFrame(int frameType, int offsetDelta) {
1.127 + super(frameType);
1.128 + this.offsetDelta = offsetDelta;
1.129 + }
1.130 +
1.131 + @Override
1.132 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
1.133 + localTypes.setSize(localTypes.getSize()
1.134 + - (SAME_FRAME_EXTENDED - frameType));
1.135 + stackTypes.clear();
1.136 + }
1.137 +
1.138 + @Override
1.139 + public String toString() {
1.140 + return toString("CHOP", offsetDelta, null, null);
1.141 + }
1.142 + }
1.143 +
1.144 + static class AppendFrame extends StackMapTableData {
1.145 + final int[] locals;
1.146 + AppendFrame(int frameType, int offsetDelta, int[] locals) {
1.147 + super(frameType);
1.148 + this.offsetDelta = offsetDelta;
1.149 + this.locals = locals;
1.150 + }
1.151 +
1.152 + @Override
1.153 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
1.154 + localTypes.addAll(locals);
1.155 + stackTypes.clear();
1.156 + }
1.157 +
1.158 + @Override
1.159 + public String toString() {
1.160 + return toString("APPEND", offsetDelta, locals, null);
1.161 + }
1.162 + }
1.163 +
1.164 + static class FullFrame extends StackMapTableData {
1.165 + final int[] locals;
1.166 + final int[] stack;
1.167 + FullFrame(int offsetDelta, int[] locals, int[] stack) {
1.168 + super(FULL_FRAME);
1.169 + this.offsetDelta = offsetDelta;
1.170 + this.locals = locals;
1.171 + this.stack = stack;
1.172 + }
1.173 +
1.174 + @Override
1.175 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
1.176 + localTypes.setAll(locals);
1.177 + stackTypes.setAll(stack);
1.178 + }
1.179 +
1.180 + @Override
1.181 + public String toString() {
1.182 + return toString("FULL", offsetDelta, locals, stack);
1.183 + }
1.184 + }
1.185 +
1.186 + static StackMapTableData getInstance(DataInputStream in, MethodData method)
1.187 + throws IOException {
1.188 + int frameType = in.readUnsignedByte();
1.189 +
1.190 + if (frameType < SAME_FRAME_BOUND) {
1.191 + // same_frame
1.192 + return new SameFrame(frameType, frameType);
1.193 + } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
1.194 + // same_locals_1_stack_item_frame
1.195 + // read additional single stack element
1.196 + return new SameLocals1StackItem(frameType,
1.197 + (frameType - SAME_FRAME_BOUND),
1.198 + StackMapData.readTypeArray(in, 1, method));
1.199 + } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1.200 + // same_locals_1_stack_item_extended
1.201 + return new SameLocals1StackItem(frameType,
1.202 + in.readUnsignedShort(),
1.203 + StackMapData.readTypeArray(in, 1, method));
1.204 + } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
1.205 + // chop_frame or same_frame_extended
1.206 + return new ChopFrame(frameType, in.readUnsignedShort());
1.207 + } else if (frameType == SAME_FRAME_EXTENDED) {
1.208 + // chop_frame or same_frame_extended
1.209 + return new SameFrame(frameType, in.readUnsignedShort());
1.210 + } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
1.211 + // append_frame
1.212 + return new AppendFrame(frameType, in.readUnsignedShort(),
1.213 + StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
1.214 + } else if (frameType == FULL_FRAME) {
1.215 + // full_frame
1.216 + int offsetDelta = in.readUnsignedShort();
1.217 + int locals_size = in.readUnsignedShort();
1.218 + int[] locals = StackMapData.readTypeArray(in, locals_size, method);
1.219 + int stack_size = in.readUnsignedShort();
1.220 + int[] stack = StackMapData.readTypeArray(in, stack_size, method);
1.221 + return new FullFrame(offsetDelta, locals, stack);
1.222 + } else {
1.223 + throw new ClassFormatError("unrecognized frame_type in StackMapTable");
1.224 + }
1.225 + }
1.226 +}