View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class represents a stack map attribute used for
28   * preverification of Java classes for the <a
29   * href="http://java.sun.com/j2me/"> Java 2 Micro Edition</a>
30   * (J2ME). This attribute is used by the <a
31   * href="http://java.sun.com/products/cldc/">KVM</a> and contained
32   * within the Code attribute of a method. See CLDC specification
33   * �5.3.1.2
34   *
35   * @see     Code
36   * @see     StackMapEntry
37   * @see     StackMapType
38   */
39  public final class StackMap extends Attribute {
40  
41      private StackMapEntry[] map; // Table of stack map entries
42  
43  
44      /*
45       * @param name_index Index of name
46       * @param length Content length in bytes
47       * @param map Table of stack map entries
48       * @param constant_pool Array of constants
49       */
50      public StackMap(final int name_index, final int length, final StackMapEntry[] map, final ConstantPool constant_pool) {
51          super(Const.ATTR_STACK_MAP, name_index, length, constant_pool);
52          this.map = map;
53      }
54  
55  
56      /**
57       * Construct object from input stream.
58       *
59       * @param name_index Index of name
60       * @param length Content length in bytes
61       * @param input Input stream
62       * @param constant_pool Array of constants
63       * @throws IOException
64       */
65      StackMap(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
66          this(name_index, length, (StackMapEntry[]) null, constant_pool);
67          final int map_length = input.readUnsignedShort();
68          map = new StackMapEntry[map_length];
69          for (int i = 0; i < map_length; i++) {
70              map[i] = new StackMapEntry(input, constant_pool);
71          }
72      }
73  
74  
75      /**
76       * Dump stack map table attribute to file stream in binary format.
77       *
78       * @param file Output file stream
79       * @throws IOException
80       */
81      @Override
82      public void dump( final DataOutputStream file ) throws IOException {
83          super.dump(file);
84          file.writeShort(map.length);
85          for (final StackMapEntry entry : map) {
86              entry.dump(file);
87          }
88      }
89  
90  
91      /**
92       * @return Array of stack map entries
93       */
94      public StackMapEntry[] getStackMap() {
95          return map;
96      }
97  
98  
99      /**
100      * @param map Array of stack map entries
101      */
102     public void setStackMap( final StackMapEntry[] map ) {
103         this.map = map;
104         int len = 2; // Length of 'number_of_entries' field prior to the array of stack maps
105         for (final StackMapEntry element : map) {
106             len += element.getMapEntrySize();
107         }
108         setLength(len);
109     }
110 
111 
112     /**
113      * @return String representation.
114      */
115     @Override
116     public String toString() {
117         final StringBuilder buf = new StringBuilder("StackMap(");
118         for (int i = 0; i < map.length; i++) {
119             buf.append(map[i]);
120             if (i < map.length - 1) {
121                 buf.append(", ");
122             }
123         }
124         buf.append(')');
125         return buf.toString();
126     }
127 
128 
129     /**
130      * @return deep copy of this attribute
131      */
132     @Override
133     public Attribute copy( final ConstantPool _constant_pool ) {
134         final StackMapf="../../../../org/apache/bcel/classfile/StackMap.html#StackMap">StackMap c = (StackMap) clone();
135         c.map = new StackMapEntry[map.length];
136         for (int i = 0; i < map.length; i++) {
137             c.map[i] = map[i].copy();
138         }
139         c.setConstantPool(_constant_pool);
140         return c;
141     }
142 
143 
144     /**
145      * Called by objects that are traversing the nodes of the tree implicitely
146      * defined by the contents of a Java class. I.e., the hierarchy of methods,
147      * fields, attributes, etc. spawns a tree of objects.
148      *
149      * @param v Visitor object
150      */
151     @Override
152     public void accept( final Visitor v ) {
153         v.visitStackMap(this);
154     }
155 
156 
157     public int getMapLength() {
158         return map == null ? 0 : map.length;
159     }
160 }