001/*
002 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025import java.util.*;
026
027/**
028 * Represents an exception handler within the bytecodes.
029 */
030public final class ExceptionHandler {
031
032    private final int startBCI;
033    private final int endBCI;
034    private final int handlerBCI;
035    private final int catchTypeCPI;
036    private final JavaType catchType;
037
038    /**
039     * Creates a new exception handler with the specified ranges.
040     *
041     * @param startBCI the start index of the protected range
042     * @param endBCI the end index of the protected range
043     * @param catchBCI the index of the handler
044     * @param catchTypeCPI the index of the throwable class in the constant pool
045     * @param catchType the type caught by this exception handler
046     */
047    public ExceptionHandler(int startBCI, int endBCI, int catchBCI, int catchTypeCPI, JavaType catchType) {
048        this.startBCI = startBCI;
049        this.endBCI = endBCI;
050        this.handlerBCI = catchBCI;
051        this.catchTypeCPI = catchTypeCPI;
052        this.catchType = catchType;
053    }
054
055    /**
056     * Returns the start bytecode index of the protected range of this handler.
057     */
058    public int getStartBCI() {
059        return startBCI;
060    }
061
062    /**
063     * Returns the end bytecode index of the protected range of this handler.
064     */
065    public int getEndBCI() {
066        return endBCI;
067    }
068
069    /**
070     * Returns the bytecode index of the handler block of this handler.
071     */
072    public int getHandlerBCI() {
073        return handlerBCI;
074    }
075
076    /**
077     * Returns the index into the constant pool representing the type of exception caught by this
078     * handler.
079     */
080    public int catchTypeCPI() {
081        return catchTypeCPI;
082    }
083
084    /**
085     * Checks whether this handler catches all exceptions.
086     *
087     * @return {@code true} if this handler catches all exceptions
088     */
089    public boolean isCatchAll() {
090        return catchTypeCPI == 0;
091    }
092
093    /**
094     * Returns the type of exception caught by this exception handler.
095     */
096    public JavaType getCatchType() {
097        return catchType;
098    }
099
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}