1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 
  25 import  com.sun.org.apache.bcel.internal.Constants;
  26 import  java.io.*;
  27 
  28 /**
  29  * This class represents the type of a local variable or item on stack
  30  * used in the StackMap entries.
  31  *
  32  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  33  * @see     StackMapEntry
  34  * @see     StackMap
  35  * @see     Constants
  36  */
  37 public final class StackMapType implements Cloneable {
  38   private byte         type;
  39   private int          index = -1; // Index to CONSTANT_Class or offset
  40   private ConstantPool constant_pool;
  41 
  42   /**
  43    * Construct object from file stream.
  44    * @param file Input stream
  45    * @throws IOException
  46    */
  47   StackMapType(DataInputStream file, ConstantPool constant_pool) throws IOException
  48   {
  49     this(file.readByte(), -1, constant_pool);
  50 
  51     if(hasIndex())
  52       setIndex(file.readShort());
  53 
  54     setConstantPool(constant_pool);
  55   }
  56 
  57   /**
  58    * @param type type tag as defined in the Constants interface
  59    * @param index index to constant pool, or byte code offset
  60    */
  61   public StackMapType(byte type, int index, ConstantPool constant_pool) {
  62     setType(type);
  63     setIndex(index);
  64     setConstantPool(constant_pool);
  65   }
  66 
  67   public void setType(byte t) {
  68     if((t < Constants.ITEM_Bogus) || (t > Constants.ITEM_NewObject))
  69       throw new RuntimeException("Illegal type for StackMapType: " + t);
  70     type = t;
  71   }
  72 
  73   public byte getType()       { return type; }
  74   public void setIndex(int t) { index = t; }
  75 
  76   /** @return index to constant pool if type == ITEM_Object, or offset
  77    * in byte code, if type == ITEM_NewObject, and -1 otherwise
  78    */
  79   public int  getIndex()      { return index; }
  80 
  81   /**
  82    * Dump type entries to file.
  83    *
  84    * @param file Output file stream
  85    * @throws IOException
  86    */
  87   public final void dump(DataOutputStream file) throws IOException
  88   {
  89     file.writeByte(type);
  90     if(hasIndex())
  91       file.writeShort(getIndex());
  92   }
  93 
  94   /** @return true, if type is either ITEM_Object or ITEM_NewObject
  95    */
  96   public final boolean hasIndex() {
  97     return ((type == Constants.ITEM_Object) ||
  98             (type == Constants.ITEM_NewObject));
  99   }
 100 
 101   private String printIndex() {
 102     if(type == Constants.ITEM_Object)
 103       return ", class=" + constant_pool.constantToString(index, Constants.CONSTANT_Class);
 104     else if(type == Constants.ITEM_NewObject)
 105       return ", offset=" + index;
 106     else
 107       return "";
 108   }
 109 
 110   /**
 111    * @return String representation
 112    */
 113   public final String toString() {
 114     return "(type=" + Constants.ITEM_NAMES[type] + printIndex() + ")";
 115   }
 116 
 117   /**
 118    * @return deep copy of this object
 119    */
 120   public StackMapType copy() {
 121     try {
 122       return (StackMapType)clone();
 123     } catch(CloneNotSupportedException e) {}
 124 
 125     return null;
 126   }
 127 
 128   /**
 129    * @return Constant pool used by this object.
 130    */
 131   public final ConstantPool getConstantPool() { return constant_pool; }
 132 
 133   /**
 134    * @param constant_pool Constant pool to be used for this object.
 135    */
 136   public final void setConstantPool(ConstantPool constant_pool) {
 137     this.constant_pool = constant_pool;
 138   }
 139 }