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 import java.io.Serializable;
24
25 import org.apache.bcel.Constants;
26
27 /**
28 * This class represents a local variable within a method. It contains its
29 * scope, name, signature and index on the method's frame.
30 *
31 * @version $Id: LocalVariable.java 1152072 2011-07-29 01:54:05Z dbrosius $
32 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
33 * @see LocalVariableTable
34 */
35 public final class LocalVariable implements Constants, Cloneable, Node, Serializable {
36
37 private static final long serialVersionUID = -51081099265972179L;
38 private int start_pc; // Range in which the variable is valid
39 private int length;
40 private int name_index; // Index in constant pool of variable name
41 private int signature_index; // Index of variable signature
42 private int index; /* Variable is `index'th local variable on
43 * this method's frame.
44 */
45 private ConstantPool constant_pool;
46
47
48 /**
49 * Initialize from another object. Note that both objects use the same
50 * references (shallow copy). Use copy() for a physical copy.
51 */
52 public LocalVariable(LocalVariable c) {
53 this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(),
54 c.getConstantPool());
55 }
56
57
58 /**
59 * Construct object from file stream.
60 * @param file Input stream
61 * @throws IOException
62 */
63 LocalVariable(DataInput file, ConstantPool constant_pool) throws IOException {
64 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
65 .readUnsignedShort(), file.readUnsignedShort(), constant_pool);
66 }
67
68
69 /**
70 * @param start_pc Range in which the variable
71 * @param length ... is valid
72 * @param name_index Index in constant pool of variable name
73 * @param signature_index Index of variable's signature
74 * @param index Variable is `index'th local variable on the method's frame
75 * @param constant_pool Array of constants
76 */
77 public LocalVariable(int start_pc, int length, int name_index, int signature_index, int index,
78 ConstantPool constant_pool) {
79 this.start_pc = start_pc;
80 this.length = length;
81 this.name_index = name_index;
82 this.signature_index = signature_index;
83 this.index = index;
84 this.constant_pool = constant_pool;
85 }
86
87
88 /**
89 * Called by objects that are traversing the nodes of the tree implicitely
90 * defined by the contents of a Java class. I.e., the hierarchy of methods,
91 * fields, attributes, etc. spawns a tree of objects.
92 *
93 * @param v Visitor object
94 */
95 public void accept( Visitor v ) {
96 v.visitLocalVariable(this);
97 }
98
99
100 /**
101 * Dump local variable to file stream in binary format.
102 *
103 * @param file Output file stream
104 * @throws IOException
105 */
106 public final void dump( DataOutputStream file ) throws IOException {
107 file.writeShort(start_pc);
108 file.writeShort(length);
109 file.writeShort(name_index);
110 file.writeShort(signature_index);
111 file.writeShort(index);
112 }
113
114
115 /**
116 * @return Constant pool used by this object.
117 */
118 public final ConstantPool getConstantPool() {
119 return constant_pool;
120 }
121
122
123 /**
124 * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
125 */
126 public final int getLength() {
127 return length;
128 }
129
130
131 /**
132 * @return Variable name.
133 */
134 public final String getName() {
135 ConstantUtf8 c;
136 c = (ConstantUtf8) constant_pool.getConstant(name_index, CONSTANT_Utf8);
137 return c.getBytes();
138 }
139
140
141 /**
142 * @return Index in constant pool of variable name.
143 */
144 public final int getNameIndex() {
145 return name_index;
146 }
147
148
149 /**
150 * @return Signature.
151 */
152 public final String getSignature() {
153 ConstantUtf8 c;
154 c = (ConstantUtf8) constant_pool.getConstant(signature_index, CONSTANT_Utf8);
155 return c.getBytes();
156 }
157
158
159 /**
160 * @return Index in constant pool of variable signature.
161 */
162 public final int getSignatureIndex() {
163 return signature_index;
164 }
165
166
167 /**
168 * @return index of register where variable is stored
169 */
170 public final int getIndex() {
171 return index;
172 }
173
174
175 /**
176 * @return Start of range where he variable is valid
177 */
178 public final int getStartPC() {
179 return start_pc;
180 }
181
182
183 /**
184 * @param constant_pool Constant pool to be used for this object.
185 */
186 public final void setConstantPool( ConstantPool constant_pool ) {
187 this.constant_pool = constant_pool;
188 }
189
190
191 /**
192 * @param length the length of this local variable
193 */
194 public final void setLength( int length ) {
195 this.length = length;
196 }
197
198
199 /**
200 * @param name_index the index into the constant pool for the name of this variable
201 */
202 public final void setNameIndex( int name_index ) {
203 this.name_index = name_index;
204 }
205
206
207 /**
208 * @param signature_index the index into the constant pool for the signature of this variable
209 */
210 public final void setSignatureIndex( int signature_index ) {
211 this.signature_index = signature_index;
212 }
213
214
215 /**
216 * @param index the index in the local variable table of this variable
217 */
218 public final void setIndex( int index ) {
219 this.index = index;
220 }
221
222
223 /**
224 * @param start_pc Specify range where the local variable is valid.
225 */
226 public final void setStartPC( int start_pc ) {
227 this.start_pc = start_pc;
228 }
229
230
231 /**
232 * @return string representation.
233 */
234 @Override
235 public final String toString() {
236 String name = getName(), signature = Utility.signatureToString(getSignature());
237 return "LocalVariable(start_pc = " + start_pc + ", length = " + length + ", index = "
238 + index + ":" + signature + " " + name + ")";
239 }
240
241
242 /**
243 * @return deep copy of this object
244 */
245 public LocalVariable copy() {
246 try {
247 return (LocalVariable) clone();
248 } catch (CloneNotSupportedException e) {
249 }
250 return null;
251 }
252 }