1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.meta;
  24 
  25 import java.util.Objects;
  26 
  27 /**
  28  * Represents an exception handler within the bytecodes.
  29  */
  30 public final class ExceptionHandler {
  31 
  32     private final int startBCI;
  33     private final int endBCI;
  34     private final int handlerBCI;
  35     private final int catchTypeCPI;
  36     private final JavaType catchType;
  37 
  38     /**
  39      * Creates a new exception handler with the specified ranges.
  40      *
  41      * @param startBCI the start index of the protected range
  42      * @param endBCI the end index of the protected range
  43      * @param catchBCI the index of the handler
  44      * @param catchTypeCPI the index of the throwable class in the constant pool
  45      * @param catchType the type caught by this exception handler
  46      */
  47     public ExceptionHandler(int startBCI, int endBCI, int catchBCI, int catchTypeCPI, JavaType catchType) {
  48         this.startBCI = startBCI;
  49         this.endBCI = endBCI;
  50         this.handlerBCI = catchBCI;
  51         this.catchTypeCPI = catchTypeCPI;
  52         this.catchType = catchType;
  53     }
  54 
  55     /**
  56      * Returns the start bytecode index of the protected range of this handler.
  57      */
  58     public int getStartBCI() {
  59         return startBCI;
  60     }
  61 
  62     /**
  63      * Returns the end bytecode index of the protected range of this handler.
  64      */
  65     public int getEndBCI() {
  66         return endBCI;
  67     }
  68 
  69     /**
  70      * Returns the bytecode index of the handler block of this handler.
  71      */
  72     public int getHandlerBCI() {
  73         return handlerBCI;
  74     }
  75 
  76     /**
  77      * Returns the index into the constant pool representing the type of exception caught by this
  78      * handler.
  79      */
  80     public int catchTypeCPI() {
  81         return catchTypeCPI;
  82     }
  83 
  84     /**
  85      * Checks whether this handler catches all exceptions.
  86      *
  87      * @return {@code true} if this handler catches all exceptions
  88      */
  89     public boolean isCatchAll() {
  90         return catchTypeCPI == 0;
  91     }
  92 
  93     /**
  94      * Returns the type of exception caught by this exception handler.
  95      */
  96     public JavaType getCatchType() {
  97         return catchType;
  98     }
  99 
 100     @Override
 101     public boolean equals(Object obj) {
 102         if (!(obj instanceof ExceptionHandler)) {
 103             return false;
 104         }
 105         ExceptionHandler that = (ExceptionHandler) obj;
 106         if (this.startBCI != that.startBCI || this.endBCI != that.endBCI || this.handlerBCI != that.handlerBCI || this.catchTypeCPI != that.catchTypeCPI) {
 107             return false;
 108         }
 109         return Objects.equals(this.catchType, that.catchType);
 110     }
 111 
 112     @Override
 113     public String toString() {
 114         return "ExceptionHandler<startBCI=" + startBCI + ", endBCI=" + endBCI + ", handlerBCI=" + handlerBCI + ", catchTypeCPI=" + catchTypeCPI + ", catchType=" + catchType + ">";
 115     }
 116 
 117     @Override
 118     public int hashCode() {
 119         return catchTypeCPI ^ endBCI ^ handlerBCI;
 120     }
 121 }