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 public final class StackMapIterator {
29 private static final StackMapTableData INITIAL_FRAME =
30 new StackMapTableData(-1) {
32 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
38 public String toString() {
39 return toString("INITIAL", 0, null, null);
44 private final StackMapTableData[] stackMapTable;
45 private final TypeArray localTypes;
46 private final TypeArray stackTypes;
48 private int nextFrameIndex;
49 private int lastFrameByteCodeOffset;
51 private int byteCodeOffset;
53 StackMapIterator(final StackMapTableData[] stackMapTable) {
54 this.stackMapTable = (stackMapTable != null)
56 : new StackMapTableData[0];
58 localTypes = new TypeArray();
59 stackTypes = new TypeArray();
60 lastFrameByteCodeOffset = -1;
64 public String getFrameAsString() {
65 return getCurrentFrame().toString();
68 public int getFrameIndex() {
69 return nextFrameIndex;
72 public TypeArray getFrameStack() {
76 public void advanceBy(final int numByteCodes) {
77 if (numByteCodes < 0) {
78 throw new IllegalStateException("Forward only iterator");
81 byteCodeOffset += numByteCodes;
82 while ((nextFrameIndex < stackMapTable.length)
83 && ((byteCodeOffset - lastFrameByteCodeOffset)
84 >= (stackMapTable[nextFrameIndex].offsetDelta
86 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
88 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
89 nextFrame.applyTo(localTypes, stackTypes);
95 public void advanceTo(final int nextByteCodeOffset) {
96 advanceBy(nextByteCodeOffset - byteCodeOffset);
99 private StackMapTableData getCurrentFrame() {
100 return (nextFrameIndex == 0)
102 : stackMapTable[nextFrameIndex - 1];