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   * BranchHandle is returned by specialized InstructionList.append() whenever a BranchInstruction is appended. This is
21   * useful when the target of this instruction is not known at time of creation and must be set later via setTarget().
22   *
23   * @see InstructionHandle
24   * @see Instruction
25   * @see InstructionList
26   */
27  public final class BranchHandle extends InstructionHandle {
28  
29      /**
30       * Factory method.
31       */
32      static BranchHandle getBranchHandle(final BranchInstruction i) {
33          return new BranchHandle(i);
34      }
35  
36      // This is also a cache in case the InstructionHandle#swapInstruction() method is used
37      // See BCEL-273
38      private BranchInstruction bi; // An alias in fact, but saves lots of casts
39  
40      private BranchHandle(final BranchInstruction i) {
41          super(i);
42          bi = i;
43      }
44  
45      /*
46       * Override InstructionHandle methods: delegate to branch instruction. Through this overriding all access to the private
47       * i_position field should be prevented.
48       */
49      @Override
50      public int getPosition() {
51          return bi.getPosition();
52      }
53  
54      /**
55       * @return target of instruction.
56       */
57      public InstructionHandle getTarget() {
58          return bi.getTarget();
59      }
60  
61      /**
62       * Sets new contents. Old instruction is disposed and may not be used anymore.
63       */
64      @Override // This is only done in order to apply the additional type check; could be merged with super impl.
65      public void setInstruction(final Instruction i) { // TODO could be package-protected?
66          super.setInstruction(i);
67          if (!(i instanceof BranchInstruction)) {
68              throw new ClassGenException("Assigning " + i + " to branch handle which is not a branch instruction");
69          }
70          bi = (BranchInstruction) i;
71      }
72  
73      @Override
74      void setPosition(final int pos) {
75          // Original code: i_position = bi.position = pos;
76          bi.setPosition(pos);
77          super.setPosition(pos);
78      }
79  
80      /**
81       * Pass new target to instruction.
82       */
83      public void setTarget(final InstructionHandle ih) {
84          bi.setTarget(ih);
85      }
86  
87      @Override
88      protected int updatePosition(final int offset, final int maxOffset) {
89          final int x = bi.updatePosition(offset, maxOffset);
90          super.setPosition(bi.getPosition());
91          return x;
92      }
93  
94      /**
95       * Update target of instruction.
96       */
97      public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
98          bi.updateTarget(oldIh, newIh);
99      }
100 }