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.

CodeException.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package org.aspectj.apache.bcel.classfile;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.Serializable;
  6. /* ====================================================================
  7. * The Apache Software License, Version 1.1
  8. *
  9. * Copyright (c) 2001 The Apache Software Foundation. All rights
  10. * reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * 3. The end-user documentation included with the redistribution,
  25. * if any, must include the following acknowledgment:
  26. * "This product includes software developed by the
  27. * Apache Software Foundation (http://www.apache.org/)."
  28. * Alternately, this acknowledgment may appear in the software itself,
  29. * if and wherever such third-party acknowledgments normally appear.
  30. *
  31. * 4. The names "Apache" and "Apache Software Foundation" and
  32. * "Apache BCEL" must not be used to endorse or promote products
  33. * derived from this software without prior written permission. For
  34. * written permission, please contact apache@apache.org.
  35. *
  36. * 5. Products derived from this software may not be called "Apache",
  37. * "Apache BCEL", nor may "Apache" appear in their name, without
  38. * prior written permission of the Apache Software Foundation.
  39. *
  40. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  41. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  42. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  47. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This software consists of voluntary contributions made by many
  55. * individuals on behalf of the Apache Software Foundation. For more
  56. * information on the Apache Software Foundation, please see
  57. * <http://www.apache.org/>.
  58. */
  59. import org.aspectj.apache.bcel.Constants;
  60. /**
  61. * This class represents an entry in the exception table of the <em>Code</em>
  62. * attribute and is used only there. It contains a range in which a
  63. * particular exception handler is active.
  64. *
  65. * @version $Id: CodeException.java,v 1.3 2008/05/28 23:53:02 aclement Exp $
  66. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  67. * @see Code
  68. */
  69. public final class CodeException implements Cloneable, Constants, Node, Serializable {
  70. private int start_pc; // Range in the code the exception handler is
  71. private int end_pc; // active. start_pc is inclusive, end_pc exclusive
  72. private int handler_pc; /* Starting address of exception handler, i.e.,
  73. * an offset from start of code.
  74. */
  75. private int catch_type; /* If this is zero the handler catches any
  76. * exception, otherwise it points to the
  77. * exception class which is to be caught.
  78. */
  79. public CodeException(CodeException c) {
  80. this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
  81. }
  82. CodeException(DataInputStream file) throws IOException {
  83. start_pc = file.readUnsignedShort();
  84. end_pc = file.readUnsignedShort();
  85. handler_pc = file.readUnsignedShort();
  86. catch_type = file.readUnsignedShort();
  87. }
  88. public CodeException(int start_pc, int end_pc, int handler_pc, int catch_type) {
  89. this.start_pc = start_pc;
  90. this.end_pc = end_pc;
  91. this.handler_pc = handler_pc;
  92. this.catch_type = catch_type;
  93. }
  94. public void accept(ClassVisitor v) {
  95. v.visitCodeException(this);
  96. }
  97. public final void dump(DataOutputStream file) throws IOException {
  98. file.writeShort(start_pc);
  99. file.writeShort(end_pc);
  100. file.writeShort(handler_pc);
  101. file.writeShort(catch_type);
  102. }
  103. /**
  104. * @return 0, if the handler catches any exception, otherwise it points to
  105. * the exception class which is to be caught.
  106. */
  107. public final int getCatchType() { return catch_type; }
  108. /**
  109. * @return Exclusive end index of the region where the handler is active.
  110. */
  111. public final int getEndPC() { return end_pc; }
  112. /**
  113. * @return Starting address of exception handler, relative to the code.
  114. */
  115. public final int getHandlerPC() { return handler_pc; }
  116. /**
  117. * @return Inclusive start index of the region where the handler is active.
  118. */
  119. public final int getStartPC() { return start_pc; }
  120. /**
  121. * @param catch_type.
  122. */
  123. public final void setCatchType(int catch_type) {
  124. this.catch_type = catch_type;
  125. }
  126. /**
  127. * @param end_pc end of handled block
  128. */
  129. public final void setEndPC(int end_pc) {
  130. this.end_pc = end_pc;
  131. }
  132. /**
  133. * @param handler_pc where the actual code is
  134. */
  135. public final void setHandlerPC(int handler_pc) {
  136. this.handler_pc = handler_pc;
  137. }
  138. /**
  139. * @param start_pc start of handled block
  140. */
  141. public final void setStartPC(int start_pc) {
  142. this.start_pc = start_pc;
  143. }
  144. /**
  145. * @return String representation.
  146. */
  147. public final String toString() {
  148. return "CodeException(start_pc = " + start_pc +
  149. ", end_pc = " + end_pc +
  150. ", handler_pc = " + handler_pc + ", catch_type = " + catch_type + ")";
  151. }
  152. /**
  153. * @return String representation.
  154. */
  155. public final String toString(ConstantPool cp, boolean verbose) {
  156. String str;
  157. if(catch_type == 0)
  158. str = "<Any exception>(0)";
  159. else
  160. str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false) +
  161. (verbose? "(" + catch_type + ")" : "");
  162. return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
  163. }
  164. public final String toString(ConstantPool cp) {
  165. return toString(cp, true);
  166. }
  167. /**
  168. * @return deep copy of this object
  169. */
  170. public CodeException copy() {
  171. try {
  172. return (CodeException)clone();
  173. } catch(CloneNotSupportedException e) {}
  174. return null;
  175. }
  176. }