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 


 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 int         index;
  80   private String      name;
  81   private Type        type;
  82   private InstructionHandle start, end;
  83 


 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 }