TargetLostException.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. /**
  19.  * Thrown by {@link InstructionList} when one or multiple disposed instructions are still being referenced by an {@link InstructionTargeter} object. I.e. the
  20.  * {@link InstructionTargeter} has to be notified that (one of) the {@link InstructionHandle} it is referencing is being removed from the
  21.  * {@link InstructionList} and thus not valid anymore.
  22.  *
  23.  * <p>
  24.  * 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
  25.  * this may be done:
  26.  * </p>
  27.  *
  28.  * <pre>
  29.  *     ...
  30.  *     try {
  31.  *         il.delete(start_ih, end_ih);
  32.  *     } catch (TargetLostException e) {
  33.  *         for (InstructionHandle target : e.getTargets()) {
  34.  *             for (InstructionTargeter targeter : target.getTargeters()) {
  35.  *                 targeter.updateTarget(target, new_target);
  36.  *             }
  37.  *         }
  38.  *     }
  39.  * </pre>
  40.  *
  41.  * @see InstructionHandle
  42.  * @see InstructionList
  43.  * @see InstructionTargeter
  44.  */
  45. public final class TargetLostException extends Exception {

  46.     private static final long serialVersionUID = -6857272667645328384L;
  47.     private final InstructionHandle[] targets;

  48.     TargetLostException(final InstructionHandle[] targets, final String message) {
  49.         super(message);
  50.         this.targets = targets;
  51.     }

  52.     /**
  53.      * Gets the list of instructions still being targeted.
  54.      *
  55.      * @return list of instructions still being targeted.
  56.      */
  57.     public InstructionHandle[] getTargets() {
  58.         return targets;
  59.     }
  60. }