StackMapEntry xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.classfile;
    18
    19  import java.io.DataInputStream;
    20  import java.io.DataOutputStream;
    21  import java.io.IOException;
    22
    23  /***
    24   * This class represents a stack map entry recording the types of
    25   * local variables and the the of stack items at a given byte code offset.
    26   * See CLDC specification ß5.3.1.2
    27   *
    28   * @version $Id: StackMapEntry.java 386056 2006-03-15 11:31:56Z tcurdt $
    29   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    30   * @see     StackMap
    31   * @see     StackMapType
    32   */
    33  public final class StackMapEntry implements Cloneable {
    34
    35      private int byte_code_offset;
    36      private int number_of_locals;
    37      private StackMapType[] types_of_locals;
    38      private int number_of_stack_items;
    39      private StackMapType[] types_of_stack_items;
    40      private ConstantPool constant_pool;
    41
    42
    43      /***
    44       * Construct object from file stream.
    45       * @param file Input stream
    46       * @throws IOException
    47       */
    48      StackMapEntry(DataInputStream file, ConstantPool constant_pool) throws IOException {
    49          this(file.readShort(), file.readShort(), null, -1, null, constant_pool);
    50          types_of_locals = new StackMapType[number_of_locals];
    51          for (int i = 0; i < number_of_locals; i++) {
    52              types_of_locals[i] = new StackMapType(file, constant_pool);
    53          }
    54          number_of_stack_items = file.readShort();
    55          types_of_stack_items = new StackMapType[number_of_stack_items];
    56          for (int i = 0; i < number_of_stack_items; i++) {
    57              types_of_stack_items[i] = new StackMapType(file, constant_pool);
    58          }
    59      }
    60
    61
    62      public StackMapEntry(int byte_code_offset, int number_of_locals,
    63              StackMapType[] types_of_locals, int number_of_stack_items,
    64              StackMapType[] types_of_stack_items, ConstantPool constant_pool) {
    65          this.byte_code_offset = byte_code_offset;
    66          this.number_of_locals = number_of_locals;
    67          this.types_of_locals = types_of_locals;
    68          this.number_of_stack_items = number_of_stack_items;
    69          this.types_of_stack_items = types_of_stack_items;
    70          this.constant_pool = constant_pool;
    71      }
    72
    73
    74      /***
    75       * Dump stack map entry
    76       *
    77       * @param file Output file stream
    78       * @throws IOException
    79       */
    80      public final void dump( DataOutputStream file ) throws IOException {
    81          file.writeShort(byte_code_offset);
    82          file.writeShort(number_of_locals);
    83          for (int i = 0; i < number_of_locals; i++) {
    84              types_of_locals[i].dump(file);
    85          }
    86          file.writeShort(number_of_stack_items);
    87          for (int i = 0; i < number_of_stack_items; i++) {
    88              types_of_stack_items[i].dump(file);
    89          }
    90      }
    91
    92
    93      /***
    94       * @return String representation.
    95       */
    96      public final String toString() {
    97          StringBuffer buf = new StringBuffer(64);
    98          buf.append("(offset=").append(byte_code_offset);
    99          if (number_of_locals > 0) {
    100             buf.append(", locals={");
    101             for (int i = 0; i < number_of_locals; i++) {
    102                 buf.append(types_of_locals[i]);
    103                 if (i < number_of_locals - 1) {
    104                     buf.append(", ");
    105                 }
    106             }
    107             buf.append("}");
    108         }
    109         if (number_of_stack_items > 0) {
    110             buf.append(", stack items={");
    111             for (int i = 0; i < number_of_stack_items; i++) {
    112                 buf.append(types_of_stack_items[i]);
    113                 if (i < number_of_stack_items - 1) {
    114                     buf.append(", ");
    115                 }
    116             }
    117             buf.append("}");
    118         }
    119         buf.append(")");
    120         return buf.toString();
    121     }
    122
    123
    124     public void setByteCodeOffset( int b ) {
    125         byte_code_offset = b;
    126     }
    127
    128
    129     public int getByteCodeOffset() {
    130         return byte_code_offset;
    131     }
    132
    133
    134     public void setNumberOfLocals( int n ) {
    135         number_of_locals = n;
    136     }
    137
    138
    139     public int getNumberOfLocals() {
    140         return number_of_locals;
    141     }
    142
    143
    144     public void setTypesOfLocals( StackMapType[] t ) {
    145         types_of_locals = t;
    146     }
    147
    148
    149     public StackMapType[] getTypesOfLocals() {
    150         return types_of_locals;
    151     }
    152
    153
    154     public void setNumberOfStackItems( int n ) {
    155         number_of_stack_items = n;
    156     }
    157
    158
    159     public int getNumberOfStackItems() {
    160         return number_of_stack_items;
    161     }
    162
    163
    164     public void setTypesOfStackItems( StackMapType[] t ) {
    165         types_of_stack_items = t;
    166     }
    167
    168
    169     public StackMapType[] getTypesOfStackItems() {
    170         return types_of_stack_items;
    171     }
    172
    173
    174     /***
    175      * @return deep copy of this object
    176      */
    177     public StackMapEntry copy() {
    178         try {
    179             return (StackMapEntry) clone();
    180         } catch (CloneNotSupportedException e) {
    181         }
    182         return null;
    183     }
    184
    185
    186     /***
    187      * Called by objects that are traversing the nodes of the tree implicitely
    188      * defined by the contents of a Java class. I.e., the hierarchy of methods,
    189      * fields, attributes, etc. spawns a tree of objects.
    190      *
    191      * @param v Visitor object
    192      */
    193     public void accept( Visitor v ) {
    194         v.visitStackMapEntry(this);
    195     }
    196
    197
    198     /***
    199      * @return Constant pool used by this object.
    200      */
    201     public final ConstantPool getConstantPool() {
    202         return constant_pool;
    203     }
    204
    205
    206     /***
    207      * @param constant_pool Constant pool to be used for this object.
    208      */
    209     public final void setConstantPool( ConstantPool constant_pool ) {
    210         this.constant_pool = constant_pool;
    211     }
    212 }