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.

LocalVariableGen.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package org.aspectj.apache.bcel.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 (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 org.aspectj.apache.bcel.Constants;
  56. import org.aspectj.apache.bcel.classfile.ConstantPool;
  57. import org.aspectj.apache.bcel.classfile.LocalVariable;
  58. /**
  59. * This class represents a local variable within a method. It contains its scope, name and type. The generated LocalVariable object
  60. * can be obtained with getLocalVariable which needs the instruction list and the constant pool as parameters.
  61. *
  62. * @version $Id: LocalVariableGen.java,v 1.7 2008/08/28 00:04:23 aclement Exp $
  63. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  64. * @see LocalVariable
  65. * @see MethodGen
  66. */
  67. public class LocalVariableGen implements InstructionTargeter, Cloneable, java.io.Serializable {
  68. private int index;
  69. private String name;
  70. private Type type;
  71. private InstructionHandle start, end;
  72. /**
  73. * Generate a local variable that with index `index'. Note that double and long variables need two indexs. Index indices have to
  74. * be provided by the user.
  75. *
  76. * @param index index of local variable
  77. * @param name its name
  78. * @param type its type
  79. * @param start from where the instruction is valid (null means from the start)
  80. * @param end until where the instruction is valid (null means to the end)
  81. */
  82. public LocalVariableGen(int index, String name, Type type, InstructionHandle start, InstructionHandle end) {
  83. if (index < 0 || index > Constants.MAX_SHORT) {
  84. throw new ClassGenException("Invalid index index: " + index);
  85. }
  86. this.name = name;
  87. this.type = type;
  88. this.index = index;
  89. setStart(start);
  90. setEnd(end);
  91. }
  92. /**
  93. * Get LocalVariable object.
  94. *
  95. * This relies on that the instruction list has already been dumped to byte code or or that the `setPositions' methods has been
  96. * called for the instruction list.
  97. *
  98. * Note that for local variables whose scope end at the last instruction of the method's code, the JVM specification is
  99. * ambiguous: both a start_pc+length ending at the last instruction and start_pc+length ending at first index beyond the end of
  100. * the code are valid.
  101. *
  102. * @param il instruction list (byte code) which this variable belongs to
  103. * @param cp constant pool
  104. */
  105. public LocalVariable getLocalVariable(ConstantPool cp) {
  106. int start_pc = start.getPosition();
  107. int length = end.getPosition() - start_pc;
  108. if (length > 0) {
  109. length += end.getInstruction().getLength();
  110. }
  111. int name_index = cp.addUtf8(name);
  112. int signature_index = cp.addUtf8(type.getSignature());
  113. return new LocalVariable(start_pc, length, name_index, signature_index, index, cp);
  114. }
  115. public void setIndex(int index) {
  116. this.index = index;
  117. }
  118. public int getIndex() {
  119. return index;
  120. }
  121. public void setName(String name) {
  122. this.name = name;
  123. }
  124. public String getName() {
  125. return name;
  126. }
  127. public void setType(Type type) {
  128. this.type = type;
  129. }
  130. public Type getType() {
  131. return type;
  132. }
  133. public InstructionHandle getStart() {
  134. return start;
  135. }
  136. public InstructionHandle getEnd() {
  137. return end;
  138. }
  139. public void setStart(InstructionHandle start) {
  140. InstructionBranch.notifyTarget(this.start, start, this);
  141. this.start = start;
  142. }
  143. public void setEnd(InstructionHandle end) {
  144. InstructionBranch.notifyTarget(this.end, end, this);
  145. this.end = end;
  146. }
  147. /**
  148. * @param old_ih old target, either start or end
  149. * @param new_ih new target
  150. */
  151. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  152. boolean targeted = false;
  153. if (start == old_ih) {
  154. targeted = true;
  155. setStart(new_ih);
  156. }
  157. if (end == old_ih) {
  158. targeted = true;
  159. setEnd(new_ih);
  160. }
  161. if (!targeted) {
  162. throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " + end + "}");
  163. }
  164. }
  165. /**
  166. * @return true, if ih is target of this variable
  167. */
  168. public boolean containsTarget(InstructionHandle ih) {
  169. return start == ih || end == ih;
  170. }
  171. /**
  172. * We consider to local variables to be equal, if the use the same index and are valid in the same range.
  173. */
  174. public boolean equals(Object o) {
  175. if (!(o instanceof LocalVariableGen)) {
  176. return false;
  177. }
  178. LocalVariableGen l = (LocalVariableGen) o;
  179. return l.index == index && l.start == start && l.end == end;
  180. }
  181. public String toString() {
  182. return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")";
  183. }
  184. public Object clone() {
  185. try {
  186. return super.clone();
  187. } catch (CloneNotSupportedException e) {
  188. System.err.println(e);
  189. return null;
  190. }
  191. }
  192. }