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