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