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   * @version $Id: StackMap.html 1021978 2017-12-09 17:38:21Z ggregory $
36   * @see     Code
37   * @see     StackMapEntry
38   * @see     StackMapType
39   */
40  public final class StackMap extends Attribute {
41  
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(final int name_index, final int length, final StackMapEntry[] map, final ConstantPool constant_pool) {
52          super(Const.ATTR_STACK_MAP, name_index, length, constant_pool);
53          this.map = map;
54      }
55  
56  
57      /**
58       * Construct object from input stream.
59       *
60       * @param name_index Index of name
61       * @param length Content length in bytes
62       * @param input Input stream
63       * @param constant_pool Array of constants
64       * @throws IOException
65       */
66      StackMap(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
67          this(name_index, length, (StackMapEntry[]) null, constant_pool);
68          final int map_length = input.readUnsignedShort();
69          map = new StackMapEntry[map_length];
70          for (int i = 0; i < map_length; i++) {
71              map[i] = new StackMapEntry(input, constant_pool);
72          }
73      }
74  
75  
76      /**
77       * Dump stack map table attribute to file stream in binary format.
78       *
79       * @param file Output file stream
80       * @throws IOException
81       */
82      @Override
83      public final void dump( final DataOutputStream file ) throws IOException {
84          super.dump(file);
85          file.writeShort(map.length);
86          for (final StackMapEntry entry : map) {
87              entry.dump(file);
88          }
89      }
90  
91  
92      /**
93       * @return Array of stack map entries
94       */
95      public final StackMapEntry[] getStackMap() {
96          return map;
97      }
98  
99  
100     /**
101      * @param map Array of stack map entries
102      */
103     public final void setStackMap( final StackMapEntry[] map ) {
104         this.map = map;
105         int len = 2; // Length of 'number_of_entries' field prior to the array of stack maps
106         for (final StackMapEntry element : map) {
107             len += element.getMapEntrySize();
108         }
109         setLength(len);
110     }
111 
112 
113     /**
114      * @return String representation.
115      */
116     @Override
117     public final String toString() {
118         final StringBuilder buf = new StringBuilder("StackMap(");
119         for (int i = 0; i < map.length; i++) {
120             buf.append(map[i]);
121             if (i < map.length - 1) {
122                 buf.append(", ");
123             }
124         }
125         buf.append(')');
126         return buf.toString();
127     }
128 
129 
130     /**
131      * @return deep copy of this attribute
132      */
133     @Override
134     public Attribute copy( final ConstantPool _constant_pool ) {
135         final StackMap c = (StackMap) clone();
136         c.map = new StackMapEntry[map.length];
137         for (int i = 0; i < map.length; i++) {
138             c.map[i] = map[i].copy();
139         }
140         c.setConstantPool(_constant_pool);
141         return c;
142     }
143 
144 
145     /**
146      * Called by objects that are traversing the nodes of the tree implicitely
147      * defined by the contents of a Java class. I.e., the hierarchy of methods,
148      * fields, attributes, etc. spawns a tree of objects.
149      *
150      * @param v Visitor object
151      */
152     @Override
153     public void accept( final Visitor v ) {
154         v.visitStackMap(this);
155     }
156 
157 
158     public final int getMapLength() {
159         return map == null ? 0 : map.length;
160     }
161 }