KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > LocalVariable


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import org.apache.bcel.Constants;
24
25 /**
26  * This class represents a local variable within a method. It contains its
27  * scope, name, signature and index on the method's frame.
28  *
29  * @version $Id: LocalVariable.java 386056 2006-03-15 11:31:56Z tcurdt $
30  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31  * @see LocalVariableTable
32  */

33 public final class LocalVariable implements Constants, Cloneable JavaDoc, Node, Serializable JavaDoc {
34
35     private int start_pc; // Range in which the variable is valid
36
private int length;
37     private int name_index; // Index in constant pool of variable name
38
private int signature_index; // Index of variable signature
39
private int index; /* Variable is `index'th local variable on
40      * this method's frame.
41      */

42     private ConstantPool constant_pool;
43
44
45     /**
46      * Initialize from another object. Note that both objects use the same
47      * references (shallow copy). Use copy() for a physical copy.
48      */

49     public LocalVariable(LocalVariable c) {
50         this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(),
51                 c.getConstantPool());
52     }
53
54
55     /**
56      * Construct object from file stream.
57      * @param file Input stream
58      * @throws IOException
59      */

60     LocalVariable(DataInputStream JavaDoc file, ConstantPool constant_pool) throws IOException JavaDoc {
61         this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
62                 .readUnsignedShort(), file.readUnsignedShort(), constant_pool);
63     }
64
65
66     /**
67      * @param start_pc Range in which the variable
68      * @param length ... is valid
69      * @param name_index Index in constant pool of variable name
70      * @param signature_index Index of variable's signature
71      * @param index Variable is `index'th local variable on the method's frame
72      * @param constant_pool Array of constants
73      */

74     public LocalVariable(int start_pc, int length, int name_index, int signature_index, int index,
75             ConstantPool constant_pool) {
76         this.start_pc = start_pc;
77         this.length = length;
78         this.name_index = name_index;
79         this.signature_index = signature_index;
80         this.index = index;
81         this.constant_pool = constant_pool;
82     }
83
84
85     /**
86      * Called by objects that are traversing the nodes of the tree implicitely
87      * defined by the contents of a Java class. I.e., the hierarchy of methods,
88      * fields, attributes, etc. spawns a tree of objects.
89      *
90      * @param v Visitor object
91      */

92     public void accept( Visitor v ) {
93         v.visitLocalVariable(this);
94     }
95
96
97     /**
98      * Dump local variable to file stream in binary format.
99      *
100      * @param file Output file stream
101      * @throws IOException
102      */

103     public final void dump( DataOutputStream JavaDoc file ) throws IOException JavaDoc {
104         file.writeShort(start_pc);
105         file.writeShort(length);
106         file.writeShort(name_index);
107         file.writeShort(signature_index);
108         file.writeShort(index);
109     }
110
111
112     /**
113      * @return Constant pool used by this object.
114      */

115     public final ConstantPool getConstantPool() {
116         return constant_pool;
117     }
118
119
120     /**
121      * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
122      */

123     public final int getLength() {
124         return length;
125     }
126
127
128     /**
129      * @return Variable name.
130      */

131     public final String JavaDoc getName() {
132         ConstantUtf8 c;
133         c = (ConstantUtf8) constant_pool.getConstant(name_index, CONSTANT_Utf8);
134         return c.getBytes();
135     }
136
137
138     /**
139      * @return Index in constant pool of variable name.
140      */

141     public final int getNameIndex() {
142         return name_index;
143     }
144
145
146     /**
147      * @return Signature.
148      */

149     public final String JavaDoc getSignature() {
150         ConstantUtf8 c;
151         c = (ConstantUtf8) constant_pool.getConstant(signature_index, CONSTANT_Utf8);
152         return c.getBytes();
153     }
154
155
156     /**
157      * @return Index in constant pool of variable signature.
158      */

159     public final int getSignatureIndex() {
160         return signature_index;
161     }
162
163
164     /**
165      * @return index of register where variable is stored
166      */

167     public final int getIndex() {
168         return index;
169     }
170
171
172     /**
173      * @return Start of range where he variable is valid
174      */

175     public final int getStartPC() {
176         return start_pc;
177     }
178
179
180     /**
181      * @param constant_pool Constant pool to be used for this object.
182      */

183     public final void setConstantPool( ConstantPool constant_pool ) {
184         this.constant_pool = constant_pool;
185     }
186
187
188     /**
189      * @param length the length of this local variable
190      */

191     public final void setLength( int length ) {
192         this.length = length;
193     }
194
195
196     /**
197      * @param name_index the index into the constant pool for the name of this variable
198      */

199     public final void setNameIndex( int name_index ) {
200         this.name_index = name_index;
201     }
202
203
204     /**
205      * @param signature_index the index into the constant pool for the signature of this variable
206      */

207     public final void setSignatureIndex( int signature_index ) {
208         this.signature_index = signature_index;
209     }
210
211
212     /**
213      * @param index the index in the local variable table of this variable
214      */

215     public final void setIndex( int index ) {
216         this.index = index;
217     }
218
219
220     /**
221      * @param start_pc Specify range where the local variable is valid.
222      */

223     public final void setStartPC( int start_pc ) {
224         this.start_pc = start_pc;
225     }
226
227
228     /**
229      * @return string representation.
230      */

231     public final String JavaDoc toString() {
232         String JavaDoc name = getName(), signature = Utility.signatureToString(getSignature());
233         return "LocalVariable(start_pc = " + start_pc + ", length = " + length + ", index = "
234                 + index + ":" + signature + " " + name + ")";
235     }
236
237
238     /**
239      * @return deep copy of this object
240      */

241     public LocalVariable copy() {
242         try {
243             return (LocalVariable) clone();
244         } catch (CloneNotSupportedException JavaDoc e) {
245         }
246         return null;
247     }
248 }
249
Popular Tags