KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > classfile > LocalVariable


1 package com.sun.org.apache.bcel.internal.classfile;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.Constants;
58 import java.io.*;
59
60 /**
61  * This class represents a local variable within a method. It contains its
62  * scope, name, signature and index on the method's frame.
63  *
64  * @version $Id: LocalVariable.java,v 1.1.1.1 2001/10/29 20:00:02 jvanzyl Exp $
65  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
66  * @see LocalVariableTable
67  */

68 public final class LocalVariable implements Constants, Cloneable JavaDoc, Node {
69   private int start_pc; // Range in which the variable is valid
70
private int length;
71   private int name_index; // Index in constant pool of variable name
72
private int signature_index; // Index of variable signature
73
private int index; /* Variable is `index'th local variable on
74                 * this method's frame.
75                 */

76
77   private ConstantPool constant_pool;
78
79   /**
80    * Initialize from another object. Note that both objects use the same
81    * references (shallow copy). Use copy() for a physical copy.
82    */

83   public LocalVariable(LocalVariable c) {
84     this(c.getStartPC(), c.getLength(), c.getNameIndex(),
85      c.getSignatureIndex(), c.getIndex(), c.getConstantPool());
86   }
87
88   /**
89    * Construct object from file stream.
90    * @param file Input stream
91    * @throw IOException
92    */

93   LocalVariable(DataInputStream file, ConstantPool constant_pool)
94        throws IOException
95   {
96     this(file.readUnsignedShort(), file.readUnsignedShort(),
97      file.readUnsignedShort(), file.readUnsignedShort(),
98      file.readUnsignedShort(), constant_pool);
99   }
100
101   /**
102    * @param start_pc Range in which the variable
103    * @param length ... is valid
104    * @param name_index Index in constant pool of variable name
105    * @param signature_index Index of variable's signature
106    * @param index Variable is `index'th local variable on the method's frame
107    * @param constant_pool Array of constants
108    */

109   public LocalVariable(int start_pc, int length, int name_index,
110                int signature_index, int index,
111                ConstantPool constant_pool)
112   {
113     this.start_pc = start_pc;
114     this.length = length;
115     this.name_index = name_index;
116     this.signature_index = signature_index;
117     this.index = index;
118     this.constant_pool = constant_pool;
119   }
120
121   /**
122    * Called by objects that are traversing the nodes of the tree implicitely
123    * defined by the contents of a Java class. I.e., the hierarchy of methods,
124    * fields, attributes, etc. spawns a tree of objects.
125    *
126    * @param v Visitor object
127    */

128   public void accept(Visitor v) {
129     v.visitLocalVariable(this);
130   }
131
132   /**
133    * Dump local variable to file stream in binary format.
134    *
135    * @param file Output file stream
136    * @throw IOException
137    */

138   public final void dump(DataOutputStream file) throws IOException
139   {
140     file.writeShort(start_pc);
141     file.writeShort(length);
142     file.writeShort(name_index);
143     file.writeShort(signature_index);
144     file.writeShort(index);
145   }
146
147   /**
148    * @return Constant pool used by this object.
149    */

150   public final ConstantPool getConstantPool() { return constant_pool; }
151
152   /**
153    * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
154    */

155   public final int getLength() { return length; }
156
157   /**
158    * @return Variable name.
159    */

160   public final String JavaDoc getName() {
161     ConstantUtf8 c;
162
163     c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
164     return c.getBytes();
165   }
166
167   /**
168    * @return Index in constant pool of variable name.
169    */

170   public final int getNameIndex() { return name_index; }
171
172   /**
173    * @return Signature.
174    */

175   public final String JavaDoc getSignature() {
176     ConstantUtf8 c;
177     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
178                         CONSTANT_Utf8);
179     return c.getBytes();
180   }
181
182   /**
183    * @return Index in constant pool of variable signature.
184    */

185   public final int getSignatureIndex() { return signature_index; }
186
187   /**
188    * @return index of register where variable is stored
189    */

190   public final int getIndex() { return index; }
191
192   /**
193    * @return Start of range where he variable is valid
194    */

195   public final int getStartPC() { return start_pc; }
196
197   /**
198    * @param constant_pool Constant pool to be used for this object.
199    */

200   public final void setConstantPool(ConstantPool constant_pool) {
201     this.constant_pool = constant_pool;
202   }
203
204   /**
205    * @param length.
206    */

207   public final void setLength(int length) {
208     this.length = length;
209   }
210
211   /**
212    * @param name_index.
213    */

214   public final void setNameIndex(int name_index) {
215     this.name_index = name_index;
216   }
217
218   /**
219    * @param signature_index.
220    */

221   public final void setSignatureIndex(int signature_index) {
222     this.signature_index = signature_index;
223   }
224
225   /**
226    * @param index.
227    */

228   public final void setIndex(int index) { this.index = index; }
229
230   /**
231    * @param start_pc Specify range where the local variable is valid.
232    */

233   public final void setStartPC(int start_pc) {
234     this.start_pc = start_pc;
235   }
236
237   /**
238    * @return string representation.
239    */

240   public final String JavaDoc toString() {
241     String JavaDoc name = getName(), signature = Utility.signatureToString(getSignature());
242
243     return "LocalVariable(start_pc = " + start_pc + ", length = " + length +
244       ", index = " + index + ":" + signature + " " + name + ")";
245   }
246
247   /**
248    * @return deep copy of this object
249    */

250   public LocalVariable copy() {
251     try {
252       return (LocalVariable)clone();
253     } catch(CloneNotSupportedException JavaDoc e) {}
254
255     return null;
256   }
257 }
258
Popular Tags