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