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:44:02 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 Constants, InstructionTargeter {
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 > 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     setStart(end);
00043   }  
00044   /**
00045    * @return true, if ih is target of this variable
00046    */
00047   public boolean containsTarget(InstructionHandle ih) {
00048     return (start == ih) || (end == ih);
00049   }  
00050   /**
00051    * We consider to local variables to be equal, if the use the same index and
00052    * are valid in the same range.
00053    */
00054   public boolean equals(Object o) {
00055     if(!(o instanceof LocalVariableGen))
00056       return false;
00057 
00058     LocalVariableGen l = (LocalVariableGen)o;
00059     return (l.index == index) && (l.start == start) && (l.end == end);
00060   }  
00061   public InstructionHandle getEnd()                    { return end; }  
00062   public int         getIndex()                   { return index; }  
00063   /**
00064    * Get LocalVariable object.
00065    *
00066    * This relies on that the instruction list has already been dumped to byte code or
00067    * or that the `setPositions' methods has been called for the instruction list.
00068    *
00069    * @param il instruction list (byte code) which this variable belongs to
00070    * @param cp constant pool
00071    */
00072   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
00073     int start_pc        = start.getPosition();
00074     int length          = end.getPosition() - start_pc;
00075     int name_index      = cp.addUtf8(name);
00076     int signature_index = cp.addUtf8(type.getSignature());
00077 
00078     return new LocalVariable(start_pc, length, name_index,
00079                  signature_index, index, cp.getConstantPool());
00080   }  
00081   public String      getName()                   { return name; }  
00082   /** @deprecated
00083    */
00084   public int         getSlot()                   { return index; }  
00085   public InstructionHandle getStart()                  { return start; }  
00086   public Type        getType()                   { return type; }  
00087   public void setEnd(InstructionHandle end) {
00088     BranchInstruction.notifyTarget(this.end, end, this);
00089     this.end = end;
00090   }  
00091   public void        setIndex(int index)           { this.index = index; }  
00092   public void        setName(String name)        { this.name = name; }  
00093   /** @deprecated
00094    */
00095   public void        setSlot(int index)           { this.index = index; }  
00096   public void setStart(InstructionHandle start) {
00097     BranchInstruction.notifyTarget(this.start, start, this);
00098     this.start = start;
00099   }  
00100   public void        setType(Type type)          { this.type = type; }  
00101   public String toString() {
00102     return "LocalvariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
00103   }  
00104   /**
00105    * @param old_ih old target, either start or end
00106    * @param new_ih new target
00107    */
00108   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
00109     boolean targeted = false;
00110 
00111     if(start == old_ih) {
00112       targeted = true;
00113       setStart(new_ih);
00114     }
00115 
00116     if(end == old_ih) {
00117       targeted = true;
00118       setEnd(new_ih);
00119     }
00120 
00121     if(!targeted)
00122       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
00123                   end + "}");
00124   }  
00125 }

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