1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import org.apache.bcel.util.ByteSequence;
22
23 /***
24 * TABLESWITCH - Switch within given range of values, i.e., low..high
25 *
26 * @version $Id: TABLESWITCH.java 386056 2006-03-15 11:31:56Z tcurdt $
27 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
28 * @see SWITCH
29 */
30 public class TABLESWITCH extends Select {
31
32 /***
33 * Empty constructor needed for the Class.newInstance() statement in
34 * Instruction.readInstruction(). Not to be used otherwise.
35 */
36 TABLESWITCH() {
37 }
38
39
40 /***
41 * @param match sorted array of match values, match[0] must be low value,
42 * match[match_length - 1] high value
43 * @param targets where to branch for matched values
44 * @param defaultTarget default branch
45 */
46 public TABLESWITCH(int[] match, InstructionHandle[] targets, InstructionHandle defaultTarget) {
47 super(org.apache.bcel.Constants.TABLESWITCH, match, targets, defaultTarget);
48 length = (short) (13 + match_length * 4);
49
50 fixed_length = length;
51 }
52
53
54 /***
55 * Dump instruction as byte code to stream out.
56 * @param out Output stream
57 */
58 public void dump( DataOutputStream out ) throws IOException {
59 super.dump(out);
60 int low = (match_length > 0) ? match[0] : 0;
61 out.writeInt(low);
62 int high = (match_length > 0) ? match[match_length - 1] : 0;
63 out.writeInt(high);
64 for (int i = 0; i < match_length; i++) {
65 out.writeInt(indices[i] = getTargetOffset(targets[i]));
66 }
67 }
68
69
70 /***
71 * Read needed data (e.g. index) from file.
72 */
73 protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
74 super.initFromFile(bytes, wide);
75 int low = bytes.readInt();
76 int high = bytes.readInt();
77 match_length = high - low + 1;
78 fixed_length = (short) (13 + match_length * 4);
79 length = (short) (fixed_length + padding);
80 match = new int[match_length];
81 indices = new int[match_length];
82 targets = new InstructionHandle[match_length];
83 for (int i = low; i <= high; i++) {
84 match[i - low] = i;
85 }
86 for (int i = 0; i < match_length; i++) {
87 indices[i] = bytes.readInt();
88 }
89 }
90
91
92 /***
93 * Call corresponding visitor method(s). The order is:
94 * Call visitor methods of implemented interfaces first, then
95 * call methods according to the class hierarchy in descending order,
96 * i.e., the most specific visitXXX() call comes last.
97 *
98 * @param v Visitor object
99 */
100 public void accept( Visitor v ) {
101 v.visitVariableLengthInstruction(this);
102 v.visitStackProducer(this);
103 v.visitBranchInstruction(this);
104 v.visitSelect(this);
105 v.visitTABLESWITCH(this);
106 }
107 }