View Javadoc
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  /**
20   * Thrown by {@link InstructionList} when one or multiple disposed instructions are still being referenced by an {@link InstructionTargeter} object. I.e. the
21   * {@link InstructionTargeter} has to be notified that (one of) the {@link InstructionHandle} it is referencing is being removed from the
22   * {@link InstructionList} and thus not valid anymore.
23   *
24   * <p>
25   * 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
26   * this may be done:
27   * </p>
28   *
29   * <pre>
30   *     ...
31   *     try {
32   *         il.delete(start_ih, end_ih);
33   *     } catch (TargetLostException e) {
34   *         for (InstructionHandle target : e.getTargets()) {
35   *             for (InstructionTargeter targeter : target.getTargeters()) {
36   *                 targeter.updateTarget(target, new_target);
37   *             }
38   *         }
39   *     }
40   * </pre>
41   *
42   * @see InstructionHandle
43   * @see InstructionList
44   * @see InstructionTargeter
45   */
46  public final class TargetLostException extends Exception {
47  
48      private static final long serialVersionUID = -6857272667645328384L;
49      private final InstructionHandle[] targets;
50  
51      TargetLostException(final InstructionHandle[] targets, final String message) {
52          super(message);
53          this.targets = targets;
54      }
55  
56      /**
57       * Gets the list of instructions still being targeted.
58       *
59       * @return list of instructions still being targeted.
60       */
61      public InstructionHandle[] getTargets() {
62          return targets;
63      }
64  }