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

LocalVariableGen.java

00001 package de.fub.bytecode.generic;
00002 
00003 import de.fub.bytecode.Constants;
00004 import de.fub.bytecode.classfile.*;
00005 
00006 /** 
00007  * This class represents a local variable within a method. It contains its
00008  * scope, name and type. The generated LocalVariable object can be obtained
00009  * with getLocalVariable which needs the instruction list and the constant
00010  * pool as parameters.
00011  *
00012  * @version $Id: LocalVariableGen.java,v 1.1.1.1 2002/01/24 03:41:41 pserver Exp $
00013  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00014  * @see     LocalVariable
00015  * @see     MethodGen
00016  */
00017 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable {
00018   private int         index;
00019   private String      name;
00020   private Type        type;
00021   private InstructionHandle start, end;
00022 
00023   /**
00024    * Generate a local variable that with index `index'. Note that double and long
00025    * variables need two indexs. Index indices have to be provided by the user.
00026    *
00027    * @param index index of local variable
00028    * @param name its name
00029    * @param type its type
00030    * @param start from where the instruction is valid (null means from the start)
00031    * @param end until where the instruction is valid (null means to the end)
00032    */
00033   public LocalVariableGen(int index, String name, Type type,
00034               InstructionHandle start, InstructionHandle end) {
00035     if((index < 0) || (index > Constants.MAX_SHORT))
00036       throw new ClassGenException("Invalid index index: " + index);
00037     
00038     this.name  = name;
00039     this.type  = type;
00040     this.index  = index;
00041     setStart(start);
00042     setEnd(end);
00043   }  
00044   public Object clone() {
00045     try {
00046       return super.clone();
00047     } catch(CloneNotSupportedException e) {
00048       System.err.println(e);
00049       return null;
00050     }
00051   }  
00052   /**
00053    * @return true, if ih is target of this variable
00054    */
00055   public boolean containsTarget(InstructionHandle ih) {
00056     return (start == ih) || (end == ih);
00057   }  
00058   /**
00059    * We consider to local variables to be equal, if the use the same index and
00060    * are valid in the same range.
00061    */
00062   public boolean equals(Object o) {
00063     if(!(o instanceof LocalVariableGen))
00064       return false;
00065 
00066     LocalVariableGen l = (LocalVariableGen)o;
00067     return (l.index == index) && (l.start == start) && (l.end == end);
00068   }  
00069   public InstructionHandle getEnd()                    { return end; }  
00070   public int         getIndex()                   { return index; }  
00071   /**
00072    * Get LocalVariable object.
00073    *
00074    * This relies on that the instruction list has already been dumped to byte code or
00075    * or that the `setPositions' methods has been called for the instruction list.
00076    *
00077    * @param il instruction list (byte code) which this variable belongs to
00078    * @param cp constant pool
00079    */
00080   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
00081     int start_pc        = start.getPosition();
00082     int length          = end.getPosition() - start_pc;
00083     int name_index      = cp.addUtf8(name);
00084     int signature_index = cp.addUtf8(type.getSignature());
00085 
00086     return new LocalVariable(start_pc, length, name_index,
00087                  signature_index, index, cp.getConstantPool());
00088   }  
00089   public String      getName()                   { return name; }  
00090   /** @deprecated Use getIndex()
00091    */
00092   public int         getSlot()                   { return index; }  
00093   public InstructionHandle getStart()                  { return start; }  
00094   public Type        getType()                   { return type; }  
00095   public void setEnd(InstructionHandle end) {
00096     BranchInstruction.notifyTarget(this.end, end, this);
00097     this.end = end;
00098   }  
00099   public void        setIndex(int index)           { this.index = index; }  
00100   public void        setName(String name)        { this.name = name; }  
00101   /** @deprecated Use setIndex()
00102    */
00103   public void        setSlot(int index)           { this.index = index; }  
00104   public void setStart(InstructionHandle start) {
00105     BranchInstruction.notifyTarget(this.start, start, this);
00106     this.start = start;
00107   }  
00108   public void        setType(Type type)          { this.type = type; }  
00109   public String toString() {
00110     return "LocalVariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
00111   }  
00112   /**
00113    * @param old_ih old target, either start or end
00114    * @param new_ih new target
00115    */
00116   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
00117     boolean targeted = false;
00118 
00119     if(start == old_ih) {
00120       targeted = true;
00121       setStart(new_ih);
00122     }
00123 
00124     if(end == old_ih) {
00125       targeted = true;
00126       setEnd(new_ih);
00127     }
00128 
00129     if(!targeted)
00130       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
00131                   end + "}");
00132   }  
00133 }

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