1 cananian 1.1.2.2 // ExceptionTable.java, created Mon Jan 18 22:44:38 1999 by cananian
  2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.2.1 package harpoon.IR.RawClass;
  5 cananian 1.1.2.1 
  6 cananian 1.1.2.1 /** 
  7 cananian 1.1.2.1  * Each <code>ExceptionTable</code> object describes one exception
  8 cananian 1.1.2.1  * handler in the <code>code</code> array of an
  9 cananian 1.1.2.1  * <code>AttributeCode</code>. 
 10 cananian 1.1.2.1  *
 11 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 12 cananian 1.2      * @version $Id: ExceptionTable.java,v 1.2 2002/02/25 21:05:27 cananian Exp $
 13 cananian 1.1.2.1  * @see "The Java Virtual Machine Specification, section 4.7.4"
 14 cananian 1.1.2.1  * @see AttributeCode
 15 cananian 1.1.2.1  */
 16 cananian 1.1.2.1 public class ExceptionTable {
 17 cananian 1.1.2.1   /** ClassFile in which this attribute information is found. */
 18 cananian 1.1.2.1   protected ClassFile parent;
 19 cananian 1.1.2.1 
 20 cananian 1.1.2.1   /** The values of the two items <code>start_pc</code> and
 21 cananian 1.1.2.1       <code>end_pc</code> indicate the ranges in the <code>code</code>
 22 cananian 1.1.2.1       array at which the exception handler is active.  The value of
 23 cananian 1.1.2.1       <code>start_pc</code> must be a valid index into the
 24 cananian 1.1.2.1       <code>code</code> array of the opcode of an instruction.  The
 25 cananian 1.1.2.1       value of <code>start_pc</code> must be less than the value of
 26 cananian 1.1.2.1       <code>end_pc</code>. <p> The <code>start_pc</code> is
 27 cananian 1.1.2.1       inclusive. */
 28 cananian 1.1.2.1   public int start_pc;
 29 cananian 1.1.2.1   /** The values of the two items <code>start_pc</code> and
 30 cananian 1.1.2.1       <code>end_pc</code> indicate the ranges in the <code>code</code>
 31 cananian 1.1.2.1       array at which the exception handler is active.  The value of
 32 cananian 1.1.2.1       <code>end_pc</code> either must be a valid index into the
 33 cananian 1.1.2.1       <code>code</code> array of the opcode of an instruction, or must
 34 cananian 1.1.2.1       be equal to <code>code_length</code>, the length of the
 35 cananian 1.1.2.1       <code>code</code> array.  The value of <code>start_pc</code>
 36 cananian 1.1.2.1       must be less than the value of <code>end_pc</code>. 
 37 cananian 1.1.2.1       <p> The <code>end_pc</code> is exclusive. */
 38 cananian 1.1.2.1   public int end_pc;
 39 cananian 1.1.2.1   /** The value of the <code>handler_pc</code> item indicates the
 40 cananian 1.1.2.1       start of the exception handler.  The value of the item must be a
 41 cananian 1.1.2.1       valid index into the <code>code</code> array, must be the index
 42 cananian 1.1.2.1       of the opcode of an instruction, and must be less than the value
 43 cananian 1.1.2.1       of the <code>code_length</code> item. */
 44 cananian 1.1.2.1   public int handler_pc;
 45 cananian 1.1.2.1   /** If the value of the <code>catch_type</code> item is nonzero, it
 46 cananian 1.1.2.1       must be a valid index into the <code>constant_pool</code>
 47 cananian 1.1.2.1       table.  the <code>constant_pool</code> entry at that index must
 48 cananian 1.1.2.1       be a <code>CONSTANT_Class_info</code> structure representing a
 49 cananian 1.1.2.1       class of exceptions that this exception handler is designated to
 50 cananian 1.1.2.1       catch.  This class must be the class <code>Throwable</code> of
 51 cananian 1.1.2.1       one of its subclasses.  The exception handler will be called
 52 cananian 1.1.2.1       only if the thrown exception is an instance of the given class
 53 cananian 1.1.2.1       or one of its subclasses.
 54 cananian 1.1.2.1       <p>
 55 cananian 1.1.2.1       If the value of the <code>catch_type</code> item is zero, this
 56 cananian 1.1.2.1       exception handler is called for all exceptions.  This is used to
 57 cananian 1.1.2.1       implement <code>finally</code>. */
 58 cananian 1.1.2.1   public int catch_type;
 59 cananian 1.1.2.1 
 60 cananian 1.1.2.1   /** Constructor. */
 61 cananian 1.1.2.1   ExceptionTable(ClassFile parent, ClassDataInputStream in)
 62 cananian 1.1.2.1        throws java.io.IOException 
 63 cananian 1.1.2.1   {
 64 cananian 1.1.2.1     this.parent = parent;
 65 cananian 1.1.2.1 
 66 cananian 1.1.2.1     start_pc = in.read_u2();
 67 cananian 1.1.2.1     end_pc = in.read_u2();
 68 cananian 1.1.2.1     handler_pc = in.read_u2();
 69 cananian 1.1.2.1     catch_type = in.read_u2();
 70 cananian 1.1.2.1   }
 71 cananian 1.1.2.1 
 72 cananian 1.1.2.1   /** Constructor. */
 73 cananian 1.1.2.1   public ExceptionTable(ClassFile parent, 
 74 cananian 1.1.2.1                         int start_pc, int end_pc, 
 75 cananian 1.1.2.1                         int handler_pc, int catch_type) {
 76 cananian 1.1.2.1     this.parent = parent;
 77 cananian 1.1.2.1 
 78 cananian 1.1.2.1     this.start_pc = start_pc;
 79 cananian 1.1.2.1     this.end_pc = end_pc;
 80 cananian 1.1.2.1     this.handler_pc = handler_pc;
 81 cananian 1.1.2.1     this.catch_type = catch_type;
 82 cananian 1.1.2.1   }
 83 cananian 1.1.2.1 
 84 cananian 1.1.2.1   /** Write to bytecode stream. */
 85 cananian 1.1.2.1   public void write(ClassDataOutputStream out) throws java.io.IOException {
 86 cananian 1.1.2.1     out.write_u2(start_pc);
 87 cananian 1.1.2.1     out.write_u2(end_pc);
 88 cananian 1.1.2.1     out.write_u2(handler_pc);
 89 cananian 1.1.2.1     out.write_u2(catch_type);
 90 cananian 1.1.2.1   }
 91 cananian 1.1.2.1 
 92 cananian 1.1.2.1   // convenience
 93 cananian 1.1.2.1   public ConstantClass catch_type() 
 94 cananian 1.1.2.1   { return (ConstantClass) parent.constant_pool[catch_type]; }
 95 cananian 1.1.2.1 
 96 cananian 1.1.2.1   /** Human-readable representation. */
 97 cananian 1.1.2.1   public String toString() {
 98 cananian 1.1.2.1     StringBuffer sb = new StringBuffer("ExceptionTable: ");
 99 cananian 1.1.2.1     sb.append("pc=["+start_pc+","+end_pc+") ");
100 cananian 1.1.2.1     sb.append("handler at "+handler_pc+" ");
101 cananian 1.1.2.1     sb.append("catches ");
102 cananian 1.1.2.1     if (catch_type==0)
103 cananian 1.1.2.1       sb.append("<all>");
104 cananian 1.1.2.1     else {
105 cananian 1.1.2.1       sb.append(catch_type().name());
106 cananian 1.1.2.1       sb.append(" {"+catch_type+"}");
107 cananian 1.1.2.1     }
108 cananian 1.1.2.1     return sb.toString();
109 cananian 1.1.2.1   }
110 cananian 1.1.2.1   /** Pretty-print this entry.
111 cananian 1.1.2.1    *  @param indent the indentation level to use.
112 cananian 1.1.2.1    */
113 cananian 1.1.2.1   public void print(java.io.PrintWriter pw, int indent) {
114 cananian 1.1.2.1     int in=indent;
115 cananian 1.1.2.1     ClassFile.indent(pw, in, 
116 cananian 1.1.2.1                      "pc=["+start_pc+","+end_pc+") handler at "+handler_pc);
117 cananian 1.1.2.1     String catches="<all>";
118 cananian 1.1.2.1     if (catch_type!=0)
119 cananian 1.1.2.1       catches = catch_type().name() + " {" + catch_type + "}";
120 cananian 1.1.2.1     ClassFile.indent(pw, in, "catches "+catches);
121 cananian 1.1.2.1   }
122 cananian 1.2     }