rt/javap/src/main/java/org/apidesign/javap/StackMapIterator.java
changeset 772 d382dacfd73f
parent 472 1a73c6e08cc5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/StackMapIterator.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 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 +package org.apidesign.javap;
    1.30 +
    1.31 +import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
    1.32 +import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
    1.33 +import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
    1.34 +import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
    1.35 +import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
    1.36 +
    1.37 +public final class StackMapIterator {
    1.38 +    private final StackMapTableData[] stackMapTable;
    1.39 +    private final TypeArray argTypes;
    1.40 +    private final TypeArray localTypes;
    1.41 +    private final TypeArray stackTypes;
    1.42 +
    1.43 +    private int nextFrameIndex;
    1.44 +    private int lastFrameByteCodeOffset;
    1.45 +
    1.46 +    private int byteCodeOffset;
    1.47 +
    1.48 +    StackMapIterator(final MethodData methodData) {
    1.49 +        this(methodData.getStackMapTable(),
    1.50 +             methodData.getInternalSig(),
    1.51 +             methodData.isStatic());
    1.52 +    }
    1.53 +
    1.54 +    StackMapIterator(final StackMapTableData[] stackMapTable,
    1.55 +                     final String methodSignature,
    1.56 +                     final boolean isStaticMethod) {
    1.57 +        this.stackMapTable = (stackMapTable != null)
    1.58 +                                 ? stackMapTable
    1.59 +                                 : new StackMapTableData[0];
    1.60 +
    1.61 +        argTypes = getArgTypes(methodSignature, isStaticMethod);
    1.62 +        localTypes = new TypeArray();
    1.63 +        stackTypes = new TypeArray();
    1.64 +
    1.65 +        localTypes.addAll(argTypes);
    1.66 +
    1.67 +        lastFrameByteCodeOffset = -1;
    1.68 +        advanceBy(0);
    1.69 +    }
    1.70 +
    1.71 +    public String getFrameAsString() {
    1.72 +        return (nextFrameIndex == 0)
    1.73 +                   ? StackMapTableData.toString("INITIAL", 0, null, null)
    1.74 +                   : stackMapTable[nextFrameIndex - 1].toString();
    1.75 +    }
    1.76 +
    1.77 +    public int getFrameIndex() {
    1.78 +        return nextFrameIndex;
    1.79 +    }
    1.80 +
    1.81 +    public TypeArray getFrameStack() {
    1.82 +        return stackTypes;
    1.83 +    }
    1.84 +
    1.85 +    public TypeArray getFrameLocals() {
    1.86 +        return localTypes;
    1.87 +    }
    1.88 +
    1.89 +    public TypeArray getArguments() {
    1.90 +        return argTypes;
    1.91 +    }
    1.92 +
    1.93 +    public void advanceBy(final int numByteCodes) {
    1.94 +        if (numByteCodes < 0) {
    1.95 +            throw new IllegalStateException("Forward only iterator");
    1.96 +        }
    1.97 +
    1.98 +        byteCodeOffset += numByteCodes;
    1.99 +        while ((nextFrameIndex < stackMapTable.length)
   1.100 +                    && ((byteCodeOffset - lastFrameByteCodeOffset)
   1.101 +                            >= (stackMapTable[nextFrameIndex].offsetDelta
   1.102 +                                    + 1))) {
   1.103 +            final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
   1.104 +
   1.105 +            lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
   1.106 +            nextFrame.applyTo(localTypes, stackTypes);
   1.107 +
   1.108 +            ++nextFrameIndex;
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    public void advanceTo(final int nextByteCodeOffset) {
   1.113 +        advanceBy(nextByteCodeOffset - byteCodeOffset);
   1.114 +    }
   1.115 +
   1.116 +    private static TypeArray getArgTypes(final String methodSignature,
   1.117 +                                         final boolean isStaticMethod) {
   1.118 +        final TypeArray argTypes = new TypeArray();
   1.119 +
   1.120 +        if (!isStaticMethod) {
   1.121 +            argTypes.add(ITEM_Object);
   1.122 +        }
   1.123 +
   1.124 +        if (methodSignature.charAt(0) != '(') {
   1.125 +            throw new IllegalArgumentException("Invalid method signature");
   1.126 +        }
   1.127 +
   1.128 +        final int length = methodSignature.length();
   1.129 +        boolean skipType = false;
   1.130 +        int argType;
   1.131 +        for (int i = 1; i < length; ++i) {
   1.132 +            switch (methodSignature.charAt(i)) {
   1.133 +                case 'B':
   1.134 +                case 'C':
   1.135 +                case 'S':
   1.136 +                case 'Z':
   1.137 +                case 'I':
   1.138 +                    argType = ITEM_Integer;
   1.139 +                    break;
   1.140 +                case 'J':
   1.141 +                    argType = ITEM_Long;
   1.142 +                    break;
   1.143 +                case 'F':
   1.144 +                    argType = ITEM_Float;
   1.145 +                    break;
   1.146 +                case 'D':
   1.147 +                    argType = ITEM_Double;
   1.148 +                    break;
   1.149 +                case 'L': {
   1.150 +                    i = methodSignature.indexOf(';', i + 1);
   1.151 +                    if (i == -1) {
   1.152 +                        throw new IllegalArgumentException(
   1.153 +                                      "Invalid method signature");
   1.154 +                    }
   1.155 +                    argType = ITEM_Object;
   1.156 +                    break;
   1.157 +                }
   1.158 +                case ')':
   1.159 +                    // not interested in the return value type
   1.160 +                    return argTypes;
   1.161 +                case '[':
   1.162 +                    if (!skipType) {
   1.163 +                        argTypes.add(ITEM_Object);
   1.164 +                        skipType = true;
   1.165 +                    }
   1.166 +                    continue;
   1.167 +
   1.168 +                default:
   1.169 +                    throw new IllegalArgumentException(
   1.170 +                                  "Invalid method signature");
   1.171 +            }
   1.172 +
   1.173 +            if (!skipType) {
   1.174 +                argTypes.add(argType);
   1.175 +            } else {
   1.176 +                skipType = false;
   1.177 +            }
   1.178 +        }
   1.179 +
   1.180 +        return argTypes;
   1.181 +    }
   1.182 +}