View Javadoc

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;
20  import java.io.IOException;
21  import org.apache.bcel.util.ByteSequence;
22  
23  /*** 
24   * LOOKUPSWITCH - Switch with unordered set of values
25   *
26   * @version $Id: LOOKUPSWITCH.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 LOOKUPSWITCH extends Select {
31  
32      /***
33       * Empty constructor needed for the Class.newInstance() statement in
34       * Instruction.readInstruction(). Not to be used otherwise.
35       */
36      LOOKUPSWITCH() {
37      }
38  
39  
40      public LOOKUPSWITCH(int[] match, InstructionHandle[] targets, InstructionHandle defaultTarget) {
41          super(org.apache.bcel.Constants.LOOKUPSWITCH, match, targets, defaultTarget);
42          length = (short) (9 + match_length * 8); /* alignment remainder assumed
43           * 0 here, until dump time. */
44          fixed_length = length;
45      }
46  
47  
48      /***
49       * Dump instruction as byte code to stream out.
50       * @param out Output stream
51       */
52      public void dump( DataOutputStream out ) throws IOException {
53          super.dump(out);
54          out.writeInt(match_length); // npairs
55          for (int i = 0; i < match_length; i++) {
56              out.writeInt(match[i]); // match-offset pairs
57              out.writeInt(indices[i] = getTargetOffset(targets[i]));
58          }
59      }
60  
61  
62      /***
63       * Read needed data (e.g. index) from file.
64       */
65      protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
66          super.initFromFile(bytes, wide); // reads padding
67          match_length = bytes.readInt();
68          fixed_length = (short) (9 + match_length * 8);
69          length = (short) (fixed_length + padding);
70          match = new int[match_length];
71          indices = new int[match_length];
72          targets = new InstructionHandle[match_length];
73          for (int i = 0; i < match_length; i++) {
74              match[i] = bytes.readInt();
75              indices[i] = bytes.readInt();
76          }
77      }
78  
79  
80      /***
81       * Call corresponding visitor method(s). The order is:
82       * Call visitor methods of implemented interfaces first, then
83       * call methods according to the class hierarchy in descending order,
84       * i.e., the most specific visitXXX() call comes last.
85       *
86       * @param v Visitor object
87       */
88      public void accept( Visitor v ) {
89          v.visitVariableLengthInstruction(this);
90          v.visitStackProducer(this);
91          v.visitBranchInstruction(this);
92          v.visitSelect(this);
93          v.visitLOOKUPSWITCH(this);
94      }
95  }