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  import java.util.Iterator;
24  import java.util.stream.Stream;
25  
26  import org.apache.bcel.Const;
27  import org.apache.bcel.util.Args;
28  
29  /**
30   * This class represents colection of local variables in a method. This attribute is contained in the <em>Code</em>
31   * attribute.
32   *
33   * @see Code
34   * @see LocalVariable
35   */
36  public class LocalVariableTable extends Attribute implements Iterable<LocalVariable> {
37  
38      private static final LocalVariable[] EMPTY_ARRAY = {};
39  
40      private LocalVariable[] localVariableTable; // variables
41  
42      /**
43       * Constructs object from input stream.
44       *
45       * @param nameIndex Index in constant pool
46       * @param length Content length in bytes
47       * @param input Input stream
48       * @param constantPool Array of constants
49       * @throws IOException if an I/O error occurs.
50       */
51      LocalVariableTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
52          this(nameIndex, length, (LocalVariable[]) null, constantPool);
53          final int localVariableTableLength = input.readUnsignedShort();
54          localVariableTable = new LocalVariable[localVariableTableLength];
55          for (int i = 0; i < localVariableTableLength; i++) {
56              localVariableTable[i] = new LocalVariable(input, constantPool);
57          }
58      }
59  
60      /**
61       * @param nameIndex Index in constant pool to 'LocalVariableTable'
62       * @param length Content length in bytes
63       * @param localVariableTable Table of local variables
64       * @param constantPool Array of constants
65       */
66      public LocalVariableTable(final int nameIndex, final int length, final LocalVariable[] localVariableTable, final ConstantPool constantPool) {
67          super(Const.ATTR_LOCAL_VARIABLE_TABLE, nameIndex, length, constantPool);
68          this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
69          Args.requireU2(this.localVariableTable.length, "localVariableTable.length");
70      }
71  
72      /**
73       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
74       * physical copy.
75       *
76       * @param c Source to copy.
77       */
78      public LocalVariableTable(final LocalVariableTable c) {
79          this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool());
80      }
81  
82      /**
83       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
84       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
85       *
86       * @param v Visitor object
87       */
88      @Override
89      public void accept(final Visitor v) {
90          v.visitLocalVariableTable(this);
91      }
92  
93      /**
94       * @return deep copy of this attribute
95       */
96      @Override
97      public Attribute copy(final ConstantPool constantPool) {
98          final LocalVariableTable c = (LocalVariableTable) clone();
99          c.localVariableTable = new LocalVariable[localVariableTable.length];
100         Arrays.setAll(c.localVariableTable, i -> localVariableTable[i].copy());
101         c.setConstantPool(constantPool);
102         return c;
103     }
104 
105     /**
106      * Dump local variable table attribute to file stream in binary format.
107      *
108      * @param file Output file stream
109      * @throws IOException if an I/O error occurs.
110      */
111     @Override
112     public final void dump(final DataOutputStream file) throws IOException {
113         super.dump(file);
114         file.writeShort(localVariableTable.length);
115         for (final LocalVariable variable : localVariableTable) {
116             variable.dump(file);
117         }
118     }
119 
120     /**
121      *
122      * @param index the variable slot
123      *
124      * @return the first LocalVariable that matches the slot or null if not found
125      *
126      * @deprecated since 5.2 because multiple variables can share the same slot, use getLocalVariable(int index, int pc)
127      *             instead.
128      */
129     @java.lang.Deprecated
130     public final LocalVariable getLocalVariable(final int index) {
131         for (final LocalVariable variable : localVariableTable) {
132             if (variable.getIndex() == index) {
133                 return variable;
134             }
135         }
136         return null;
137     }
138 
139     /**
140      *
141      * @param index the variable slot
142      * @param pc the current pc that this variable is alive
143      *
144      * @return the LocalVariable that matches or null if not found
145      */
146     public final LocalVariable getLocalVariable(final int index, final int pc) {
147         for (final LocalVariable variable : localVariableTable) {
148             if (variable.getIndex() == index) {
149                 final int startPc = variable.getStartPC();
150                 final int endPc = startPc + variable.getLength();
151                 if (pc >= startPc && pc <= endPc) {
152                     return variable;
153                 }
154             }
155         }
156         return null;
157     }
158 
159     /**
160      * @return Array of local variables of method.
161      */
162     public final LocalVariable[] getLocalVariableTable() {
163         return localVariableTable;
164     }
165 
166     public final int getTableLength() {
167         return localVariableTable.length;
168     }
169 
170     @Override
171     public Iterator<LocalVariable> iterator() {
172         return Stream.of(localVariableTable).iterator();
173     }
174 
175     public final void setLocalVariableTable(final LocalVariable[] localVariableTable) {
176         this.localVariableTable = localVariableTable != null ? localVariableTable : EMPTY_ARRAY;
177     }
178 
179     /**
180      * @return String representation.
181      */
182     @Override
183     public final String toString() {
184         final StringBuilder buf = new StringBuilder();
185         for (int i = 0; i < localVariableTable.length; i++) {
186             buf.append(localVariableTable[i]);
187             if (i < localVariableTable.length - 1) {
188                 buf.append('\n');
189             }
190         }
191         return buf.toString();
192     }
193 }