View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.stream.Stream;
27  
28  import org.apache.bcel.Const;
29  import org.apache.bcel.util.Args;
30  
31  /**
32   * This class represents colection of local variables in a method. This attribute is contained in the <em>Code</em>
33   * attribute.
34   *
35   * @see Code
36   * @see LocalVariable
37   */
38  public class LocalVariableTable extends Attribute implements Iterable<LocalVariable> {
39  
40      private static final LocalVariable[] EMPTY_ARRAY = {};
41  
42      private LocalVariable[] localVariableTable; // variables
43  
44      /**
45       * Constructs object from input stream.
46       *
47       * @param nameIndex Index in constant pool.
48       * @param length Content length in bytes.
49       * @param input Input stream.
50       * @param constantPool Array of constants.
51       * @throws IOException Thrown if an I/O error occurs.
52       */
53      LocalVariableTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
54          this(nameIndex, length, (LocalVariable[]) null, constantPool);
55          final int localVariableTableLength = input.readUnsignedShort();
56          localVariableTable = new LocalVariable[localVariableTableLength];
57          for (int i = 0; i < localVariableTableLength; i++) {
58              localVariableTable[i] = new LocalVariable(input, constantPool);
59          }
60      }
61  
62      /**
63       * Constructs a new LocalVariableTable.
64       *
65       * @param nameIndex Index in constant pool to 'LocalVariableTable'.
66       * @param length Content length in bytes.
67       * @param localVariableTable Table of local variables.
68       * @param constantPool Array of constants.
69       */
70      public LocalVariableTable(final int nameIndex, final int length, final LocalVariable[] localVariableTable, final ConstantPool constantPool) {
71          super(Const.ATTR_LOCAL_VARIABLE_TABLE, nameIndex, length, constantPool);
72          this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
73          Args.requireU2(this.localVariableTable.length, "localVariableTable.length");
74      }
75  
76      /**
77       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
78       * physical copy.
79       *
80       * @param c Source to copy.
81       */
82      public LocalVariableTable(final LocalVariableTable c) {
83          this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool());
84      }
85  
86      /**
87       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
88       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
89       *
90       * @param v Visitor object.
91       */
92      @Override
93      public void accept(final Visitor v) {
94          v.visitLocalVariableTable(this);
95      }
96  
97      /**
98       * @return deep copy of this attribute.
99       */
100     @Override
101     public Attribute copy(final ConstantPool constantPool) {
102         final LocalVariableTable c = (LocalVariableTable) clone();
103         c.localVariableTable = new LocalVariable[localVariableTable.length];
104         Arrays.setAll(c.localVariableTable, i -> localVariableTable[i].copy());
105         c.setConstantPool(constantPool);
106         return c;
107     }
108 
109     /**
110      * Dumps local variable table attribute to file stream in binary format.
111      *
112      * @param file Output file stream.
113      * @throws IOException Thrown if an I/O error occurs.
114      */
115     @Override
116     public final void dump(final DataOutputStream file) throws IOException {
117         super.dump(file);
118         file.writeShort(Args.requireU2(localVariableTable.length, "localVariableTable.length"));
119         for (final LocalVariable variable : localVariableTable) {
120             variable.dump(file);
121         }
122     }
123 
124     /**
125      * Gets the local variable for the given index.
126      *
127      * @param index The variable slot.
128      * @return The first LocalVariable that matches the slot or null if not found.
129      * @deprecated Since 5.2 because multiple variables can share the same slot, use getLocalVariable(int index, int pc) instead.
130      */
131     @java.lang.Deprecated
132     public final LocalVariable getLocalVariable(final int index) {
133         for (final LocalVariable variable : localVariableTable) {
134             if (variable.getIndex() == index) {
135                 return variable;
136             }
137         }
138         return null;
139     }
140 
141     /**
142      * Gets the local variable for the given index and program counter.
143      *
144      * @param index The variable slot.
145      * @param pc The current pc that this variable is alive.
146      * @return The LocalVariable that matches or null if not found.
147      */
148     public final LocalVariable getLocalVariable(final int index, final int pc) {
149         for (final LocalVariable variable : localVariableTable) {
150             if (variable.getIndex() == index) {
151                 final int startPc = variable.getStartPC();
152                 final int endPc = startPc + variable.getLength();
153                 if (pc >= startPc && pc < endPc) {
154                     return variable;
155                 }
156             }
157         }
158         return null;
159     }
160 
161     /**
162      * Gets the local variable table.
163      *
164      * @return Array of local variables of method.
165      */
166     public final LocalVariable[] getLocalVariableTable() {
167         return localVariableTable;
168     }
169 
170     /**
171      * Gets the table length.
172      *
173      * @return The table length.
174      */
175     public final int getTableLength() {
176         return localVariableTable.length;
177     }
178 
179     @Override
180     public Iterator<LocalVariable> iterator() {
181         return Stream.of(localVariableTable).iterator();
182     }
183 
184     /**
185      * Sets the local variable table.
186      *
187      * @param localVariableTable The local variable table to set.
188      */
189     public final void setLocalVariableTable(final LocalVariable[] localVariableTable) {
190         this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
191     }
192 
193     /**
194      * @return String representation.
195      */
196     @Override
197     public final String toString() {
198         final StringBuilder buf = new StringBuilder();
199         for (int i = 0; i < localVariableTable.length; i++) {
200             buf.append(localVariableTable[i]);
201             if (i < localVariableTable.length - 1) {
202                 buf.append('\n');
203             }
204         }
205         return buf.toString();
206     }
207 }