1. package com.sun.org.apache.bcel.internal.generic;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import com.sun.org.apache.bcel.internal.Constants;
  56. import com.sun.org.apache.bcel.internal.classfile.*;
  57. /**
  58. * This class represents a local variable within a method. It contains its
  59. * scope, name and type. The generated LocalVariable object can be obtained
  60. * with getLocalVariable which needs the instruction list and the constant
  61. * pool as parameters.
  62. *
  63. * @version $Id: LocalVariableGen.java,v 1.1.1.1 2001/10/29 20:00:23 jvanzyl Exp $
  64. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  65. * @see LocalVariable
  66. * @see MethodGen
  67. */
  68. public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable {
  69. private int index;
  70. private String name;
  71. private Type type;
  72. private InstructionHandle start, end;
  73. /**
  74. * Generate a local variable that with index `index'. Note that double and long
  75. * variables need two indexs. Index indices have to be provided by the user.
  76. *
  77. * @param index index of local variable
  78. * @param name its name
  79. * @param type its type
  80. * @param start from where the instruction is valid (null means from the start)
  81. * @param end until where the instruction is valid (null means to the end)
  82. */
  83. public LocalVariableGen(int index, String name, Type type,
  84. InstructionHandle start, InstructionHandle end) {
  85. if((index < 0) || (index > Constants.MAX_SHORT))
  86. throw new ClassGenException("Invalid index index: " + index);
  87. this.name = name;
  88. this.type = type;
  89. this.index = index;
  90. setStart(start);
  91. setEnd(end);
  92. }
  93. /**
  94. * Get LocalVariable object.
  95. *
  96. * This relies on that the instruction list has already been dumped to byte code or
  97. * or that the `setPositions' methods has been called for the instruction list.
  98. *
  99. * @param il instruction list (byte code) which this variable belongs to
  100. * @param cp constant pool
  101. */
  102. public LocalVariable getLocalVariable(ConstantPoolGen cp) {
  103. int start_pc = start.getPosition();
  104. int length = end.getPosition() - start_pc;
  105. int name_index = cp.addUtf8(name);
  106. int signature_index = cp.addUtf8(type.getSignature());
  107. return new LocalVariable(start_pc, length, name_index,
  108. signature_index, index, cp.getConstantPool());
  109. }
  110. public void setIndex(int index) { this.index = index; }
  111. public int getIndex() { return index; }
  112. public void setName(String name) { this.name = name; }
  113. public String getName() { return name; }
  114. public void setType(Type type) { this.type = type; }
  115. public Type getType() { return type; }
  116. public InstructionHandle getStart() { return start; }
  117. public InstructionHandle getEnd() { return end; }
  118. public void setStart(InstructionHandle start) {
  119. BranchInstruction.notifyTarget(this.start, start, this);
  120. this.start = start;
  121. }
  122. public void setEnd(InstructionHandle end) {
  123. BranchInstruction.notifyTarget(this.end, end, this);
  124. this.end = end;
  125. }
  126. /**
  127. * @param old_ih old target, either start or end
  128. * @param new_ih new target
  129. */
  130. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  131. boolean targeted = false;
  132. if(start == old_ih) {
  133. targeted = true;
  134. setStart(new_ih);
  135. }
  136. if(end == old_ih) {
  137. targeted = true;
  138. setEnd(new_ih);
  139. }
  140. if(!targeted)
  141. throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
  142. end + "}");
  143. }
  144. /**
  145. * @return true, if ih is target of this variable
  146. */
  147. public boolean containsTarget(InstructionHandle ih) {
  148. return (start == ih) || (end == ih);
  149. }
  150. /**
  151. * We consider to local variables to be equal, if the use the same index and
  152. * are valid in the same range.
  153. */
  154. public boolean equals(Object o) {
  155. if(!(o instanceof LocalVariableGen))
  156. return false;
  157. LocalVariableGen l = (LocalVariableGen)o;
  158. return (l.index == index) && (l.start == start) && (l.end == end);
  159. }
  160. public String toString() {
  161. return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")";
  162. }
  163. public Object clone() {
  164. try {
  165. return super.clone();
  166. } catch(CloneNotSupportedException e) {
  167. System.err.println(e);
  168. return null;
  169. }
  170. }
  171. }