KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > bytecode > TableSwitchInstruction


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.bytecode;
9
10 import org.gjt.jclasslib.io.ByteCodeInput;
11 import org.gjt.jclasslib.io.ByteCodeOutput;
12
13 import java.io.IOException JavaDoc;
14
15 /**
16     Describes the <tt>tableswitch</tt> instruction.
17  
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.5 $ $Date: 2003/08/18 07:58:35 $
20 */

21 public class TableSwitchInstruction extends PaddedInstruction {
22
23     private int defaultOffset;
24     private int lowByte;
25     private int highByte;
26     private int[] jumpOffsets;
27    
28     /**
29         Constructor.
30         @param opcode the opcode.
31      */

32     public TableSwitchInstruction(int opcode) {
33         super(opcode);
34     }
35     
36     public int getSize() {
37         return super.getSize() + 12 + 4 * jumpOffsets.length;
38     }
39
40     /**
41         Get the default offset of the branch of this instruction.
42         @return the offset
43      */

44     public int getDefaultOffset() {
45         return defaultOffset;
46     }
47
48     /**
49         Set the default offset of the branch of this instruction.
50         @param defaultOffset the offset
51      */

52     public void setDefaultOffset(int defaultOffset) {
53         this.defaultOffset = defaultOffset;
54     }
55     
56     /**
57         Get the lower bound for the table switch.
58         @return the lower bound
59      */

60     public int getLowByte() {
61         return lowByte;
62     }
63
64     /**
65         Set the lower bound for the table switch.
66         @param lowByte the lower bound
67      */

68     public void setLowByte(int lowByte) {
69         this.lowByte = lowByte;
70     }
71     
72     /**
73         Get the upper bound for the table switch.
74         @return the upper bound
75      */

76     public int getHighByte() {
77         return highByte;
78     }
79
80     /**
81         Set the upper bound for the table switch.
82         @param highByte the upper bound
83      */

84     public void setHighByte(int highByte) {
85         this.highByte = highByte;
86     }
87     
88     /**
89         Get the array of relative jump offsets for the table switch.
90         @return the array
91      */

92     public int[] getJumpOffsets() {
93         return jumpOffsets;
94     }
95
96     /**
97         Set the array of relative jump offsets for the table switch.
98         @param jumpOffsets the array
99      */

100     public void setJumpOffsets(int[] jumpOffsets) {
101         this.jumpOffsets = jumpOffsets;
102     }
103     
104     public void read(ByteCodeInput in) throws IOException JavaDoc {
105         super.read(in);
106
107         defaultOffset = in.readInt();
108         lowByte = in.readInt();
109         highByte = in.readInt();
110
111         int numberOfOffsets = highByte - lowByte + 1;
112         jumpOffsets = new int[numberOfOffsets];
113         
114         for (int i = 0; i < numberOfOffsets; i++) {
115             jumpOffsets[i] = in.readInt();
116         }
117         
118     }
119
120     public void write(ByteCodeOutput out) throws IOException JavaDoc {
121         super.write(out);
122
123         out.writeInt(defaultOffset);
124         out.writeInt(lowByte);
125         out.writeInt(highByte);
126
127         int numberOfOffsets = jumpOffsets.length;
128         
129         for (int i = 0; i < numberOfOffsets; i++) {
130             out.writeInt(jumpOffsets[i]);
131         }
132     }
133
134 }
135
Popular Tags