    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023import org.apache.bcel.Const;
    024
    025/**
    026 * This class represents a stack map entry recording the types of
    027 * local variables and the the of stack items at a given byte code offset.
    028 * See CLDC specification 5.3.1.2
    029 *
    030 * @see     StackMap
    031 * @see     StackMapType
    032 */
    033public final class StackMapEntry implements Node, Cloneable
    034{
    035
    036    private int frameType;
    037    private int byteCodeOffset;
    038    private StackMapType[] typesOfLocals;
    039    private StackMapType[] typesOfStackItems;
    040    private ConstantPool constantPool;
    041
    042
    043    /**
    044     * Construct object from input stream.
    045     *
    046     * @param input Input stream
    047     * @throws IOException
    048     */
    049    StackMapEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
    050        this(input.readByte() & 0xFF, -1, null, null, constantPool);
    051
    052        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
    053            byteCodeOffset = frameType - Const.SAME_FRAME;
    054        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    055                   frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    056            byteCodeOffset = frameType - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
    057            typesOfStackItems = new StackMapType[1];
    058            typesOfStackItems[0] = new StackMapType(input, constantPool);
    059        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
    060            byteCodeOffset = input.readShort();
    061            typesOfStackItems = new StackMapType[1];
    062            typesOfStackItems[0] = new StackMapType(input, constantPool);
    063        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
    064            byteCodeOffset = input.readShort();
    065        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
    066            byteCodeOffset = input.readShort();
    067        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
    068            byteCodeOffset = input.readShort();
    069            final int number_of_locals = frameType - 251;
    070            typesOfLocals = new StackMapType[number_of_locals];
    071            for (int i = 0; i < number_of_locals; i++) {
    072                typesOfLocals[i] = new StackMapType(input, constantPool);
    073            }
    074        } else if (frameType == Const.FULL_FRAME) {
    075            byteCodeOffset = input.readShort();
    076            final int number_of_locals = input.readShort();
    077            typesOfLocals = new StackMapType[number_of_locals];
    078            for (int i = 0; i < number_of_locals; i++) {
    079                typesOfLocals[i] = new StackMapType(input, constantPool);
    080            }
    081            final int number_of_stack_items = input.readShort();
    082            typesOfStackItems = new StackMapType[number_of_stack_items];
    083            for (int i = 0; i < number_of_stack_items; i++) {
    084                typesOfStackItems[i] = new StackMapType(input, constantPool);
    085            }
    086        } else {
    087            /* Can't happen */
    088            throw new ClassFormatException ("Invalid frame type found while parsing stack map table: " + frameType);
    089        }
    090    }
    091
    092    /**
    093     * DO NOT USE
    094     *
    095     * @param byteCodeOffset
    096     * @param numberOfLocals NOT USED
    097     * @param typesOfLocals array of {@link StackMapType}s of locals
    098     * @param numberOfStackItems NOT USED
    099     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
    100     * @param constantPool the constant pool
    101     * @deprecated Since 6.0, use {@link #StackMapEntry(int, int, StackMapType[], StackMapType[], ConstantPool)}
    102     * instead
    103     */
    104    @java.lang.Deprecated
    105    public StackMapEntry(final int byteCodeOffset, final int numberOfLocals,
    106            final StackMapType[] typesOfLocals, final int numberOfStackItems,
    107            final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
    108        this.byteCodeOffset = byteCodeOffset;
    109        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : new StackMapType[0];
    110        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : new StackMapType[0];
    111        this.constantPool = constantPool;
    112    }
    113
    114    /**
    115     * Create an instance
    116     *
    117     * @param tag the frameType to use
    118     * @param byteCodeOffset
    119     * @param typesOfLocals array of {@link StackMapType}s of locals
    120     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
    121     * @param constantPool the constant pool
    122     */
    123    public StackMapEntry(final int tag, final int byteCodeOffset,
    124            final StackMapType[] typesOfLocals,
    125            final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
    126        this.frameType = tag;
    127        this.byteCodeOffset = byteCodeOffset;
    128        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : new StackMapType[0];
    129        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : new StackMapType[0];
    130        this.constantPool = constantPool;
    131    }
    132
    133
    134    /**
    135     * Dump stack map entry
    136     *
    137     * @param file Output file stream
    138     * @throws IOException
    139     */
    140    public void dump( final DataOutputStream file ) throws IOException {
    141        file.write(frameType);
    142        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
    143            // nothing to be done
    144        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    145                   frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    146            typesOfStackItems[0].dump(file);
    147        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
    148            file.writeShort(byteCodeOffset);
    149            typesOfStackItems[0].dump(file);
    150        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
    151            file.writeShort(byteCodeOffset);
    152        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
    153            file.writeShort(byteCodeOffset);
    154        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
    155            file.writeShort(byteCodeOffset);
    156            for (final StackMapType type : typesOfLocals) {
    157                type.dump(file);
    158            }
    159        } else if (frameType == Const.FULL_FRAME) {
    160            file.writeShort(byteCodeOffset);
    161            file.writeShort(typesOfLocals.length);
    162            for (final StackMapType type : typesOfLocals) {
    163                type.dump(file);
    164            }
    165            file.writeShort(typesOfStackItems.length);
    166            for (final StackMapType type : typesOfStackItems) {
    167                type.dump(file);
    168            }
    169        } else {
    170            /* Can't happen */
    171            throw new ClassFormatException ("Invalid Stack map table tag: " + frameType);
    172        }
    173    }
    174
    175
    176    /**
    177     * @return String representation.
    178     */
    179    @Override
    180    public String toString() {
    181        final StringBuilder buf = new StringBuilder(64);
    182        buf.append("(");
    183        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
    184            buf.append("SAME");
    185        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    186                  frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    187            buf.append("SAME_LOCALS_1_STACK");
    188        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
    189            buf.append("SAME_LOCALS_1_STACK_EXTENDED");
    190        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
    191            buf.append("CHOP ").append(String.valueOf(251-frameType));
    192        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
    193            buf.append("SAME_EXTENDED");
    194        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
    195            buf.append("APPEND ").append(String.valueOf(frameType-251));
    196        } else if (frameType == Const.FULL_FRAME) {
    197            buf.append("FULL");
    198        } else {
    199            buf.append("UNKNOWN (").append(frameType).append(")");
    200        }
    201        buf.append(", offset delta=").append(byteCodeOffset);
    202        if (typesOfLocals.length > 0) {
    203            buf.append(", locals={");
    204            for (int i = 0; i < typesOfLocals.length; i++) {
    205                buf.append(typesOfLocals[i]);
    206                if (i < typesOfLocals.length - 1) {
    207                    buf.append(", ");
    208                }
    209            }
    210            buf.append("}");
    211        }
    212        if (typesOfStackItems.length > 0) {
    213            buf.append(", stack items={");
    214            for (int i = 0; i < typesOfStackItems.length; i++) {
    215                buf.append(typesOfStackItems[i]);
    216                if (i < typesOfStackItems.length - 1) {
    217                    buf.append(", ");
    218                }
    219            }
    220            buf.append("}");
    221        }
    222        buf.append(")");
    223        return buf.toString();
    224    }
    225
    226
    227    /**
    228     * Calculate stack map entry size
    229     *
    230     */
    231    int getMapEntrySize() {
    232        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
    233            return 1;
    234        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    235                   frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    236            return 1 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
    237        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
    238            return 3 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
    239        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
    240            return 3;
    241        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
    242            return 3;
    243        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
    244            int len = 3;
    245            for (final StackMapType types_of_local : typesOfLocals) {
    246                len += types_of_local.hasIndex() ? 3 : 1;
    247            }
    248            return len;
    249        } else if (frameType == Const.FULL_FRAME) {
    250            int len = 7;
    251            for (final StackMapType types_of_local : typesOfLocals) {
    252                len += types_of_local.hasIndex() ? 3 : 1;
    253            }
    254            for (final StackMapType types_of_stack_item : typesOfStackItems) {
    255                len += types_of_stack_item.hasIndex() ? 3 : 1;
    256            }
    257            return len;
    258        } else {
    259            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
    260        }
    261    }
    262
    263
    264    public void setFrameType( final int f ) {
    265        if (f >= Const.SAME_FRAME && f <= Const.SAME_FRAME_MAX) {
    266            byteCodeOffset = f - Const.SAME_FRAME;
    267        } else if (f >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    268                   f <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    269            byteCodeOffset = f - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
    270        } else if (f == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
    271        } else if (f >= Const.CHOP_FRAME && f <= Const.CHOP_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
    272        } else if (f == Const.SAME_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
    273        } else if (f >= Const.APPEND_FRAME && f <= Const.APPEND_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
    274        } else if (f == Const.FULL_FRAME) { // CHECKSTYLE IGNORE EmptyBlock
    275        } else {
    276            throw new IllegalArgumentException("Invalid StackMap frameType");
    277        }
    278        frameType = f;
    279    }
    280
    281
    282    public int getFrameType() {
    283        return frameType;
    284    }
    285
    286
    287    public void setByteCodeOffset( final int new_offset ) {
    288        if (new_offset < 0 || new_offset > 32767) {
    289            throw new IllegalArgumentException("Invalid StackMap offset: " + new_offset);
    290        }
    291
    292        if (frameType >= Const.SAME_FRAME &&
    293            frameType <= Const.SAME_FRAME_MAX) {
    294            if (new_offset > Const.SAME_FRAME_MAX) {
    295                frameType = Const.SAME_FRAME_EXTENDED;
    296            } else {
    297                frameType = new_offset;
    298            }
    299        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
    300                   frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
    301            if (new_offset > Const.SAME_FRAME_MAX) {
    302                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED;
    303            } else {
    304                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME + new_offset;
    305            }
    306        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
    307        } else if (frameType >= Const.CHOP_FRAME &&
    308                   frameType <= Const.CHOP_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
    309        } else if (frameType == Const.SAME_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
    310        } else if (frameType >= Const.APPEND_FRAME &&
    311                   frameType <= Const.APPEND_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
    312        } else if (frameType == Const.FULL_FRAME) { // CHECKSTYLE IGNORE EmptyBlock
    313        } else {
    314            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
    315        }
    316        byteCodeOffset = new_offset;
    317    }
    318
    319
    320    /**
    321     * Update the distance (as an offset delta) from this StackMap
    322     * entry to the next.  Note that this might cause the the
    323     * frame type to change.  Note also that delta may be negative.
    324     *
    325     * @param delta offset delta
    326     */
    327    public void updateByteCodeOffset(final int delta) {
    328        setByteCodeOffset(byteCodeOffset + delta);
    329    }
    330
    331
    332    public int getByteCodeOffset() {
    333        return byteCodeOffset;
    334    }
    335
    336
    337    /**
    338     *
    339     * @deprecated since 6.0
    340     */
    341    @java.lang.Deprecated
    342    public void setNumberOfLocals( final int n ) { // TODO unused
    343    }
    344
    345
    346    public int getNumberOfLocals() {
    347        return typesOfLocals.length;
    348    }
    349
    350
    351    public void setTypesOfLocals( final StackMapType[] types ) {
    352        typesOfLocals = types != null ? types : new StackMapType[0];
    353    }
    354
    355
    356    public StackMapType[] getTypesOfLocals() {
    357        return typesOfLocals;
    358    }
    359
    360
    361    /**
    362     *
    363     * @deprecated since 6.0
    364     */
    365    @java.lang.Deprecated
    366    public void setNumberOfStackItems( final int n ) { // TODO unused
    367    }
    368
    369
    370    public int getNumberOfStackItems() {
    371        return typesOfStackItems.length;
    372    }
    373
    374
    375    public void setTypesOfStackItems( final StackMapType[] types ) {
    376        typesOfStackItems = types != null ? types : new StackMapType[0];
    377    }
    378
    379
    380    public StackMapType[] getTypesOfStackItems() {
    381        return typesOfStackItems;
    382    }
    383
    384
    385    /**
    386     * @return deep copy of this object
    387     */
    388    public StackMapEntry copy() {
    389        StackMapEntry e;
    390        try {
    391            e = (StackMapEntry) clone();
    392        } catch (final CloneNotSupportedException ex) {
    393            throw new Error("Clone Not Supported");
    394        }
    395
    396        e.typesOfLocals = new StackMapType[typesOfLocals.length];
    397        for (int i = 0; i < typesOfLocals.length; i++) {
    398            e.typesOfLocals[i] = typesOfLocals[i].copy();
    399        }
    400        e.typesOfStackItems = new StackMapType[typesOfStackItems.length];
    401        for (int i = 0; i < typesOfStackItems.length; i++) {
    402            e.typesOfStackItems[i] = typesOfStackItems[i].copy();
    403        }
    404        return e;
    405    }
    406
    407
    408    /**
    409     * Called by objects that are traversing the nodes of the tree implicitely
    410     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    411     * fields, attributes, etc. spawns a tree of objects.
    412     *
    413     * @param v Visitor object
    414     */
    415    @Override
    416    public void accept( final Visitor v ) {
    417        v.visitStackMapEntry(this);
    418    }
    419
    420
    421    /**
    422     * @return Constant pool used by this object.
    423     */
    424    public ConstantPool getConstantPool() {
    425        return constantPool;
    426    }
    427
    428
    429    /**
    430     * @param constantPool Constant pool to be used for this object.
    431     */
    432    public void setConstantPool( final ConstantPool constantPool ) {
    433        this.constantPool = constantPool;
    434    }
    435}