javap/src/main/java/org/apidesign/javap/StackMapTableData.java
branchjavap
changeset 167 77f7135b6eb1
parent 149 32653a09f0db
child 221 3ee23267706c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javap/src/main/java/org/apidesign/javap/StackMapTableData.java	Fri Nov 16 08:08:36 2012 +0100
     1.3 @@ -0,0 +1,126 @@
     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 +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 +    static class SameFrame extends StackMapTableData {
    1.47 +        SameFrame(int frameType, int offsetDelta) {
    1.48 +            super(frameType);
    1.49 +            this.offsetDelta = offsetDelta;
    1.50 +        }
    1.51 +    }
    1.52 +
    1.53 +    static class SameLocals1StackItem extends StackMapTableData {
    1.54 +        final int[] stack;
    1.55 +        SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
    1.56 +            super(frameType);
    1.57 +            this.offsetDelta = offsetDelta;
    1.58 +            this.stack = stack;
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +    static class ChopFrame extends StackMapTableData {
    1.63 +        ChopFrame(int frameType, int offsetDelta) {
    1.64 +            super(frameType);
    1.65 +            this.offsetDelta = offsetDelta;
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    static class AppendFrame extends StackMapTableData {
    1.70 +        final int[] locals;
    1.71 +        AppendFrame(int frameType, int offsetDelta, int[] locals) {
    1.72 +            super(frameType);
    1.73 +            this.offsetDelta = offsetDelta;
    1.74 +            this.locals = locals;
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +    static class FullFrame extends StackMapTableData {
    1.79 +        final int[] locals;
    1.80 +        final int[] stack;
    1.81 +        FullFrame(int offsetDelta, int[] locals, int[] stack) {
    1.82 +            super(FULL_FRAME);
    1.83 +            this.offsetDelta = offsetDelta;
    1.84 +            this.locals = locals;
    1.85 +            this.stack = stack;
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    static StackMapTableData getInstance(DataInputStream in, MethodData method)
    1.90 +                  throws IOException {
    1.91 +        int frameType = in.readUnsignedByte();
    1.92 +
    1.93 +        if (frameType < SAME_FRAME_BOUND) {
    1.94 +            // same_frame
    1.95 +            return new SameFrame(frameType, frameType);
    1.96 +        } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
    1.97 +            // same_locals_1_stack_item_frame
    1.98 +            // read additional single stack element
    1.99 +            return new SameLocals1StackItem(frameType,
   1.100 +                                            (frameType - SAME_FRAME_BOUND),
   1.101 +                                            StackMapData.readTypeArray(in, 1, method));
   1.102 +        } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
   1.103 +            // same_locals_1_stack_item_extended
   1.104 +            return new SameLocals1StackItem(frameType,
   1.105 +                                            in.readUnsignedShort(),
   1.106 +                                            StackMapData.readTypeArray(in, 1, method));
   1.107 +        } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType  && frameType < SAME_FRAME_EXTENDED) {
   1.108 +            // chop_frame or same_frame_extended
   1.109 +            return new ChopFrame(frameType, in.readUnsignedShort());
   1.110 +        } else if (frameType == SAME_FRAME_EXTENDED) {
   1.111 +            // chop_frame or same_frame_extended
   1.112 +            return new SameFrame(frameType, in.readUnsignedShort());
   1.113 +        } else if (SAME_FRAME_EXTENDED < frameType  && frameType < FULL_FRAME) {
   1.114 +            // append_frame
   1.115 +            return new AppendFrame(frameType, in.readUnsignedShort(),
   1.116 +                                   StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
   1.117 +        } else if (frameType == FULL_FRAME) {
   1.118 +            // full_frame
   1.119 +            int offsetDelta = in.readUnsignedShort();
   1.120 +            int locals_size = in.readUnsignedShort();
   1.121 +            int[] locals = StackMapData.readTypeArray(in, locals_size, method);
   1.122 +            int stack_size = in.readUnsignedShort();
   1.123 +            int[] stack = StackMapData.readTypeArray(in, stack_size, method);
   1.124 +            return new FullFrame(offsetDelta, locals, stack);
   1.125 +        } else {
   1.126 +            throw new ClassFormatError("unrecognized frame_type in StackMapTable");
   1.127 +        }
   1.128 +    }
   1.129 +}