View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   package com.sun.org.apache.bcel.internal.classfile;
6   
7   /* ====================================================================
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2001 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution,
26   *    if any, must include the following acknowledgment:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowledgment may appear in the software itself,
30   *    if and wherever such third-party acknowledgments normally appear.
31   *
32   * 4. The names "Apache" and "Apache Software Foundation" and
33   *    "Apache BCEL" must not be used to endorse or promote products
34   *    derived from this software without prior written permission. For
35   *    written permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache",
38   *    "Apache BCEL", nor may "Apache" appear in their name, without
39   *    prior written permission of the Apache Software Foundation.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   */
60  
61  import  com.sun.org.apache.bcel.internal.Constants;
62  import  java.io.*;
63  
64  /**
65   * This class represents a local variable within a method. It contains its
66   * scope, name, signature and index on the method's frame.
67   *
68   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
69   * @see     LocalVariableTable
70   */
71  public final class LocalVariable
72    implements Constants, Cloneable, Node, Serializable
73  {
74    private int start_pc;        // Range in which the variable is valid
75    private int length;
76    private int name_index;      // Index in constant pool of variable name
77    private int signature_index; // Index of variable signature
78    private int index;            /* Variable is `index'th local variable on
79                                  * this method's frame.
80                                  */
81  
82    private ConstantPool constant_pool;
83  
84    /**
85     * Initialize from another object. Note that both objects use the same
86     * references (shallow copy). Use copy() for a physical copy.
87     */
88    public LocalVariable(LocalVariable c) {
89      this(c.getStartPC(), c.getLength(), c.getNameIndex(),
90           c.getSignatureIndex(), c.getIndex(), c.getConstantPool());
91    }
92  
93    /**
94     * Construct object from file stream.
95     * @param file Input stream
96     * @throws IOException
97     */
98    LocalVariable(DataInputStream file, ConstantPool constant_pool)
99         throws IOException
100   {
101     this(file.readUnsignedShort(), file.readUnsignedShort(),
102          file.readUnsignedShort(), file.readUnsignedShort(),
103          file.readUnsignedShort(), constant_pool);
104   }
105 
106   /**
107    * @param start_pc Range in which the variable
108    * @param length ... is valid
109    * @param name_index Index in constant pool of variable name
110    * @param signature_index Index of variable's signature
111    * @param index Variable is `index'th local variable on the method's frame
112    * @param constant_pool Array of constants
113    */
114   public LocalVariable(int start_pc, int length, int name_index,
115                        int signature_index, int index,
116                        ConstantPool constant_pool)
117   {
118     this.start_pc        = start_pc;
119     this.length          = length;
120     this.name_index      = name_index;
121     this.signature_index = signature_index;
122     this.index           = index;
123     this.constant_pool   = constant_pool;
124   }
125 
126   /**
127    * Called by objects that are traversing the nodes of the tree implicitely
128    * defined by the contents of a Java class. I.e., the hierarchy of methods,
129    * fields, attributes, etc. spawns a tree of objects.
130    *
131    * @param v Visitor object
132    */
133   public void accept(Visitor v) {
134     v.visitLocalVariable(this);
135   }
136 
137   /**
138    * Dump local variable to file stream in binary format.
139    *
140    * @param file Output file stream
141    * @throws IOException
142    */
143   public final void dump(DataOutputStream file) throws IOException
144   {
145     file.writeShort(start_pc);
146     file.writeShort(length);
147     file.writeShort(name_index);
148     file.writeShort(signature_index);
149     file.writeShort(index);
150   }
151 
152   /**
153    * @return Constant pool used by this object.
154    */
155   public final ConstantPool getConstantPool() { return constant_pool; }
156 
157   /**
158    * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
159    */
160   public final int getLength()         { return length; }
161 
162   /**
163    * @return Variable name.
164    */
165   public final String getName() {
166     ConstantUtf8  c;
167 
168     c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
169     return c.getBytes();
170   }
171 
172   /**
173    * @return Index in constant pool of variable name.
174    */
175   public final int getNameIndex()      { return name_index; }
176 
177   /**
178    * @return Signature.
179    */
180   public final String getSignature() {
181     ConstantUtf8  c;
182     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
183                                                 CONSTANT_Utf8);
184     return c.getBytes();
185   }
186 
187   /**
188    * @return Index in constant pool of variable signature.
189    */
190   public final int getSignatureIndex() { return signature_index; }
191 
192   /**
193    * @return index of register where variable is stored
194    */
195   public final int getIndex()           { return index; }
196 
197   /**
198    * @return Start of range where he variable is valid
199    */
200   public final int getStartPC()        { return start_pc; }
201 
202   /**
203    * @param constant_pool Constant pool to be used for this object.
204    */
205   public final void setConstantPool(ConstantPool constant_pool) {
206     this.constant_pool = constant_pool;
207   }
208 
209   /**
210    * @param length.
211    */
212   public final void setLength(int length) {
213     this.length = length;
214   }
215 
216   /**
217    * @param name_index.
218    */
219   public final void setNameIndex(int name_index) {
220     this.name_index = name_index;
221   }
222 
223   /**
224    * @param signature_index.
225    */
226   public final void setSignatureIndex(int signature_index) {
227     this.signature_index = signature_index;
228   }
229 
230   /**
231    * @param index.
232    */
233   public final void setIndex(int index) { this.index = index; }
234 
235   /**
236    * @param start_pc Specify range where the local variable is valid.
237    */
238   public final void setStartPC(int start_pc) {
239     this.start_pc = start_pc;
240   }
241 
242   /**
243    * @return string representation.
244    */
245   public final String toString() {
246     String name = getName(), signature = Utility.signatureToString(getSignature());
247 
248     return "LocalVariable(start_pc = " + start_pc + ", length = " + length +
249       ", index = " + index + ":" + signature + " " + name + ")";
250   }
251 
252   /**
253    * @return deep copy of this object
254    */
255   public LocalVariable copy() {
256     try {
257       return (LocalVariable)clone();
258     } catch(CloneNotSupportedException e) {}
259 
260     return null;
261   }
262 }