KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18     Describes the <tt>lookupswitch</tt> instruction.
19  
20     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
21     @version $Revision: 1.7 $ $Date: 2003/08/18 07:58:35 $
22 */

23 public class LookupSwitchInstruction extends PaddedInstruction {
24
25     private int defaultOffset;
26     private List JavaDoc matchOffsetPairs = new ArrayList JavaDoc();
27    
28     /**
29         Constructor.
30         @param opcode the opcode.
31      */

32     public LookupSwitchInstruction(int opcode) {
33         super(opcode);
34     }
35     
36     public int getSize() {
37         return super.getSize() + 8 + 8 * matchOffsetPairs.size();
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 match-offset pairs of the branch of this instruction as
58         a <tt>java.util.List</tt> of <tt>MatchOffsetPair</tt>
59         elements.
60         @return the list
61      */

62     public List JavaDoc getMatchOffsetPairs() {
63         return matchOffsetPairs;
64     }
65     
66     /**
67         Set the match-offset pairs of the branch of this instruction as
68         a <tt>java.util.List</tt> of <tt>LookupSwitchInstruction.MatchOffsetPair</tt>
69         elements.
70         @param matchOffsetPairs the list
71      */

72     public void setMatchOffsetPairs(List JavaDoc matchOffsetPairs) {
73         this.matchOffsetPairs = matchOffsetPairs;
74     }
75
76     public void read(ByteCodeInput in) throws IOException JavaDoc {
77         super.read(in);
78
79         matchOffsetPairs.clear();
80         
81         defaultOffset = in.readInt();
82         int numberOfPairs = in.readInt();
83         
84         int match, offset;
85         for (int i = 0; i < numberOfPairs; i++) {
86             match = in.readInt();
87             offset = in.readInt();
88             
89             matchOffsetPairs.add(new MatchOffsetPair(match, offset));
90         }
91         
92     }
93
94     public void write(ByteCodeOutput out) throws IOException JavaDoc {
95         super.write(out);
96
97         out.writeInt(defaultOffset);
98
99         int numberOfPairs = matchOffsetPairs.size();
100         out.writeInt(numberOfPairs);
101         
102         MatchOffsetPair currentMatchOffsetPair;
103         for (int i = 0; i < numberOfPairs; i++) {
104             currentMatchOffsetPair = (MatchOffsetPair)matchOffsetPairs.get(i);
105             out.writeInt(currentMatchOffsetPair.getMatch());
106             out.writeInt(currentMatchOffsetPair.getOffset());
107         }
108     }
109
110     
111 }
112
Popular Tags