1. package com.sun.org.apache.bcel.internal.classfile;
  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 java.io.*;
  57. /**
  58. * This class represents a local variable within a method. It contains its
  59. * scope, name, signature and index on the method's frame.
  60. *
  61. * @version $Id: LocalVariable.java,v 1.1.1.1 2001/10/29 20:00:02 jvanzyl Exp $
  62. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  63. * @see LocalVariableTable
  64. */
  65. public final class LocalVariable implements Constants, Cloneable, Node {
  66. private int start_pc; // Range in which the variable is valid
  67. private int length;
  68. private int name_index; // Index in constant pool of variable name
  69. private int signature_index; // Index of variable signature
  70. private int index; /* Variable is `index'th local variable on
  71. * this method's frame.
  72. */
  73. private ConstantPool constant_pool;
  74. /**
  75. * Initialize from another object. Note that both objects use the same
  76. * references (shallow copy). Use copy() for a physical copy.
  77. */
  78. public LocalVariable(LocalVariable c) {
  79. this(c.getStartPC(), c.getLength(), c.getNameIndex(),
  80. c.getSignatureIndex(), c.getIndex(), c.getConstantPool());
  81. }
  82. /**
  83. * Construct object from file stream.
  84. * @param file Input stream
  85. * @throw IOException
  86. */
  87. LocalVariable(DataInputStream file, ConstantPool constant_pool)
  88. throws IOException
  89. {
  90. this(file.readUnsignedShort(), file.readUnsignedShort(),
  91. file.readUnsignedShort(), file.readUnsignedShort(),
  92. file.readUnsignedShort(), constant_pool);
  93. }
  94. /**
  95. * @param start_pc Range in which the variable
  96. * @param length ... is valid
  97. * @param name_index Index in constant pool of variable name
  98. * @param signature_index Index of variable's signature
  99. * @param index Variable is `index'th local variable on the method's frame
  100. * @param constant_pool Array of constants
  101. */
  102. public LocalVariable(int start_pc, int length, int name_index,
  103. int signature_index, int index,
  104. ConstantPool constant_pool)
  105. {
  106. this.start_pc = start_pc;
  107. this.length = length;
  108. this.name_index = name_index;
  109. this.signature_index = signature_index;
  110. this.index = index;
  111. this.constant_pool = constant_pool;
  112. }
  113. /**
  114. * Called by objects that are traversing the nodes of the tree implicitely
  115. * defined by the contents of a Java class. I.e., the hierarchy of methods,
  116. * fields, attributes, etc. spawns a tree of objects.
  117. *
  118. * @param v Visitor object
  119. */
  120. public void accept(Visitor v) {
  121. v.visitLocalVariable(this);
  122. }
  123. /**
  124. * Dump local variable to file stream in binary format.
  125. *
  126. * @param file Output file stream
  127. * @throw IOException
  128. */
  129. public final void dump(DataOutputStream file) throws IOException
  130. {
  131. file.writeShort(start_pc);
  132. file.writeShort(length);
  133. file.writeShort(name_index);
  134. file.writeShort(signature_index);
  135. file.writeShort(index);
  136. }
  137. /**
  138. * @return Constant pool used by this object.
  139. */
  140. public final ConstantPool getConstantPool() { return constant_pool; }
  141. /**
  142. * @return Variable is valid within getStartPC() .. getStartPC()+getLength()
  143. */
  144. public final int getLength() { return length; }
  145. /**
  146. * @return Variable name.
  147. */
  148. public final String getName() {
  149. ConstantUtf8 c;
  150. c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8);
  151. return c.getBytes();
  152. }
  153. /**
  154. * @return Index in constant pool of variable name.
  155. */
  156. public final int getNameIndex() { return name_index; }
  157. /**
  158. * @return Signature.
  159. */
  160. public final String getSignature() {
  161. ConstantUtf8 c;
  162. c = (ConstantUtf8)constant_pool.getConstant(signature_index,
  163. CONSTANT_Utf8);
  164. return c.getBytes();
  165. }
  166. /**
  167. * @return Index in constant pool of variable signature.
  168. */
  169. public final int getSignatureIndex() { return signature_index; }
  170. /**
  171. * @return index of register where variable is stored
  172. */
  173. public final int getIndex() { return index; }
  174. /**
  175. * @return Start of range where he variable is valid
  176. */
  177. public final int getStartPC() { return start_pc; }
  178. /**
  179. * @param constant_pool Constant pool to be used for this object.
  180. */
  181. public final void setConstantPool(ConstantPool constant_pool) {
  182. this.constant_pool = constant_pool;
  183. }
  184. /**
  185. * @param length.
  186. */
  187. public final void setLength(int length) {
  188. this.length = length;
  189. }
  190. /**
  191. * @param name_index.
  192. */
  193. public final void setNameIndex(int name_index) {
  194. this.name_index = name_index;
  195. }
  196. /**
  197. * @param signature_index.
  198. */
  199. public final void setSignatureIndex(int signature_index) {
  200. this.signature_index = signature_index;
  201. }
  202. /**
  203. * @param index.
  204. */
  205. public final void setIndex(int index) { this.index = index; }
  206. /**
  207. * @param start_pc Specify range where the local variable is valid.
  208. */
  209. public final void setStartPC(int start_pc) {
  210. this.start_pc = start_pc;
  211. }
  212. /**
  213. * @return string representation.
  214. */
  215. public final String toString() {
  216. String name = getName(), signature = Utility.signatureToString(getSignature());
  217. return "LocalVariable(start_pc = " + start_pc + ", length = " + length +
  218. ", index = " + index + ":" + signature + " " + name + ")";
  219. }
  220. /**
  221. * @return deep copy of this object
  222. */
  223. public LocalVariable copy() {
  224. try {
  225. return (LocalVariable)clone();
  226. } catch(CloneNotSupportedException e) {}
  227. return null;
  228. }
  229. }