1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 package com.sun.org.apache.bcel.internal.generic;
   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 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 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;
 100     this.type  = type;
 101     this.index  = index;
 102     setStart(start);
 103     setEnd(end);
 104   }
 105 
 106   /**
 107    * Get LocalVariable object.
 108    *
 109    * This relies on that the instruction list has already been dumped to byte code or
 110    * or that the `setPositions' methods has been called for the instruction list.
 111    *
 112    * Note that for local variables whose scope end at the last
 113    * instruction of the method's code, the JVM specification is ambiguous:
 114    * both a start_pc+length ending at the last instruction and
 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 void        setIndex(int index)         { this.index = index; }
 136   public int         getIndex()                  { return index; }
 137   @Override
 138   public void        setName(String name)        { this.name = name; }
 139   @Override
 140   public String      getName()                   { return name; }
 141   @Override
 142   public void        setType(Type type)          { this.type = type; }
 143   @Override
 144   public Type        getType()                   { return type; }
 145 
 146   public InstructionHandle getStart()                  { return start; }
 147   public InstructionHandle getEnd()                    { return end; }
 148 
 149   public void setStart(InstructionHandle start) {
 150     BranchInstruction.notifyTarget(this.start, start, this);
 151     this.start = start;
 152   }
 153 
 154   public void setEnd(InstructionHandle end) {
 155     BranchInstruction.notifyTarget(this.end, end, this);
 156     this.end = end;
 157   }
 158 
 159   /**
 160    * @param old_ih old target, either start or end
 161    * @param new_ih new target
 162    */
 163   @Override
 164   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
 165     boolean targeted = false;
 166 
 167     if(start == old_ih) {
 168       targeted = true;
 169       setStart(new_ih);
 170     }
 171 
 172     if(end == old_ih) {
 173       targeted = true;
 174       setEnd(new_ih);
 175     }
 176 
 177     if(!targeted)
 178       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
 179                                   end + "}");
 180   }
 181 
 182   /**
 183    * @return true, if ih is target of this variable
 184    */
 185   @Override
 186   public boolean containsTarget(InstructionHandle ih) {
 187     return (start == ih) || (end == ih);
 188   }
 189 
 190   /**
 191    * We consider to local variables to be equal, if the use the same index and
 192    * are valid in the same range.
 193    */
 194   @Override
 195   public boolean equals(Object o) {
 196     if(!(o instanceof LocalVariableGen))
 197       return false;
 198 
 199     LocalVariableGen l = (LocalVariableGen)o;
 200     return (l.index == index) && (l.start == start) && (l.end == end);
 201   }
 202 
 203   @Override
 204   public int hashCode() {
 205     int hash = 7;
 206     hash = 59 * hash + this.index;
 207     hash = 59 * hash + Objects.hashCode(this.start);
 208     hash = 59 * hash + Objects.hashCode(this.end);
 209     return hash;
 210   }
 211 
 212   @Override
 213   public String toString() {
 214     return "LocalVariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
 215   }
 216 
 217   @Override
 218   public Object clone() {
 219     try {
 220       return super.clone();
 221     } catch(CloneNotSupportedException e) {
 222       System.err.println(e);
 223       return null;
 224     }
 225   }
 226 }