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