Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

LocalVariable.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import  de.fub.bytecode.Constants;
00004 import  java.io.*;
00005 
00006 /**
00007  * This class represents a local variable within a method. It contains its
00008  * scope, name, signature and index on the method's frame.
00009  *
00010  * @version $Id: LocalVariable.java,v 1.1.1.1 2002/01/24 03:41:37 pserver Exp $
00011  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00012  * @see     LocalVariableTable
00013  */
00014 public final class LocalVariable implements Constants, Cloneable {
00015   private int start_pc;        // Range in which the variable is valid
00016   private int length;
00017   private int name_index;      // Index in constant pool of variable name
00018   private int signature_index; // Index of variable signature
00019   private int index;            /* Variable is `index'th local variable on
00020                 * this method's frame.
00021                 */
00022 
00023   private ConstantPool constant_pool;
00024 
00025   /**
00026    * @param start_pc Range in which the variable
00027    * @param length ... is valid
00028    * @param name_index Index in constant pool of variable name
00029    * @param signature_index Index of variable's signature
00030    * @param index Variable is `index'th local variable on the method's frame
00031    * @param constant_pool Array of constants
00032    */
00033   public LocalVariable(int start_pc, int length, int name_index,
00034                int signature_index, int index,
00035                ConstantPool constant_pool)
00036   {
00037     this.start_pc        = start_pc;
00038     this.length          = length;
00039     this.name_index      = name_index;
00040     this.signature_index = signature_index;
00041     this.index            = index;
00042     this.constant_pool   = constant_pool;
00043   }  
00044   /**
00045    * Initialize from another object. Note that both objects use the same
00046    * references (shallow copy). Use clone() for a physical copy.
00047    */
00048   public LocalVariable(LocalVariable c) {
00049     this(c.getStartPC(), c.getLength(), c.getNameIndex(),
00050      c.getSignatureIndex(), c.getIndex(), c.getConstantPool());
00051   }  
00052   /**
00053    * Construct object from file stream.
00054    * @param file Input stream
00055    * @throw IOException
00056    */
00057   LocalVariable(DataInputStream file, ConstantPool constant_pool)
00058        throws IOException
00059   {
00060     this(file.readUnsignedShort(), file.readUnsignedShort(), 
00061      file.readUnsignedShort(), file.readUnsignedShort(), 
00062      file.readUnsignedShort(), constant_pool);
00063   }  
00064   /**
00065    * Called by objects that are traversing the nodes of the tree implicitely
00066    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00067    * fields, attributes, etc. spawns a tree of objects.
00068    *
00069    * @param v Visitor object
00070    */
00071   public void accept(Visitor v) {
00072     v.visitLocalVariable(this);
00073   }  
00074   /**
00075    * @return deep copy of this object
00076    */
00077   public LocalVariable copy() {
00078     try {
00079       return (LocalVariable)clone();
00080     } catch(CloneNotSupportedException e) {}
00081 
00082     return null;
00083   }  
00084   /**
00085    * Dump local variable to file stream in binary format.
00086    *
00087    * @param file Output file stream
00088    * @throw IOException
00089    */ 
00090   public final void dump(DataOutputStream file) throws IOException
00091   {
00092     file.writeShort(start_pc);
00093     file.writeShort(length);
00094     file.writeShort(name_index);
00095     file.writeShort(signature_index);
00096     file.writeShort(index);
00097   }  
00098   /**
00099    * @return Constant pool used by this object.
00100    * @see ConstantPool
00101    */   
00102   public final ConstantPool getConstantPool() { return constant_pool; }  
00103   /**
00104    * @return Variable is `getIndex()'th local variable on this method's frame.
00105    */   
00106   public final int getIndex()           { return index; }  
00107   /**
00108    * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
00109    */  
00110   public final int getLength()         { return length; }  
00111   /**
00112    * @return Variable name.
00113    */   
00114   public final String getName() {
00115     ConstantUtf8  c;
00116 
00117     c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
00118     return c.getBytes();
00119   }  
00120   /**
00121    * @return Index in constant pool of variable name.
00122    */   
00123   public final int getNameIndex()      { return name_index; }  
00124   /**
00125    * @return Signature.
00126    */   
00127   public final String getSignature() {
00128     ConstantUtf8  c;
00129     c = (ConstantUtf8)constant_pool.getConstant(signature_index, 
00130                         CONSTANT_Utf8);
00131     return c.getBytes();
00132   }  
00133   /**
00134    * @return Index in constant pool of variable signature.
00135    */   
00136   public final int getSignatureIndex() { return signature_index; }  
00137   /**
00138    * @return Start of range where he variable is valid
00139    */  
00140   public final int getStartPC()        { return start_pc; }  
00141   /**
00142    * @param constant_pool Constant pool to be used for this object.
00143    * @see ConstantPool
00144    */   
00145   public final void setConstantPool(ConstantPool constant_pool) {
00146     this.constant_pool = constant_pool;
00147   }  
00148   /**
00149    * @param index.
00150    */
00151   public final void setIndex(int index) { this.index = index; }  
00152   /**
00153    * @param length.
00154    */
00155   public final void setLength(int length) {
00156     this.length = length;
00157   }  
00158   /**
00159    * @param name_index.
00160    */
00161   public final void setNameIndex(int name_index) {
00162     this.name_index = name_index;
00163   }  
00164   /**
00165    * @param signature_index.
00166    */
00167   public final void setSignatureIndex(int signature_index) {
00168     this.signature_index = signature_index;
00169   }  
00170   /**
00171    * @param start_pc Specify range where the local variable is valid.
00172    */
00173   public final void setStartPC(int start_pc) {
00174     this.start_pc = start_pc;
00175   }  
00176   /**
00177    * @return string representation.
00178    */
00179   public final String toString() {
00180     String name = getName(), signature = Utility.signatureToString(getSignature());
00181 
00182     return "LocalVariable(start_pc = " + start_pc + ", length = " + length +
00183       ", index = " + index + ":" + signature + " " + name + ")";
00184   }  
00185 }

Generated at Thu Feb 7 06:49:47 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001