lubomir@221: /* lubomir@221: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. lubomir@221: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. lubomir@221: * lubomir@221: * This code is free software; you can redistribute it and/or modify it lubomir@221: * under the terms of the GNU General Public License version 2 only, as lubomir@221: * published by the Free Software Foundation. Oracle designates this lubomir@221: * particular file as subject to the "Classpath" exception as provided lubomir@221: * by Oracle in the LICENSE file that accompanied this code. lubomir@221: * lubomir@221: * This code is distributed in the hope that it will be useful, but WITHOUT lubomir@221: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or lubomir@221: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License lubomir@221: * version 2 for more details (a copy is included in the LICENSE file that lubomir@221: * accompanied this code). lubomir@221: * lubomir@221: * You should have received a copy of the GNU General Public License version lubomir@221: * 2 along with this work; if not, write to the Free Software Foundation, lubomir@221: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. lubomir@221: * lubomir@221: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA lubomir@221: * or visit www.oracle.com if you need additional information or have any lubomir@221: * questions. lubomir@221: */ lubomir@221: lubomir@221: package org.apidesign.javap; lubomir@221: lubomir@221: public final class StackMapIterator { lubomir@221: private static final StackMapTableData INITIAL_FRAME = lubomir@221: new StackMapTableData(-1) { lubomir@221: @Override lubomir@221: int getStackItemsCount() { lubomir@221: return 0; lubomir@221: } lubomir@221: lubomir@221: @Override lubomir@221: public String toString() { lubomir@221: return "INITIAL(0)"; lubomir@221: } lubomir@221: }; lubomir@221: lubomir@221: private final StackMapTableData[] stackMapTable; lubomir@221: lubomir@221: private int nextFrameIndex; lubomir@221: private int lastFrameByteCodeOffset; lubomir@221: lubomir@221: private int byteCodeOffset; lubomir@221: lubomir@221: StackMapIterator(final StackMapTableData[] stackMapTable) { lubomir@221: this.stackMapTable = (stackMapTable != null) lubomir@221: ? stackMapTable lubomir@221: : new StackMapTableData[0]; lubomir@221: this.lastFrameByteCodeOffset = -1; lubomir@221: advanceBy(0); lubomir@221: } lubomir@221: lubomir@221: public String getFrameAsString() { lubomir@221: return getCurrentFrame().toString(); lubomir@221: } lubomir@221: lubomir@221: public int getFrameIndex() { lubomir@221: return nextFrameIndex; lubomir@221: } lubomir@221: lubomir@221: public int getFrameStackItemsCount() { lubomir@221: return getCurrentFrame().getStackItemsCount(); lubomir@221: } lubomir@221: lubomir@221: public void advanceBy(final int numByteCodes) { lubomir@221: if (numByteCodes < 0) { lubomir@221: throw new IllegalStateException("Forward only iterator"); lubomir@221: } lubomir@221: lubomir@221: byteCodeOffset += numByteCodes; lubomir@221: while ((nextFrameIndex < stackMapTable.length) lubomir@221: && ((byteCodeOffset - lastFrameByteCodeOffset) lubomir@221: >= (stackMapTable[nextFrameIndex].offsetDelta lubomir@221: + 1))) { lubomir@221: lastFrameByteCodeOffset += lubomir@221: stackMapTable[nextFrameIndex].offsetDelta + 1; lubomir@221: ++nextFrameIndex; lubomir@221: } lubomir@221: } lubomir@221: lubomir@221: public void advanceTo(final int nextByteCodeOffset) { lubomir@221: advanceBy(nextByteCodeOffset - byteCodeOffset); lubomir@221: } lubomir@221: lubomir@221: private StackMapTableData getCurrentFrame() { lubomir@221: return (nextFrameIndex == 0) lubomir@221: ? INITIAL_FRAME lubomir@221: : stackMapTable[nextFrameIndex - 1]; lubomir@221: } lubomir@221: }