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 a stack map attribute used for
  30  * preverification of Java classes for the <a
  31  * href="http://java.sun.com/j2me/"> Java 2 Micro Edition</a>
  32  * (J2ME). This attribute is used by the <a
  33  * href="http://java.sun.com/products/cldc/">KVM</a> and contained
  34  * within the Code attribute of a method. See CLDC specification
  35  * 5.3.1.2
  36  *
  37  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  38  * @see     Code
  39  * @see     StackMapEntry
  40  * @see     StackMapType
  41  */
  42 public final class StackMap extends Attribute implements Node {
  43   private int             map_length;
  44   private StackMapEntry[] map; // Table of stack map entries
  45 
  46   /*
  47    * @param name_index Index of name
  48    * @param length Content length in bytes
  49    * @param map Table of stack map entries
  50    * @param constant_pool Array of constants
  51    */
  52   public StackMap(int name_index, int length,  StackMapEntry[] map,
  53                   ConstantPool constant_pool)
  54   {
  55     super(Constants.ATTR_STACK_MAP, name_index, length, constant_pool);
  56 
  57     setStackMap(map);
  58   }
  59 
  60   /**
  61    * Construct object from file stream.
  62    * @param name_index Index of name
  63    * @param length Content length in bytes
  64    * @param file Input stream
  65    * @throws IOException
  66    * @param constant_pool Array of constants
  67    */
  68   StackMap(int name_index, int length, DataInputStream file,
  69            ConstantPool constant_pool) throws IOException
  70   {
  71     this(name_index, length, (StackMapEntry[])null, constant_pool);
  72 
  73     map_length = file.readUnsignedShort();
  74     map = new StackMapEntry[map_length];
  75 
  76     for(int i=0; i < map_length; i++)
  77       map[i] = new StackMapEntry(file, constant_pool);
  78   }
  79 
  80   /**
  81    * Dump line number table attribute to file stream in binary format.
  82    *
  83    * @param file Output file stream
  84    * @throws IOException
  85    */
  86   public final void dump(DataOutputStream file) throws IOException
  87   {
  88     super.dump(file);
  89     file.writeShort(map_length);
  90     for(int i=0; i < map_length; i++)
  91       map[i].dump(file);
  92   }
  93 
  94   /**
  95    * @return Array of stack map entries
  96    */
  97   public final StackMapEntry[] getStackMap() { return map; }
  98 
  99   /**
 100    * @param map Array of stack map entries
 101    */
 102   public final void setStackMap(StackMapEntry[] map) {
 103     this.map = map;
 104 
 105     map_length = (map == null)? 0 : map.length;
 106   }
 107 
 108   /**
 109    * @return String representation.
 110    */
 111   public final String toString() {
 112     StringBuffer buf = new StringBuffer("StackMap(");
 113 
 114     for(int i=0; i < map_length; i++) {
 115       buf.append(map[i].toString());
 116 
 117       if(i < map_length - 1)
 118         buf.append(", ");
 119     }
 120 
 121     buf.append(')');
 122 
 123     return buf.toString();
 124   }
 125 
 126   /**
 127    * @return deep copy of this attribute
 128    */
 129   public Attribute copy(ConstantPool constant_pool) {
 130     StackMap c = (StackMap)clone();
 131 
 132     c.map = new StackMapEntry[map_length];
 133     for(int i=0; i < map_length; i++)
 134       c.map[i] = map[i].copy();
 135 
 136     c.constant_pool = constant_pool;
 137     return c;
 138   }
 139 
 140   /**
 141    * Called by objects that are traversing the nodes of the tree implicitely
 142    * defined by the contents of a Java class. I.e., the hierarchy of methods,
 143    * fields, attributes, etc. spawns a tree of objects.
 144    *
 145    * @param v Visitor object
 146    */
 147    public void accept(Visitor v) {
 148      v.visitStackMap(this);
 149    }
 150 
 151   public final int getMapLength() { return map_length; }
 152 }