1. package com.sun.org.apache.bcel.internal.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 (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 com.sun.org.apache.bcel.internal.classfile.*;
  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.1.1.1 2001/10/29 20:00:08 jvanzyl 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 implements InstructionTargeter, Cloneable {
  73. private InstructionHandle start_pc;
  74. private InstructionHandle end_pc;
  75. private InstructionHandle handler_pc;
  76. private ObjectType catch_type;
  77. /**
  78. * Add an exception handler, i.e., specify region where a handler is active and an
  79. * instruction where the actual handling is done.
  80. *
  81. * @param start_pc Start of handled region (inclusive)
  82. * @param end_pc End of handled region (inclusive)
  83. * @param handler_pc Where handling is done
  84. * @param catch_type which exception is handled, null for ANY
  85. */
  86. public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
  87. InstructionHandle handler_pc, ObjectType catch_type) {
  88. setStartPC(start_pc);
  89. setEndPC(end_pc);
  90. setHandlerPC(handler_pc);
  91. this.catch_type = catch_type;
  92. }
  93. /**
  94. * Get CodeException object.<BR>
  95. *
  96. * This relies on that the instruction list has already been dumped
  97. * to byte code or or that the `setPositions' methods has been
  98. * called for the instruction list.
  99. *
  100. * @param cp constant pool
  101. */
  102. public CodeException getCodeException(ConstantPoolGen cp) {
  103. return new CodeException(start_pc.getPosition(),
  104. end_pc.getPosition() + end_pc.getInstruction().getLength(),
  105. handler_pc.getPosition(),
  106. (catch_type == null)? 0 : cp.addClass(catch_type));
  107. }
  108. /* Set start of handler
  109. * @param start_pc Start of handled region (inclusive)
  110. */
  111. public void setStartPC(InstructionHandle start_pc) {
  112. BranchInstruction.notifyTarget(this.start_pc, start_pc, this);
  113. this.start_pc = start_pc;
  114. }
  115. /* Set end of handler
  116. * @param end_pc End of handled region (inclusive)
  117. */
  118. public void setEndPC(InstructionHandle end_pc) {
  119. BranchInstruction.notifyTarget(this.end_pc, end_pc, this);
  120. this.end_pc = end_pc;
  121. }
  122. /* Set handler code
  123. * @param handler_pc Start of handler
  124. */
  125. public void setHandlerPC(InstructionHandle handler_pc) {
  126. BranchInstruction.notifyTarget(this.handler_pc, handler_pc, this);
  127. this.handler_pc = handler_pc;
  128. }
  129. /**
  130. * @param old_ih old target, either start or end
  131. * @param new_ih new target
  132. */
  133. public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
  134. boolean targeted = false;
  135. if(start_pc == old_ih) {
  136. targeted = true;
  137. setStartPC(new_ih);
  138. }
  139. if(end_pc == old_ih) {
  140. targeted = true;
  141. setEndPC(new_ih);
  142. }
  143. if(handler_pc == old_ih) {
  144. targeted = true;
  145. setHandlerPC(new_ih);
  146. }
  147. if(!targeted)
  148. throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " +
  149. end_pc + ", " + handler_pc + "}");
  150. }
  151. /**
  152. * @return true, if ih is target of this handler
  153. */
  154. public boolean containsTarget(InstructionHandle ih) {
  155. return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
  156. }
  157. /** Sets the type of the Exception to catch. Set 'null' for ANY. */
  158. public void setCatchType(ObjectType catch_type) { this.catch_type = catch_type; }
  159. /** Gets the type of the Exception to catch, 'null' for ANY. */
  160. public ObjectType getCatchType() { return catch_type; }
  161. /** @return start of handled region (inclusive)
  162. */
  163. public InstructionHandle getStartPC() { return start_pc; }
  164. /** @return end of handled region (inclusive)
  165. */
  166. public InstructionHandle getEndPC() { return end_pc; }
  167. /** @return start of handler
  168. */
  169. public InstructionHandle getHandlerPC() { return handler_pc; }
  170. public String toString() {
  171. return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
  172. }
  173. public Object clone() {
  174. try {
  175. return super.clone();
  176. } catch(CloneNotSupportedException e) {
  177. System.err.println(e);
  178. return null;
  179. }
  180. }
  181. }