View Javadoc

1   // ExceptionHandler.java, created Fri Jan 11 16:42:38 2002 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.Compiler.Quad;
5   
6   import joeq.Class.PrimordialClassLoader;
7   import joeq.Class.jq_Class;
8   import joeq.Runtime.TypeCheck;
9   import joeq.Util.Templates.List;
10  import joeq.Util.Templates.ListWrapper;
11  
12  /***
13   * Exception handler for basic blocks.  Each exception handler handles a type of
14   * exception.  When an exception is raised at run time, a routine looks up the list
15   * of exception handlers that guard the location where the exception was raised.
16   * It checks each of the exception handlers in order.  Control flow branches to the
17   * first exception handler whose type matches the type of the raised exception.
18   * Note that the type check is a Java "assignable" type check, and therefore
19   * inheritance and interface checks may be necessary.
20   * 
21   * @see  ExceptionHandlerList
22   * @see  joeq.Runtime.TypeCheck
23   * @author  John Whaley <jwhaley@alum.mit.edu>
24   * @version  $Id: ExceptionHandler.java 1712 2004-04-28 17:36:13Z joewhaley $
25   */
26  
27  public class ExceptionHandler {
28  
29      /*** Type of exception that this exception handler catches. */
30      private jq_Class exception_type;
31      /*** List of handled basic blocks. */
32      private java.util.List/*<BasicBlock>*/ handled_blocks;
33      /*** Exception handler entry point. */
34      private BasicBlock entry;
35      
36      /*** Creates new ExceptionHandler.
37       * @param ex_type  type of exception to catch.
38       * @param numOfHandledBlocks  estimated number of handled basic blocks.
39       * @param entry  exception handler entry point. */
40      public ExceptionHandler(jq_Class ex_type, int numOfHandledBlocks, BasicBlock entry) {
41          if (ex_type == null)
42              this.exception_type = PrimordialClassLoader.getJavaLangThrowable();
43          else
44              this.exception_type = ex_type;
45          this.handled_blocks = new java.util.ArrayList(numOfHandledBlocks);
46          this.entry = entry;
47      }
48      ExceptionHandler(jq_Class ex_type) {
49          if (ex_type == null)
50              this.exception_type = PrimordialClassLoader.getJavaLangThrowable();
51          else
52              this.exception_type = ex_type;
53          this.handled_blocks = new java.util.ArrayList();
54      }
55  
56      /*** Returns the type of exception that this exception handler catches.
57       * @return  the type of exception that this exception handler catches. */
58      public jq_Class getExceptionType() { return exception_type; }
59      /*** Returns an iteration of the handled basic blocks.
60       * @return  an iteration of the handled basic blocks. */
61      public List.BasicBlock getHandledBasicBlocks() { return new ListWrapper.BasicBlock(handled_blocks); }
62      /*** Returns the entry point for this exception handler.
63       * @return  the entry point for this exception handler. */
64      public BasicBlock getEntry() { return entry; }
65      public void setEntry(BasicBlock entry) {this.entry = entry; }
66  
67      public boolean mustCatch(jq_Class exType) {
68          exType.prepare();
69          exception_type.prepare();
70          return TypeCheck.isAssignable(exType, exception_type);
71      }
72      public boolean mayCatch(jq_Class exType) {
73          exType.prepare();
74          exception_type.prepare();
75          return TypeCheck.isAssignable(exType, exception_type) ||
76                TypeCheck.isAssignable(exception_type, exType);
77      }
78      public String toString() { return "Type: "+exception_type+" Entry: "+entry; }
79      
80      /*** Add a handled basic block to the list of handled basic blocks.
81       * @param bb  basic block to add. */
82      void addHandledBasicBlock(BasicBlock bb) { handled_blocks.add(bb); }
83  }