2 * Copyright (c) 2012, 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
26 package org.apidesign.javap;
28 import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
29 import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
30 import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
31 import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
32 import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
34 public final class StackMapIterator {
35 private final StackMapTableData[] stackMapTable;
36 private final TypeArray argTypes;
37 private final TypeArray localTypes;
38 private final TypeArray stackTypes;
40 private int nextFrameIndex;
41 private int lastFrameByteCodeOffset;
43 private int byteCodeOffset;
45 StackMapIterator(final MethodData methodData) {
46 this(methodData.getStackMapTable(),
47 methodData.getInternalSig(),
48 methodData.isStatic());
51 StackMapIterator(final StackMapTableData[] stackMapTable,
52 final String methodSignature,
53 final boolean isStaticMethod) {
54 this.stackMapTable = (stackMapTable != null)
56 : new StackMapTableData[0];
58 argTypes = getArgTypes(methodSignature, isStaticMethod);
59 localTypes = new TypeArray();
60 stackTypes = new TypeArray();
62 localTypes.addAll(argTypes);
64 lastFrameByteCodeOffset = -1;
68 public String getFrameAsString() {
69 return (nextFrameIndex == 0)
70 ? StackMapTableData.toString("INITIAL", 0, null, null)
71 : stackMapTable[nextFrameIndex - 1].toString();
74 public int getFrameIndex() {
75 return nextFrameIndex;
78 public TypeArray getFrameStack() {
82 public TypeArray getFrameLocals() {
86 public TypeArray getArguments() {
90 public void advanceBy(final int numByteCodes) {
91 if (numByteCodes < 0) {
92 throw new IllegalStateException("Forward only iterator");
95 byteCodeOffset += numByteCodes;
96 while ((nextFrameIndex < stackMapTable.length)
97 && ((byteCodeOffset - lastFrameByteCodeOffset)
98 >= (stackMapTable[nextFrameIndex].offsetDelta
100 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
102 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
103 nextFrame.applyTo(localTypes, stackTypes);
109 public void advanceTo(final int nextByteCodeOffset) {
110 advanceBy(nextByteCodeOffset - byteCodeOffset);
113 private static TypeArray getArgTypes(final String methodSignature,
114 final boolean isStaticMethod) {
115 final TypeArray argTypes = new TypeArray();
117 if (!isStaticMethod) {
118 argTypes.add(ITEM_Object);
121 if (methodSignature.charAt(0) != '(') {
122 throw new IllegalArgumentException("Invalid method signature");
125 final int length = methodSignature.length();
128 for (int i = 1; i < length; ++i) {
129 switch (methodSignature.charAt(i)) {
135 argType = ITEM_Integer;
141 argType = ITEM_Float;
144 argType = ITEM_Double;
147 i = methodSignature.indexOf(';', i + 1);
149 throw new IllegalArgumentException(
150 "Invalid method signature");
152 argType = ITEM_Object;
156 // not interested in the return value type
160 argTypes.add(ITEM_Object);
166 throw new IllegalArgumentException(
167 "Invalid method signature");
171 argTypes.add(argType);