001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.generic;
019
020import org.apache.bcel.classfile.CodeException;
021
022/**
023 * This class represents an exception handler, i.e., specifies the  region where
024 * a handler is active and an instruction where the actual handling is done.
025 * pool as parameters. Opposed to the JVM specification the end of the handled
026 * region is set to be inclusive, i.e. all instructions between start and end
027 * are protected including the start and end instructions (handles) themselves.
028 * The end of the region is automatically mapped to be exclusive when calling
029 * getCodeException(), i.e., there is no difference semantically.
030 *
031 * @see     MethodGen
032 * @see     CodeException
033 * @see     InstructionHandle
034 */
035public final class CodeExceptionGen implements InstructionTargeter, Cloneable {
036
037    private InstructionHandle startPc;
038    private InstructionHandle endPc;
039    private InstructionHandle handlerPc;
040    private ObjectType catchType;
041
042
043    /**
044     * Add an exception handler, i.e., specify region where a handler is active and an
045     * instruction where the actual handling is done.
046     *
047     * @param startPc Start of handled region (inclusive)
048     * @param endPc End of handled region (inclusive)
049     * @param handlerPc Where handling is done
050     * @param catchType which exception is handled, null for ANY
051     */
052    public CodeExceptionGen(final InstructionHandle startPc, final InstructionHandle endPc,
053            final InstructionHandle handlerPc, final ObjectType catchType) {
054        setStartPC(startPc);
055        setEndPC(endPc);
056        setHandlerPC(handlerPc);
057        this.catchType = catchType;
058    }
059
060
061    /**
062     * Get CodeException object.<BR>
063     *
064     * This relies on that the instruction list has already been dumped
065     * to byte code or or that the `setPositions' methods has been
066     * called for the instruction list.
067     *
068     * @param cp constant pool
069     */
070    public CodeException getCodeException( final ConstantPoolGen cp ) {
071        return new CodeException(startPc.getPosition(), endPc.getPosition()
072                + endPc.getInstruction().getLength(), handlerPc.getPosition(),
073                (catchType == null) ? 0 : cp.addClass(catchType));
074    }
075
076
077    /* Set start of handler
078     * @param startPc Start of handled region (inclusive)
079     */
080    public void setStartPC( final InstructionHandle start_pc ) { // TODO could be package-protected?
081        BranchInstruction.notifyTarget(this.startPc, start_pc, this);
082        this.startPc = start_pc;
083    }
084
085
086    /* Set end of handler
087     * @param endPc End of handled region (inclusive)
088     */
089    public void setEndPC( final InstructionHandle end_pc ) { // TODO could be package-protected?
090        BranchInstruction.notifyTarget(this.endPc, end_pc, this);
091        this.endPc = end_pc;
092    }
093
094
095    /* Set handler code
096     * @param handlerPc Start of handler
097     */
098    public void setHandlerPC( final InstructionHandle handler_pc ) { // TODO could be package-protected?
099        BranchInstruction.notifyTarget(this.handlerPc, handler_pc, this);
100        this.handlerPc = handler_pc;
101    }
102
103
104    /**
105     * @param old_ih old target, either start or end
106     * @param new_ih new target
107     */
108    @Override
109    public void updateTarget( final InstructionHandle old_ih, final InstructionHandle new_ih ) {
110        boolean targeted = false;
111        if (startPc == old_ih) {
112            targeted = true;
113            setStartPC(new_ih);
114        }
115        if (endPc == old_ih) {
116            targeted = true;
117            setEndPC(new_ih);
118        }
119        if (handlerPc == old_ih) {
120            targeted = true;
121            setHandlerPC(new_ih);
122        }
123        if (!targeted) {
124            throw new ClassGenException("Not targeting " + old_ih + ", but {" + startPc + ", "
125                    + endPc + ", " + handlerPc + "}");
126        }
127    }
128
129
130    /**
131     * @return true, if ih is target of this handler
132     */
133    @Override
134    public boolean containsTarget( final InstructionHandle ih ) {
135        return (startPc == ih) || (endPc == ih) || (handlerPc == ih);
136    }
137
138
139    /** Sets the type of the Exception to catch. Set 'null' for ANY. */
140    public void setCatchType( final ObjectType catchType ) {
141        this.catchType = catchType;
142    }
143
144
145    /** Gets the type of the Exception to catch, 'null' for ANY. */
146    public ObjectType getCatchType() {
147        return catchType;
148    }
149
150
151    /** @return start of handled region (inclusive)
152     */
153    public InstructionHandle getStartPC() {
154        return startPc;
155    }
156
157
158    /** @return end of handled region (inclusive)
159     */
160    public InstructionHandle getEndPC() {
161        return endPc;
162    }
163
164
165    /** @return start of handler
166     */
167    public InstructionHandle getHandlerPC() {
168        return handlerPc;
169    }
170
171
172    @Override
173    public String toString() {
174        return "CodeExceptionGen(" + startPc + ", " + endPc + ", " + handlerPc + ")";
175    }
176
177
178    @Override
179    public Object clone() {
180        try {
181            return super.clone();
182        } catch (final CloneNotSupportedException e) {
183            throw new Error("Clone Not Supported"); // never happens
184        }
185    }
186}