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.

CodeExceptionGen.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.classfile.CodeException;
  56. import org.aspectj.apache.bcel.classfile.ConstantPool;
  57. /**
  58. * This class represents an exception handler, i.e., specifies the region where
  59. * a handler is active and an instruction where the actual handling is done.
  60. * pool as parameters. Opposed to the JVM specification the end of the handled
  61. * region is set to be inclusive, i.e. all instructions between start and end
  62. * are protected including the start and end instructions (handles) themselves.
  63. * The end of the region is automatically mapped to be exclusive when calling
  64. * getCodeException(), i.e., there is no difference semantically.
  65. *
  66. * @version $Id: CodeExceptionGen.java,v 1.5 2008/05/28 23:52:56 aclement Exp $
  67. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  68. * @see MethodGen
  69. * @see CodeException
  70. * @see InstructionHandle
  71. */
  72. public final class CodeExceptionGen
  73. implements InstructionTargeter, Cloneable, java.io.Serializable {
  74. private InstructionHandle start_pc;
  75. private InstructionHandle end_pc;
  76. private InstructionHandle handler_pc;
  77. private ObjectType catch_type;
  78. /**
  79. * Add an exception handler, i.e., specify region where a handler is active and an
  80. * instruction where the actual handling is done.
  81. *
  82. * @param start_pc Start of handled region (inclusive)
  83. * @param end_pc End of handled region (inclusive)
  84. * @param handler_pc Where handling is done
  85. * @param catch_type which exception is handled, null for ANY
  86. */
  87. public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
  88. InstructionHandle handler_pc, ObjectType catch_type) {
  89. setStartPC(start_pc);
  90. setEndPC(end_pc);
  91. setHandlerPC(handler_pc);
  92. this.catch_type = catch_type;
  93. }
  94. /**
  95. * Get CodeException object.<BR>
  96. *
  97. * This relies on that the instruction list has already been dumped
  98. * to byte code or or that the `setPositions' methods has been
  99. * called for the instruction list.
  100. *
  101. * @param cp constant pool
  102. */
  103. public CodeException getCodeException(ConstantPool cp) {
  104. return new CodeException(start_pc.getPosition(),
  105. end_pc.getPosition() + end_pc.getInstruction().getLength(),
  106. handler_pc.getPosition(),
  107. (catch_type == null)? 0 : cp.addClass(catch_type));
  108. }
  109. /* Set start of handler
  110. * @param start_pc Start of handled region (inclusive)
  111. */
  112. public void setStartPC(InstructionHandle start_pc) {
  113. InstructionBranch.notifyTarget(this.start_pc, start_pc, this);
  114. this.start_pc = start_pc;
  115. }
  116. /* Set end of handler
  117. * @param end_pc End of handled region (inclusive)
  118. */
  119. public void setEndPC(InstructionHandle end_pc) {
  120. InstructionBranch.notifyTarget(this.end_pc, end_pc, this);
  121. this.end_pc = end_pc;
  122. }
  123. /* Set handler code
  124. * @param handler_pc Start of handler
  125. */
  126. public void setHandlerPC(InstructionHandle handler_pc) {
  127. InstructionBranch.notifyTarget(this.handler_pc, handler_pc, this);
  128. this.handler_pc = handler_pc;
  129. }
  130. /**
  131. * @param old_ih old target, either start or end
  132. * @param new_ih new target
  133. */
  134. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  135. boolean targeted = false;
  136. if(start_pc == old_ih) {
  137. targeted = true;
  138. setStartPC(new_ih);
  139. }
  140. if(end_pc == old_ih) {
  141. targeted = true;
  142. setEndPC(new_ih);
  143. }
  144. if(handler_pc == old_ih) {
  145. targeted = true;
  146. setHandlerPC(new_ih);
  147. }
  148. if(!targeted)
  149. throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " +
  150. end_pc + ", " + handler_pc + "}");
  151. }
  152. /**
  153. * @return true, if ih is target of this handler
  154. */
  155. public boolean containsTarget(InstructionHandle ih) {
  156. return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
  157. }
  158. /** Sets the type of the Exception to catch. Set 'null' for ANY. */
  159. public void setCatchType(ObjectType catch_type) { this.catch_type = catch_type; }
  160. /** Gets the type of the Exception to catch, 'null' for ANY. */
  161. public ObjectType getCatchType() { return catch_type; }
  162. /** @return start of handled region (inclusive)
  163. */
  164. public InstructionHandle getStartPC() { return start_pc; }
  165. /** @return end of handled region (inclusive)
  166. */
  167. public InstructionHandle getEndPC() { return end_pc; }
  168. /** @return start of handler
  169. */
  170. public InstructionHandle getHandlerPC() { return handler_pc; }
  171. public String toString() {
  172. return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
  173. }
  174. public Object clone() {
  175. try {
  176. return super.clone();
  177. } catch(CloneNotSupportedException e) {
  178. System.err.println(e);
  179. return null;
  180. }
  181. }
  182. }