You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocalVariable.java 7.8KB

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