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  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import org.apache.bcel.Const;
25  import org.apache.bcel.util.Args;
26  
27  /**
28   * This class represents a stack map attribute used for preverification of Java classes for the
29   * <a href="https://java.sun.com/j2me/"> Java 2 Micro Edition</a> (J2ME). This attribute is used by the
30   * <a href="https://java.sun.com/products/cldc/">KVM</a> and contained within the Code attribute of a method. See CLDC
31   * specification �5.3.1.2
32   *
33   * <pre>
34   * StackMapTable_attribute {
35   *   u2              attribute_name_index;
36   *   u4              attribute_length;
37   *   u2              number_of_entries;
38   *   stack_map_frame entries[number_of_entries];
39   * }
40   * </pre>
41   *
42   * @see Code
43   * @see StackMapEntry
44   * @see StackMapType
45   */
46  public final class StackMap extends Attribute {
47  
48      private StackMapEntry[] table; // Table of stack map entries
49  
50      /**
51       * Constructs object from input stream.
52       *
53       * @param nameIndex Index of name
54       * @param length Content length in bytes
55       * @param dataInput Input stream
56       * @param constantPool Array of constants
57       * @throws IOException if an I/O error occurs.
58       */
59      StackMap(final int nameIndex, final int length, final DataInput dataInput, final ConstantPool constantPool) throws IOException {
60          this(nameIndex, length, (StackMapEntry[]) null, constantPool);
61          final int mapLength = dataInput.readUnsignedShort();
62          table = new StackMapEntry[mapLength];
63          for (int i = 0; i < mapLength; i++) {
64              table[i] = new StackMapEntry(dataInput, constantPool);
65          }
66      }
67  
68      /*
69       * @param nameIndex Index of name
70       *
71       * @param length Content length in bytes
72       *
73       * @param map Table of stack map entries
74       *
75       * @param constantPool Array of constants
76       */
77      public StackMap(final int nameIndex, final int length, final StackMapEntry[] table, final ConstantPool constantPool) {
78          super(Const.ATTR_STACK_MAP, nameIndex, length, constantPool);
79          this.table = table != null ? table : StackMapEntry.EMPTY_ARRAY;
80          Args.requireU2(this.table.length, "table.length");
81      }
82  
83      /**
84       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
85       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
86       *
87       * @param v Visitor object
88       */
89      @Override
90      public void accept(final Visitor v) {
91          v.visitStackMap(this);
92      }
93  
94      /**
95       * @return deep copy of this attribute
96       */
97      @Override
98      public Attribute copy(final ConstantPool constantPool) {
99          final StackMap c = (StackMap) clone();
100         c.table = new StackMapEntry[table.length];
101         Arrays.setAll(c.table, i -> table[i].copy());
102         c.setConstantPool(constantPool);
103         return c;
104     }
105 
106     /**
107      * Dump stack map table attribute to file stream in binary format.
108      *
109      * @param file Output file stream
110      * @throws IOException if an I/O error occurs.
111      */
112     @Override
113     public void dump(final DataOutputStream file) throws IOException {
114         super.dump(file);
115         file.writeShort(table.length);
116         for (final StackMapEntry entry : table) {
117             entry.dump(file);
118         }
119     }
120 
121     public int getMapLength() {
122         return table.length;
123     }
124 
125     /**
126      * @return Array of stack map entries
127      */
128     public StackMapEntry[] getStackMap() {
129         return table;
130     }
131 
132     /**
133      * @param table Array of stack map entries
134      */
135     public void setStackMap(final StackMapEntry[] table) {
136         this.table = table != null ? table : StackMapEntry.EMPTY_ARRAY;
137         int len = 2; // Length of 'number_of_entries' field prior to the array of stack maps
138         for (final StackMapEntry element : this.table) {
139             len += element.getMapEntrySize();
140         }
141         setLength(len);
142     }
143 
144     /**
145      * @return String representation.
146      */
147     @Override
148     public String toString() {
149         final StringBuilder buf = new StringBuilder("StackMap(");
150         int runningOffset = -1; // no +1 on first entry
151         for (int i = 0; i < table.length; i++) {
152             runningOffset = table[i].getByteCodeOffset() + runningOffset + 1;
153             buf.append(String.format("%n@%03d %s", runningOffset, table[i]));
154             if (i < table.length - 1) {
155                 buf.append(", ");
156             }
157         }
158         buf.append(')');
159         return buf.toString();
160     }
161 }