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  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.util.ByteSequence;
24  
25  /**
26   * Abstract super class for branching instructions like GOTO, IFEQ, etc..
27   * Branch instructions may have a variable length, namely GOTO, JSR,
28   * LOOKUPSWITCH and TABLESWITCH.
29   *
30   * @see InstructionList
31   */
32  public abstract class BranchInstruction extends Instruction implements InstructionTargeter {
33  
34      /**
35       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
36       */
37      @Deprecated
38      protected int index; // Branch target relative to this instruction
39  
40      /**
41       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
42       */
43      @Deprecated
44      protected InstructionHandle target; // Target object in instruction list
45  
46      /**
47       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
48       */
49      @Deprecated
50      protected int position; // Byte code offset
51  
52  
53      /**
54       * Empty constructor needed for Instruction.readInstruction.
55       * Not to be used otherwise.
56       */
57      BranchInstruction() {
58      }
59  
60  
61      /** Common super constructor
62       * @param opcode Instruction opcode
63       * @param target instruction to branch to
64       */
65      protected BranchInstruction(final short opcode, final InstructionHandle target) {
66          super(opcode, (short) 3);
67          setTarget(target);
68      }
69  
70  
71      /**
72       * Dump instruction as byte code to stream out.
73       * @param out Output stream
74       */
75      @Override
76      public void dump( final DataOutputStream out ) throws IOException {
77          out.writeByte(super.getOpcode());
78          index = getTargetOffset();
79          if (!isValidShort(index)) {
80              throw new ClassGenException("Branch target offset too large for short: " + index);
81          }
82          out.writeShort(index); // May be negative, i.e., point backwards
83      }
84  
85  
86      /**
87       * @param _target branch target
88       * @return the offset to  `target' relative to this instruction
89       */
90      protected int getTargetOffset( final InstructionHandle _target ) {
91          if (_target == null) {
92              throw new ClassGenException("Target of " + super.toString(true)
93                      + " is invalid null handle");
94          }
95          final int t = _target.getPosition();
96          if (t < 0) {
97              throw new ClassGenException("Invalid branch target position offset for "
98                      + super.toString(true) + ":" + t + ":" + _target);
99          }
100         return t - position;
101     }
102 
103 
104     /**
105      * @return the offset to this instruction's target
106      */
107     protected int getTargetOffset() {
108         return getTargetOffset(target);
109     }
110 
111 
112     /**
113      * Called by InstructionList.setPositions when setting the position for every
114      * instruction. In the presence of variable length instructions `setPositions'
115      * performs multiple passes over the instruction list to calculate the
116      * correct (byte) positions and offsets by calling this function.
117      *
118      * @param offset additional offset caused by preceding (variable length) instructions
119      * @param max_offset the maximum offset that may be caused by these instructions
120      * @return additional offset caused by possible change of this instruction's length
121      */
122     protected int updatePosition( final int offset, final int max_offset ) {
123         position += offset;
124         return 0;
125     }
126 
127 
128     /**
129      * Long output format:
130      *
131      * &lt;position in byte code&gt;
132      * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]"
133      * "("&lt;length of instruction&gt;")"
134      * "&lt;"&lt;target instruction&gt;"&gt;" "@"&lt;branch target offset&gt;
135      *
136      * @param verbose long/short format switch
137      * @return mnemonic for instruction
138      */
139     @Override
140     public String toString( final boolean verbose ) {
141         final String s = super.toString(verbose);
142         String t = "null";
143         if (verbose) {
144             if (target != null) {
145                 if (target.getInstruction() == this) {
146                     t = "<points to itself>";
147                 } else if (target.getInstruction() == null) {
148                     t = "<null instruction!!!?>";
149                 } else {
150                     // I'm more interested in the address of the target then
151                     // the instruction located there.
152                     //t = target.getInstruction().toString(false); // Avoid circles
153                     t = "" + target.getPosition();
154                 }
155             }
156         } else {
157             if (target != null) {
158                 index = target.getPosition();
159                 // index = getTargetOffset();  crashes if positions haven't been set
160                 // t = "" + (index + position);
161                 t = "" + index;
162             }
163         }
164         return s + " -> " + t;
165     }
166 
167 
168     /**
169      * Read needed data (e.g. index) from file. Conversion to a InstructionHandle
170      * is done in InstructionList(byte[]).
171      *
172      * @param bytes input stream
173      * @param wide wide prefix?
174      * @see InstructionList
175      */
176     @Override
177     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
178         super.setLength(3);
179         index = bytes.readShort();
180     }
181 
182 
183     /**
184      * @return target offset in byte code
185      */
186     public final int getIndex() {
187         return index;
188     }
189 
190 
191     /**
192      * @return target of branch instruction
193      */
194     public InstructionHandle getTarget() {
195         return target;
196     }
197 
198 
199     /**
200      * Set branch target
201      * @param target branch target
202      */
203     public void setTarget( final InstructionHandle target ) {
204         notifyTarget(this.target, target, this);
205         this.target = target;
206     }
207 
208 
209     /**
210      * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen, LineNumberGen
211      */
212     static void notifyTarget( final InstructionHandlee.html#InstructionHandle">InstructionHandle old_ih, final InstructionHandle new_ih,
213             final InstructionTargeter t ) {
214         if (old_ih != null) {
215             old_ih.removeTargeter(t);
216         }
217         if (new_ih != null) {
218             new_ih.addTargeter(t);
219         }
220     }
221 
222 
223     /**
224      * @param old_ih old target
225      * @param new_ih new target
226      */
227     @Override
228     public void updateTarget( final InstructionHandlee.html#InstructionHandle">InstructionHandle old_ih, final InstructionHandle new_ih ) {
229         if (target == old_ih) {
230             setTarget(new_ih);
231         } else {
232             throw new ClassGenException("Not targeting " + old_ih + ", but " + target);
233         }
234     }
235 
236 
237     /**
238      * @return true, if ih is target of this instruction
239      */
240     @Override
241     public boolean containsTarget( final InstructionHandle ih ) {
242         return target == ih;
243     }
244 
245 
246     /**
247      * Inform target that it's not targeted anymore.
248      */
249     @Override
250     void dispose() {
251         setTarget(null);
252         index = -1;
253         position = -1;
254     }
255 
256 
257     /**
258      * @return the position
259      * @since 6.0
260      */
261     protected int getPosition() {
262         return position;
263     }
264 
265 
266     /**
267      * @param position the position to set
268      * @since 6.0
269      */
270     protected void setPosition(final int position) {
271         this.position = position;
272     }
273 
274 
275     /**
276      * @param index the index to set
277      * @since 6.0
278      */
279     protected void setIndex(final int index) {
280         this.index = index;
281     }
282 
283 }