View Javadoc

1   // jq_TryCatch.java, created Mon Feb  5 23:23:20 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Class;
5   
6   import joeq.Runtime.Debug;
7   import joeq.Runtime.TypeCheck;
8   import jwutil.strings.Strings;
9   
10  /*
11   * @author  John Whaley <jwhaley@alum.mit.edu>
12   * @version $Id: jq_TryCatch.java 1931 2004-09-22 22:17:47Z joewhaley $
13   */
14  public class jq_TryCatch {
15  
16      public static final boolean DEBUG = false;
17      
18      // NOTE: startPC is exclusive, endPC is inclusive (opposite of jq_TryCatchBC)
19      // this is because the IP that we check against is IMMEDIATELY AFTER where the exception actually occurred.
20      // these are CODE OFFSETS.
21      private int startPC, endPC, handlerPC;
22      private jq_Class exType;
23      // this is the offset from the frame pointer where to put the exception.
24      private int exceptionOffset;
25  
26      public jq_TryCatch(int startPC, int endPC, int handlerPC, jq_Class exType, int exceptionOffset) {
27          this.startPC = startPC;
28          this.endPC = endPC;
29          this.handlerPC = handlerPC;
30          this.exType = exType;
31          this.exceptionOffset = exceptionOffset;
32      }
33  
34      // note: offset is the offset of the instruction after the one which threw the exception.
35      public boolean catches(int offset, jq_Class t) {
36          if (DEBUG) Debug.writeln(this+": checking "+Strings.hex(offset)+" "+t);
37          if (offset <= startPC) return false;
38          if (offset > endPC) return false;
39          if (exType != null) {
40              exType.prepare();
41              if (!TypeCheck.isAssignable(t, exType)) return false;
42          }
43          return true;
44      }
45      
46      public int getStart() { return startPC; }
47      public int getEnd() { return endPC; }
48      public int getHandlerEntry() { return handlerPC; }
49      public jq_Class getExceptionType() { return exType; }
50      public int getExceptionOffset() { return exceptionOffset; }
51  
52      public String toString() {
53          return "(start="+Strings.hex(startPC)+",end="+Strings.hex(endPC)+",handler="+Strings.hex(handlerPC)+",type="+exType+",offset="+Strings.shex(exceptionOffset)+")";
54      }
55      
56  }