src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,380 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007, 2009, 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 com.sun.tools.classfile;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +
    1.33 +/**
    1.34 + * See JVMS, section 4.8.4.
    1.35 + *
    1.36 + *  <p><b>This is NOT part of any supported API.
    1.37 + *  If you write code that depends on this, you do so at your own risk.
    1.38 + *  This code and its internal interfaces are subject to change or
    1.39 + *  deletion without notice.</b>
    1.40 + */
    1.41 +public class StackMapTable_attribute extends Attribute {
    1.42 +    static class InvalidStackMap extends AttributeException {
    1.43 +        private static final long serialVersionUID = -5659038410855089780L;
    1.44 +        InvalidStackMap(String msg) {
    1.45 +            super(msg);
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49 +    StackMapTable_attribute(ClassReader cr, int name_index, int length)
    1.50 +            throws IOException, InvalidStackMap {
    1.51 +        super(name_index, length);
    1.52 +        number_of_entries = cr.readUnsignedShort();
    1.53 +        entries = new stack_map_frame[number_of_entries];
    1.54 +        for (int i = 0; i < number_of_entries; i++)
    1.55 +            entries[i] = stack_map_frame.read(cr);
    1.56 +    }
    1.57 +
    1.58 +    public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries)
    1.59 +            throws ConstantPoolException {
    1.60 +        this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries);
    1.61 +    }
    1.62 +
    1.63 +    public StackMapTable_attribute(int name_index, stack_map_frame[] entries) {
    1.64 +        super(name_index, length(entries));
    1.65 +        this.number_of_entries = entries.length;
    1.66 +        this.entries = entries;
    1.67 +    }
    1.68 +
    1.69 +    public <R, D> R accept(Visitor<R, D> visitor, D data) {
    1.70 +        return visitor.visitStackMapTable(this, data);
    1.71 +    }
    1.72 +
    1.73 +    static int length(stack_map_frame[] entries) {
    1.74 +        int n = 2;
    1.75 +        for (stack_map_frame entry: entries)
    1.76 +            n += entry.length();
    1.77 +        return n;
    1.78 +    }
    1.79 +
    1.80 +    public final int number_of_entries;
    1.81 +    public final stack_map_frame entries[];
    1.82 +
    1.83 +    public static abstract class stack_map_frame {
    1.84 +        static stack_map_frame read(ClassReader cr)
    1.85 +                throws IOException, InvalidStackMap {
    1.86 +            int frame_type = cr.readUnsignedByte();
    1.87 +            if (frame_type <= 63)
    1.88 +                return new same_frame(frame_type);
    1.89 +            else if (frame_type <= 127)
    1.90 +                return new same_locals_1_stack_item_frame(frame_type, cr);
    1.91 +            else if (frame_type <= 246)
    1.92 +                throw new Error("unknown frame_type " + frame_type);
    1.93 +            else if (frame_type == 247)
    1.94 +                return new same_locals_1_stack_item_frame_extended(frame_type, cr);
    1.95 +            else if (frame_type <= 250)
    1.96 +                return new chop_frame(frame_type, cr);
    1.97 +            else if (frame_type == 251)
    1.98 +                return new same_frame_extended(frame_type, cr);
    1.99 +            else if (frame_type <= 254)
   1.100 +                return new append_frame(frame_type, cr);
   1.101 +            else
   1.102 +                return new full_frame(frame_type, cr);
   1.103 +        }
   1.104 +
   1.105 +        protected stack_map_frame(int frame_type) {
   1.106 +            this.frame_type = frame_type;
   1.107 +        }
   1.108 +
   1.109 +        public int length() {
   1.110 +            return 1;
   1.111 +        }
   1.112 +
   1.113 +        public abstract int getOffsetDelta();
   1.114 +
   1.115 +        public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
   1.116 +
   1.117 +        public final int frame_type;
   1.118 +
   1.119 +        public static interface Visitor<R,P> {
   1.120 +            R visit_same_frame(same_frame frame, P p);
   1.121 +            R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p);
   1.122 +            R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p);
   1.123 +            R visit_chop_frame(chop_frame frame, P p);
   1.124 +            R visit_same_frame_extended(same_frame_extended frame, P p);
   1.125 +            R visit_append_frame(append_frame frame, P p);
   1.126 +            R visit_full_frame(full_frame frame, P p);
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +    public static class same_frame extends stack_map_frame {
   1.131 +        same_frame(int frame_type) {
   1.132 +            super(frame_type);
   1.133 +        }
   1.134 +
   1.135 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.136 +            return visitor.visit_same_frame(this, data);
   1.137 +        }
   1.138 +
   1.139 +        public int getOffsetDelta() {
   1.140 +            return frame_type;
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    public static class same_locals_1_stack_item_frame extends stack_map_frame {
   1.145 +        same_locals_1_stack_item_frame(int frame_type, ClassReader cr)
   1.146 +                throws IOException, InvalidStackMap {
   1.147 +            super(frame_type);
   1.148 +            stack = new verification_type_info[1];
   1.149 +            stack[0] = verification_type_info.read(cr);
   1.150 +        }
   1.151 +
   1.152 +        @Override
   1.153 +        public int length() {
   1.154 +            return super.length() + stack[0].length();
   1.155 +        }
   1.156 +
   1.157 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.158 +            return visitor.visit_same_locals_1_stack_item_frame(this, data);
   1.159 +        }
   1.160 +
   1.161 +        public int getOffsetDelta() {
   1.162 +            return frame_type - 64;
   1.163 +        }
   1.164 +
   1.165 +        public final verification_type_info[] stack;
   1.166 +    }
   1.167 +
   1.168 +    public static class same_locals_1_stack_item_frame_extended extends stack_map_frame {
   1.169 +        same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr)
   1.170 +                throws IOException, InvalidStackMap {
   1.171 +            super(frame_type);
   1.172 +            offset_delta = cr.readUnsignedShort();
   1.173 +            stack = new verification_type_info[1];
   1.174 +            stack[0] = verification_type_info.read(cr);
   1.175 +        }
   1.176 +
   1.177 +        @Override
   1.178 +        public int length() {
   1.179 +            return super.length() + 2 + stack[0].length();
   1.180 +        }
   1.181 +
   1.182 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.183 +            return visitor.visit_same_locals_1_stack_item_frame_extended(this, data);
   1.184 +        }
   1.185 +
   1.186 +        public int getOffsetDelta() {
   1.187 +            return offset_delta;
   1.188 +        }
   1.189 +
   1.190 +        public final int offset_delta;
   1.191 +        public final verification_type_info[] stack;
   1.192 +    }
   1.193 +
   1.194 +    public static class chop_frame extends stack_map_frame {
   1.195 +        chop_frame(int frame_type, ClassReader cr) throws IOException {
   1.196 +            super(frame_type);
   1.197 +            offset_delta = cr.readUnsignedShort();
   1.198 +        }
   1.199 +
   1.200 +        @Override
   1.201 +        public int length() {
   1.202 +            return super.length() + 2;
   1.203 +        }
   1.204 +
   1.205 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.206 +            return visitor.visit_chop_frame(this, data);
   1.207 +        }
   1.208 +
   1.209 +        public int getOffsetDelta() {
   1.210 +            return offset_delta;
   1.211 +        }
   1.212 +
   1.213 +        public final int offset_delta;
   1.214 +    }
   1.215 +
   1.216 +    public static class same_frame_extended extends stack_map_frame {
   1.217 +        same_frame_extended(int frame_type, ClassReader cr) throws IOException {
   1.218 +            super(frame_type);
   1.219 +            offset_delta = cr.readUnsignedShort();
   1.220 +        }
   1.221 +
   1.222 +        @Override
   1.223 +        public int length() {
   1.224 +            return super.length() + 2;
   1.225 +        }
   1.226 +
   1.227 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.228 +            return visitor.visit_same_frame_extended(this, data);
   1.229 +        }
   1.230 +
   1.231 +        public int getOffsetDelta() {
   1.232 +            return offset_delta;
   1.233 +        }
   1.234 +
   1.235 +        public final int offset_delta;
   1.236 +    }
   1.237 +
   1.238 +    public static class append_frame extends stack_map_frame {
   1.239 +        append_frame(int frame_type, ClassReader cr)
   1.240 +                throws IOException, InvalidStackMap {
   1.241 +            super(frame_type);
   1.242 +            offset_delta = cr.readUnsignedShort();
   1.243 +            locals = new verification_type_info[frame_type - 251];
   1.244 +            for (int i = 0; i < locals.length; i++)
   1.245 +                locals[i] = verification_type_info.read(cr);
   1.246 +        }
   1.247 +
   1.248 +        @Override
   1.249 +        public int length() {
   1.250 +            int n = super.length() + 2;
   1.251 +            for (verification_type_info local: locals)
   1.252 +                n += local.length();
   1.253 +            return n;
   1.254 +        }
   1.255 +
   1.256 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.257 +            return visitor.visit_append_frame(this, data);
   1.258 +        }
   1.259 +
   1.260 +        public int getOffsetDelta() {
   1.261 +            return offset_delta;
   1.262 +        }
   1.263 +
   1.264 +        public final int offset_delta;
   1.265 +        public final verification_type_info[] locals;
   1.266 +    }
   1.267 +
   1.268 +    public static class full_frame extends stack_map_frame {
   1.269 +        full_frame(int frame_type, ClassReader cr)
   1.270 +                throws IOException, InvalidStackMap {
   1.271 +            super(frame_type);
   1.272 +            offset_delta = cr.readUnsignedShort();
   1.273 +            number_of_locals = cr.readUnsignedShort();
   1.274 +            locals = new verification_type_info[number_of_locals];
   1.275 +            for (int i = 0; i < locals.length; i++)
   1.276 +                locals[i] = verification_type_info.read(cr);
   1.277 +            number_of_stack_items = cr.readUnsignedShort();
   1.278 +            stack = new verification_type_info[number_of_stack_items];
   1.279 +            for (int i = 0; i < stack.length; i++)
   1.280 +                stack[i] = verification_type_info.read(cr);
   1.281 +        }
   1.282 +
   1.283 +        @Override
   1.284 +        public int length() {
   1.285 +            int n = super.length() + 2;
   1.286 +            for (verification_type_info local: locals)
   1.287 +                n += local.length();
   1.288 +            n += 2;
   1.289 +            for (verification_type_info item: stack)
   1.290 +                n += item.length();
   1.291 +            return n;
   1.292 +        }
   1.293 +
   1.294 +        public <R, D> R accept(Visitor<R, D> visitor, D data) {
   1.295 +            return visitor.visit_full_frame(this, data);
   1.296 +        }
   1.297 +
   1.298 +        public int getOffsetDelta() {
   1.299 +            return offset_delta;
   1.300 +        }
   1.301 +
   1.302 +        public final int offset_delta;
   1.303 +        public final int number_of_locals;
   1.304 +        public final verification_type_info[] locals;
   1.305 +        public final int number_of_stack_items;
   1.306 +        public final verification_type_info[] stack;
   1.307 +    }
   1.308 +
   1.309 +    public static class verification_type_info {
   1.310 +        public static final int ITEM_Top = 0;
   1.311 +        public static final int ITEM_Integer = 1;
   1.312 +        public static final int ITEM_Float = 2;
   1.313 +        public static final int ITEM_Long = 4;
   1.314 +        public static final int ITEM_Double = 3;
   1.315 +        public static final int ITEM_Null = 5;
   1.316 +        public static final int ITEM_UninitializedThis = 6;
   1.317 +        public static final int ITEM_Object = 7;
   1.318 +        public static final int ITEM_Uninitialized = 8;
   1.319 +
   1.320 +        static verification_type_info read(ClassReader cr)
   1.321 +                throws IOException, InvalidStackMap {
   1.322 +            int tag = cr.readUnsignedByte();
   1.323 +            switch (tag) {
   1.324 +            case ITEM_Top:
   1.325 +            case ITEM_Integer:
   1.326 +            case ITEM_Float:
   1.327 +            case ITEM_Long:
   1.328 +            case ITEM_Double:
   1.329 +            case ITEM_Null:
   1.330 +            case ITEM_UninitializedThis:
   1.331 +                return new verification_type_info(tag);
   1.332 +
   1.333 +            case ITEM_Object:
   1.334 +                return new Object_variable_info(cr);
   1.335 +
   1.336 +            case ITEM_Uninitialized:
   1.337 +                return new Uninitialized_variable_info(cr);
   1.338 +
   1.339 +            default:
   1.340 +                throw new InvalidStackMap("unrecognized verification_type_info tag");
   1.341 +            }
   1.342 +        }
   1.343 +
   1.344 +        protected verification_type_info(int tag) {
   1.345 +            this.tag = tag;
   1.346 +        }
   1.347 +
   1.348 +        public int length() {
   1.349 +            return 1;
   1.350 +        }
   1.351 +
   1.352 +        public final int tag;
   1.353 +    }
   1.354 +
   1.355 +    public static class Object_variable_info extends verification_type_info {
   1.356 +        Object_variable_info(ClassReader cr) throws IOException {
   1.357 +            super(ITEM_Object);
   1.358 +            cpool_index = cr.readUnsignedShort();
   1.359 +        }
   1.360 +
   1.361 +        @Override
   1.362 +        public int length() {
   1.363 +            return super.length() + 2;
   1.364 +        }
   1.365 +
   1.366 +        public final int cpool_index;
   1.367 +    }
   1.368 +
   1.369 +    public static class Uninitialized_variable_info extends verification_type_info {
   1.370 +        Uninitialized_variable_info(ClassReader cr) throws IOException {
   1.371 +            super(ITEM_Uninitialized);
   1.372 +            offset = cr.readUnsignedShort();
   1.373 +        }
   1.374 +
   1.375 +        @Override
   1.376 +        public int length() {
   1.377 +            return super.length() + 2;
   1.378 +        }
   1.379 +
   1.380 +        public final int offset;
   1.381 +
   1.382 +    }
   1.383 +}

mercurial