KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > generic > BranchHandle


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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; // An alias in fact, but saves lots of casts
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; // List of reusable handles
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     /* Override InstructionHandle methods: delegate to branch instruction.
66      * Through this overriding all access to the private i_position field should
67      * be prevented.
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 }
122
Popular Tags