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