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.

InstructionHandle.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 java.util.Collections;
  56. import java.util.HashSet;
  57. import java.util.Set;
  58. import org.aspectj.apache.bcel.classfile.Utility;
  59. /**
  60. * Instances of this class give users a handle to the instructions contained in an InstructionList. Instruction objects may be used
  61. * more than once within a list, this is useful because it saves memory and may be much faster.
  62. *
  63. * Within an InstructionList an InstructionHandle object is wrapped around all instructions, i.e., it implements a cell in a
  64. * doubly-linked list. From the outside only the next and the previous instruction (handle) are accessible. One can traverse the
  65. * list via an Enumeration returned by InstructionList.elements().
  66. *
  67. * @version $Id: InstructionHandle.java,v 1.9 2009/10/05 17:35:36 aclement Exp $
  68. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  69. * @see Instruction
  70. * @see BranchHandle
  71. * @see InstructionList
  72. */
  73. public class InstructionHandle implements java.io.Serializable {
  74. InstructionHandle next, prev; // Will be set from the outside
  75. Instruction instruction;
  76. protected int pos = -1; // byte code offset of instruction
  77. private Set<InstructionTargeter> targeters = Collections.emptySet();
  78. protected InstructionHandle(Instruction i) {
  79. setInstruction(i);
  80. }
  81. static final InstructionHandle getInstructionHandle(Instruction i) {
  82. return new InstructionHandle(i);
  83. }
  84. public final InstructionHandle getNext() {
  85. return next;
  86. }
  87. public final InstructionHandle getPrev() {
  88. return prev;
  89. }
  90. public final Instruction getInstruction() {
  91. return instruction;
  92. }
  93. /**
  94. * Replace current instruction contained in this handle. Old instruction is disposed using Instruction.dispose().
  95. */
  96. public void setInstruction(Instruction i) { // Overridden in BranchHandle
  97. if (instruction != null) {
  98. instruction.dispose();
  99. }
  100. instruction = i;
  101. }
  102. /**
  103. * @return the position, i.e., the byte code offset of the contained instruction. This is accurate only after
  104. * InstructionList.setPositions() has been called.
  105. */
  106. public int getPosition() {
  107. return pos;
  108. }
  109. /**
  110. * Set the position, i.e., the byte code offset of the contained instruction.
  111. */
  112. void setPosition(int pos) {
  113. this.pos = pos;
  114. }
  115. /**
  116. * Delete contents, i.e., remove user access and make handle reusable.
  117. */
  118. // OPTIMIZE get rid of this? why do we need it
  119. void dispose() {
  120. next = prev = null;
  121. instruction.dispose();
  122. instruction = null;
  123. pos = -1;
  124. removeAllTargeters();
  125. }
  126. /**
  127. * Remove all targeters, if any.
  128. */
  129. public void removeAllTargeters() {
  130. targeters.clear();
  131. }
  132. /**
  133. * Denote this handle isn't referenced anymore by t.
  134. */
  135. public void removeTargeter(InstructionTargeter t) {
  136. targeters.remove(t);
  137. }
  138. /**
  139. * Denote this handle is being referenced by t.
  140. */
  141. public void addTargeter(InstructionTargeter t) {
  142. if (targeters == Collections.EMPTY_SET) {
  143. targeters = new HashSet<>();
  144. }
  145. targeters.add(t);
  146. }
  147. public boolean hasTargeters() {
  148. return !targeters.isEmpty();
  149. }
  150. public Set<InstructionTargeter> getTargeters() {
  151. return targeters;
  152. }
  153. public Set<InstructionTargeter> getTargetersCopy() {
  154. Set<InstructionTargeter> copy = new HashSet<>(targeters);
  155. return copy;
  156. }
  157. /**
  158. * @return a (verbose) string representation of the contained instruction.
  159. */
  160. public String toString(boolean verbose) {
  161. return Utility.format(pos, 4, false, ' ') + ": " + instruction.toString(verbose);
  162. }
  163. public String toString() {
  164. return toString(true);
  165. }
  166. }