1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 /***
20 * BranchHandle is returned by specialized InstructionList.append() whenever a
21 * BranchInstruction is appended. This is useful when the target of this
22 * instruction is not known at time of creation and must be set later
23 * via setTarget().
24 *
25 * @see InstructionHandle
26 * @see Instruction
27 * @see InstructionList
28 * @version $Id: BranchHandle.java 386056 2006-03-15 11:31:56Z tcurdt $
29 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
30 */
31 public final class BranchHandle extends InstructionHandle {
32
33 private BranchInstruction bi;
34
35
36 private BranchHandle(BranchInstruction i) {
37 super(i);
38 bi = i;
39 }
40
41 /*** Factory methods.
42 */
43 private static BranchHandle bh_list = null;
44
45
46 static final BranchHandle getBranchHandle( BranchInstruction i ) {
47 if (bh_list == null) {
48 return new BranchHandle(i);
49 }
50 BranchHandle bh = bh_list;
51 bh_list = (BranchHandle) bh.next;
52 bh.setInstruction(i);
53 return bh;
54 }
55
56
57 /*** Handle adds itself to the list of resuable handles.
58 */
59 protected void addHandle() {
60 next = bh_list;
61 bh_list = this;
62 }
63
64
65
66
67
68
69 public int getPosition() {
70 return bi.position;
71 }
72
73
74 void setPosition( int pos ) {
75 i_position = bi.position = pos;
76 }
77
78
79 protected int updatePosition( int offset, int max_offset ) {
80 int x = bi.updatePosition(offset, max_offset);
81 i_position = bi.position;
82 return x;
83 }
84
85
86 /***
87 * Pass new target to instruction.
88 */
89 public void setTarget( InstructionHandle ih ) {
90 bi.setTarget(ih);
91 }
92
93
94 /***
95 * Update target of instruction.
96 */
97 public void updateTarget( InstructionHandle old_ih, InstructionHandle new_ih ) {
98 bi.updateTarget(old_ih, new_ih);
99 }
100
101
102 /***
103 * @return target of instruction.
104 */
105 public InstructionHandle getTarget() {
106 return bi.getTarget();
107 }
108
109
110 /***
111 * Set new contents. Old instruction is disposed and may not be used anymore.
112 */
113 public void setInstruction( Instruction i ) {
114 super.setInstruction(i);
115 if (!(i instanceof BranchInstruction)) {
116 throw new ClassGenException("Assigning " + i
117 + " to branch handle which is not a branch instruction");
118 }
119 bi = (BranchInstruction) i;
120 }
121 }