jaxp/src/com/sun/org/apache/bcel/internal/generic/LocalVariableGen.java

Print this page




  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 com.sun.org.apache.bcel.internal.classfile.*;

  63 
  64 /**
  65  * This class represents a local variable within a method. It contains its
  66  * scope, name and type. The generated LocalVariable object can be obtained
  67  * with getLocalVariable which needs the instruction list and the constant
  68  * pool as parameters.
  69  *
  70  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  71  * @see     LocalVariable
  72  * @see     MethodGen
  73  */
  74 public class LocalVariableGen
  75   implements InstructionTargeter, NamedAndTyped, Cloneable,
  76              java.io.Serializable
  77 {
  78   private int         index;
  79   private String      name;
  80   private Type        type;
  81   private InstructionHandle start, end;
  82 
  83   /**
  84    * Generate a local variable that with index `index'. Note that double and long
  85    * variables need two indexs. Index indices have to be provided by the user.
  86    *
  87    * @param index index of local variable
  88    * @param name its name
  89    * @param type its type
  90    * @param start from where the instruction is valid (null means from the start)
  91    * @param end until where the instruction is valid (null means to the end)
  92    */
  93   public LocalVariableGen(int index, String name, Type type,
  94                           InstructionHandle start, InstructionHandle end) {
  95     if((index < 0) || (index > Constants.MAX_SHORT))
  96       throw new ClassGenException("Invalid index index: " + index);
  97 
  98     this.name  = name;


 114    * start_pc+length ending at first index beyond the end of the code are
 115    * valid.
 116    *
 117    * @param il instruction list (byte code) which this variable belongs to
 118    * @param cp constant pool
 119    */
 120   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
 121     int start_pc        = start.getPosition();
 122     int length          = end.getPosition() - start_pc;
 123 
 124     if(length > 0)
 125       length += end.getInstruction().getLength();
 126 
 127     int name_index      = cp.addUtf8(name);
 128     int signature_index = cp.addUtf8(type.getSignature());
 129 
 130     return new LocalVariable(start_pc, length, name_index,
 131                              signature_index, index, cp.getConstantPool());
 132   }
 133 
 134   public void        setIndex(int index)           { this.index = index; }
 135   public int         getIndex()                   { return index; }

 136   public void        setName(String name)        { this.name = name; }

 137   public String      getName()                   { return name; }

 138   public void        setType(Type type)          { this.type = type; }

 139   public Type        getType()                   { return type; }
 140 
 141   public InstructionHandle getStart()                  { return start; }
 142   public InstructionHandle getEnd()                    { return end; }
 143 
 144   public void setStart(InstructionHandle start) {
 145     BranchInstruction.notifyTarget(this.start, start, this);













































 146     this.start = start;





 147   }
 148 
 149   public void setEnd(InstructionHandle end) {
 150     BranchInstruction.notifyTarget(this.end, end, this);





 151     this.end = end;







 152   }
 153 
 154   /**
 155    * @param old_ih old target, either start or end
 156    * @param new_ih new target
 157    */

 158   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
 159     boolean targeted = false;
 160 
 161     if(start == old_ih) {
 162       targeted = true;
 163       setStart(new_ih);
 164     }
 165 
 166     if(end == old_ih) {
 167       targeted = true;
 168       setEnd(new_ih);
 169     }
 170 
 171     if(!targeted)
 172       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
 173                                   end + "}");
 174   }
 175 
 176   /**
 177    * @return true, if ih is target of this variable
 178    */

 179   public boolean containsTarget(InstructionHandle ih) {
 180     return (start == ih) || (end == ih);
 181   }
 182 
 183   /**
 184    * We consider to local variables to be equal, if the use the same index and
 185    * are valid in the same range.
 186    */

 187   public boolean equals(Object o) {



 188     if(!(o instanceof LocalVariableGen))
 189       return false;
 190 
 191     LocalVariableGen l = (LocalVariableGen)o;
 192     return (l.index == index) && (l.start == start) && (l.end == end);
 193   }
 194 










 195   public String toString() {
 196     return "LocalVariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
 197   }
 198 

 199   public Object clone() {
 200     try {
 201       return super.clone();
 202     } catch(CloneNotSupportedException e) {
 203       System.err.println(e);
 204       return null;
 205     }
 206   }
 207 }


  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 com.sun.org.apache.bcel.internal.classfile.*;
  63 import java.util.Objects;
  64 
  65 /**
  66  * This class represents a local variable within a method. It contains its
  67  * scope, name and type. The generated LocalVariable object can be obtained
  68  * with getLocalVariable which needs the instruction list and the constant
  69  * pool as parameters.
  70  *
  71  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  72  * @see     LocalVariable
  73  * @see     MethodGen
  74  */
  75 public class LocalVariableGen
  76   implements InstructionTargeter, NamedAndTyped, Cloneable,
  77              java.io.Serializable
  78 {
  79   private final int   index;
  80   private String      name;
  81   private Type        type;
  82   private InstructionHandle start, end;
  83 
  84   /**
  85    * Generate a local variable that with index `index'. Note that double and long
  86    * variables need two indexs. Index indices have to be provided by the user.
  87    *
  88    * @param index index of local variable
  89    * @param name its name
  90    * @param type its type
  91    * @param start from where the instruction is valid (null means from the start)
  92    * @param end until where the instruction is valid (null means to the end)
  93    */
  94   public LocalVariableGen(int index, String name, Type type,
  95                           InstructionHandle start, InstructionHandle end) {
  96     if((index < 0) || (index > Constants.MAX_SHORT))
  97       throw new ClassGenException("Invalid index index: " + index);
  98 
  99     this.name  = name;


 115    * start_pc+length ending at first index beyond the end of the code are
 116    * valid.
 117    *
 118    * @param il instruction list (byte code) which this variable belongs to
 119    * @param cp constant pool
 120    */
 121   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
 122     int start_pc        = start.getPosition();
 123     int length          = end.getPosition() - start_pc;
 124 
 125     if(length > 0)
 126       length += end.getInstruction().getLength();
 127 
 128     int name_index      = cp.addUtf8(name);
 129     int signature_index = cp.addUtf8(type.getSignature());
 130 
 131     return new LocalVariable(start_pc, length, name_index,
 132                              signature_index, index, cp.getConstantPool());
 133   }
 134 

 135   public int         getIndex()                  { return index; }
 136   @Override
 137   public void        setName(String name)        { this.name = name; }
 138   @Override
 139   public String      getName()                   { return name; }
 140   @Override
 141   public void        setType(Type type)          { this.type = type; }
 142   @Override
 143   public Type        getType()                   { return type; }
 144 
 145   public InstructionHandle getStart()                  { return start; }
 146   public InstructionHandle getEnd()                    { return end; }
 147 
 148   /**
 149    * Remove this from any known HashSet in which it might be registered.
 150    */
 151   void notifyTargetChanging() {
 152     // hashCode depends on 'index', 'start', and 'end'.
 153     // Therefore before changing any of these values we
 154     // need to unregister 'this' from any HashSet where
 155     // this is registered, and then we need to add it
 156     // back...
 157 
 158     // Unregister 'this' from the HashSet held by 'start'.
 159     BranchInstruction.notifyTargetChanging(this.start, this);
 160     if (this.end != this.start) {
 161         // Since hashCode() is going to change we need to unregister
 162         // 'this' both form 'start' and 'end'.
 163         // Unregister 'this' from the HashSet held by 'end'.
 164         BranchInstruction.notifyTargetChanging(this.end, this);
 165     }
 166   }
 167 
 168   /**
 169    * Add back 'this' in all HashSet in which it should be registered.
 170    **/
 171   void notifyTargetChanged() {
 172     // hashCode depends on 'index', 'start', and 'end'.
 173     // Therefore before changing any of these values we
 174     // need to unregister 'this' from any HashSet where
 175     // this is registered, and then we need to add it
 176     // back...
 177 
 178     // Register 'this' in the HashSet held by start.
 179     BranchInstruction.notifyTargetChanged(this.start, this);
 180     if (this.end != this.start) {
 181         // Since hashCode() has changed we need to register
 182         // 'this' again in 'end'.
 183         // Add back 'this' in the HashSet held by 'end'.
 184         BranchInstruction.notifyTargetChanged(this.end, this);
 185     }
 186   }
 187 
 188   public final void setStart(InstructionHandle start) {
 189 
 190     // Call notifyTargetChanging *before* modifying this,
 191     // as the code triggered by notifyTargetChanging
 192     // depends on this pointing to the 'old' start.
 193     notifyTargetChanging();
 194 
 195     this.start = start;
 196 
 197     // call notifyTargetChanged *after* modifying this,
 198     // as the code triggered by notifyTargetChanged
 199     // depends on this pointing to the 'new' start.
 200     notifyTargetChanged();
 201   }
 202 
 203   public final void setEnd(InstructionHandle end) {
 204     // call notifyTargetChanging *before* modifying this,
 205     // as the code triggered by notifyTargetChanging
 206     // depends on this pointing to the 'old' end.
 207     // Unregister 'this' from the HashSet held by the 'old' end.
 208     notifyTargetChanging();
 209 
 210     this.end = end;
 211 
 212     // call notifyTargetChanged *after* modifying this,
 213     // as the code triggered by notifyTargetChanged
 214     // depends on this pointing to the 'new' end.
 215     // Register 'this' in the HashSet held by the 'new' end.
 216     notifyTargetChanged();
 217 
 218   }
 219 
 220   /**
 221    * @param old_ih old target, either start or end
 222    * @param new_ih new target
 223    */
 224   @Override
 225   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
 226     boolean targeted = false;
 227 
 228     if(start == old_ih) {
 229       targeted = true;
 230       setStart(new_ih);
 231     }
 232 
 233     if(end == old_ih) {
 234       targeted = true;
 235       setEnd(new_ih);
 236     }
 237 
 238     if(!targeted)
 239       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
 240                                   end + "}");
 241   }
 242 
 243   /**
 244    * @return true, if ih is target of this variable
 245    */
 246   @Override
 247   public boolean containsTarget(InstructionHandle ih) {
 248     return (start == ih) || (end == ih);
 249   }
 250 
 251   /**
 252    * We consider two local variables to be equal, if they use the same index and
 253    * are valid in the same range.
 254    */
 255   @Override
 256   public boolean equals(Object o) {
 257     if (o==this)
 258       return true;
 259 
 260     if(!(o instanceof LocalVariableGen))
 261       return false;
 262 
 263     LocalVariableGen l = (LocalVariableGen)o;
 264     return (l.index == index) && (l.start == start) && (l.end == end);
 265   }
 266 
 267   @Override
 268   public int hashCode() {
 269     int hash = 7;
 270     hash = 59 * hash + this.index;
 271     hash = 59 * hash + Objects.hashCode(this.start);
 272     hash = 59 * hash + Objects.hashCode(this.end);
 273     return hash;
 274   }
 275 
 276   @Override
 277   public String toString() {
 278     return "LocalVariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
 279   }
 280 
 281   @Override
 282   public Object clone() {
 283     try {
 284       return super.clone();
 285     } catch(CloneNotSupportedException e) {
 286       System.err.println(e);
 287       return null;
 288     }
 289   }
 290 }