KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.DataOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
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); /* Alignment remainder assumed
49          * 0 here, until dump time */

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 JavaDoc out ) throws IOException JavaDoc {
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 JavaDoc {
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 }
108
Popular Tags