16 * distributed under the License is distributed on an "AS IS" BASIS, |
16 * distributed under the License is distributed on an "AS IS" BASIS, |
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 * See the License for the specific language governing permissions and |
18 * See the License for the specific language governing permissions and |
19 * limitations under the License. |
19 * limitations under the License. |
20 */ |
20 */ |
21 |
|
22 package com.sun.org.apache.bcel.internal.generic; |
21 package com.sun.org.apache.bcel.internal.generic; |
23 |
|
24 |
22 |
25 /** |
23 /** |
26 * BranchHandle is returned by specialized InstructionList.append() whenever a |
24 * BranchHandle is returned by specialized InstructionList.append() whenever a |
27 * BranchInstruction is appended. This is useful when the target of this |
25 * BranchInstruction is appended. This is useful when the target of this |
28 * instruction is not known at time of creation and must be set later |
26 * instruction is not known at time of creation and must be set later via |
29 * via setTarget(). |
27 * setTarget(). |
30 * |
28 * |
31 * @see InstructionHandle |
29 * @see InstructionHandle |
32 * @see Instruction |
30 * @see Instruction |
33 * @see InstructionList |
31 * @see InstructionList |
34 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
32 * @version $Id: BranchHandle.java 1749603 2016-06-21 20:50:19Z ggregory $ |
35 */ |
33 */ |
36 public final class BranchHandle extends InstructionHandle { |
34 public final class BranchHandle extends InstructionHandle { |
37 private BranchInstruction bi; // An alias in fact, but saves lots of casts |
|
38 |
35 |
39 private BranchHandle(BranchInstruction i) { |
36 // This is also a cache in case the InstructionHandle#swapInstruction() method is used |
40 super(i); |
37 // See BCEL-273 |
41 bi = i; |
38 private BranchInstruction bi; // An alias in fact, but saves lots of casts |
42 } |
|
43 |
39 |
44 /** Factory methods. |
40 private BranchHandle(final BranchInstruction i) { |
45 */ |
41 super(i); |
46 private static BranchHandle bh_list = null; // List of reusable handles |
42 bi = i; |
|
43 } |
47 |
44 |
48 static final BranchHandle getBranchHandle(BranchInstruction i) { |
45 /** |
49 if(bh_list == null) |
46 * Factory methods. |
50 return new BranchHandle(i); |
47 */ |
51 else { |
48 private static BranchHandle bh_list = null; // List of reusable handles |
52 BranchHandle bh = bh_list; |
|
53 bh_list = (BranchHandle)bh.next; |
|
54 |
49 |
55 bh.setInstruction(i); |
50 static BranchHandle getBranchHandle(final BranchInstruction i) { |
|
51 if (bh_list == null) { |
|
52 return new BranchHandle(i); |
|
53 } |
|
54 final BranchHandle bh = bh_list; |
|
55 bh_list = (BranchHandle) bh.getNext(); |
|
56 bh.setInstruction(i); |
|
57 return bh; |
|
58 } |
56 |
59 |
57 return bh; |
60 /** |
|
61 * Handle adds itself to the list of resuable handles. |
|
62 */ |
|
63 @Override |
|
64 protected void addHandle() { |
|
65 super.setNext(bh_list); |
|
66 bh_list = this; |
58 } |
67 } |
59 } |
|
60 |
68 |
61 /** Handle adds itself to the list of resuable handles. |
|
62 */ |
|
63 protected void addHandle() { |
|
64 next = bh_list; |
|
65 bh_list = this; |
|
66 } |
|
67 |
69 |
68 /* Override InstructionHandle methods: delegate to branch instruction. |
70 /* Override InstructionHandle methods: delegate to branch instruction. |
69 * Through this overriding all access to the private i_position field should |
71 * Through this overriding all access to the private i_position field should |
70 * be prevented. |
72 * be prevented. |
71 */ |
73 */ |
72 public int getPosition() { return bi.position; } |
74 @Override |
|
75 public int getPosition() { |
|
76 return bi.getPosition(); |
|
77 } |
73 |
78 |
74 void setPosition(int pos) { |
79 @Override |
75 i_position = bi.position = pos; |
80 void setPosition(final int pos) { |
76 } |
81 // Original code: i_position = bi.position = pos; |
|
82 bi.setPosition(pos); |
|
83 super.setPosition(pos); |
|
84 } |
77 |
85 |
78 protected int updatePosition(int offset, int max_offset) { |
86 @Override |
79 int x = bi.updatePosition(offset, max_offset); |
87 protected int updatePosition(final int offset, final int max_offset) { |
80 i_position = bi.position; |
88 final int x = bi.updatePosition(offset, max_offset); |
81 return x; |
89 super.setPosition(bi.getPosition()); |
82 } |
90 return x; |
|
91 } |
83 |
92 |
84 /** |
93 /** |
85 * Pass new target to instruction. |
94 * Pass new target to instruction. |
86 */ |
95 */ |
87 public void setTarget(InstructionHandle ih) { |
96 public void setTarget(final InstructionHandle ih) { |
88 bi.setTarget(ih); |
97 bi.setTarget(ih); |
89 } |
98 } |
90 |
99 |
91 /** |
100 /** |
92 * Update target of instruction. |
101 * Update target of instruction. |
93 */ |
102 */ |
94 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { |
103 public void updateTarget(final InstructionHandle old_ih, final InstructionHandle new_ih) { |
95 bi.updateTarget(old_ih, new_ih); |
104 bi.updateTarget(old_ih, new_ih); |
96 } |
105 } |
97 |
106 |
98 /** |
107 /** |
99 * @return target of instruction. |
108 * @return target of instruction. |
100 */ |
109 */ |
101 public InstructionHandle getTarget() { |
110 public InstructionHandle getTarget() { |
102 return bi.getTarget(); |
111 return bi.getTarget(); |
103 } |
112 } |
104 |
113 |
105 /** |
114 /** |
106 * Set new contents. Old instruction is disposed and may not be used anymore. |
115 * Set new contents. Old instruction is disposed and may not be used |
107 */ |
116 * anymore. |
108 public void setInstruction(Instruction i) { |
117 */ |
109 super.setInstruction(i); |
118 @Override // This is only done in order to apply the additional type check; could be merged with super impl. |
110 |
119 public void setInstruction(final Instruction i) { // TODO could be package-protected? |
111 if(!(i instanceof BranchInstruction)) |
120 super.setInstruction(i); |
112 throw new ClassGenException("Assigning " + i + |
121 if (!(i instanceof BranchInstruction)) { |
113 " to branch handle which is not a branch instruction"); |
122 throw new ClassGenException("Assigning " + i |
114 |
123 + " to branch handle which is not a branch instruction"); |
115 bi = (BranchInstruction)i; |
124 } |
116 } |
125 bi = (BranchInstruction) i; |
|
126 } |
117 } |
127 } |