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 */
017package org.apache.bcel.generic;
018
019/**
020 * Thrown by {@link InstructionList} when one or multiple disposed instructions are still being referenced by an {@link InstructionTargeter} object. I.e. the
021 * {@link InstructionTargeter} has to be notified that (one of) the {@link InstructionHandle} it is referencing is being removed from the
022 * {@link InstructionList} and thus not valid anymore.
023 *
024 * <p>
025 * Making this an exception instead of a return value forces the user to handle these case explicitly in a try { ... } catch. The following code illustrates how
026 * this may be done:
027 * </p>
028 *
029 * <pre>
030 *     ...
031 *     try {
032 *         il.delete(start_ih, end_ih);
033 *     } catch (TargetLostException e) {
034 *         for (InstructionHandle target : e.getTargets()) {
035 *             for (InstructionTargeter targeter : target.getTargeters()) {
036 *                 targeter.updateTarget(target, new_target);
037 *             }
038 *         }
039 *     }
040 * </pre>
041 *
042 * @see InstructionHandle
043 * @see InstructionList
044 * @see InstructionTargeter
045 */
046public final class TargetLostException extends Exception {
047
048    private static final long serialVersionUID = -6857272667645328384L;
049    private final InstructionHandle[] targets;
050
051    TargetLostException(final InstructionHandle[] targets, final String message) {
052        super(message);
053        this.targets = targets;
054    }
055
056    /**
057     * Gets the list of instructions still being targeted.
058     *
059     * @return list of instructions still being targeted.
060     */
061    public InstructionHandle[] getTargets() {
062        return targets;
063    }
064}