View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.generic;
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.util.ByteSequence;
24  
25  /**
26   * LOOKUPSWITCH - Switch with unordered set of values
27   *
28   * @see SWITCH
29   */
30  public class LOOKUPSWITCH extends Select {
31  
32      /**
33       * Empty constructor needed for Instruction.readInstruction.
34       * Not to be used otherwise.
35       */
36      LOOKUPSWITCH() {
37      }
38  
39  
40      public LOOKUPSWITCH(final int[] match, final InstructionHandletml#InstructionHandle">InstructionHandle[] targets, final InstructionHandle defaultTarget) {
41          super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget);
42          /* alignment remainder assumed 0 here, until dump time. */
43          final short _length = (short) (9 + getMatch_length() * 8);
44          super.setLength(_length);
45          setFixed_length(_length);
46      }
47  
48  
49      /**
50       * Dump instruction as byte code to stream out.
51       * @param out Output stream
52       */
53      @Override
54      public void dump( final DataOutputStream out ) throws IOException {
55          super.dump(out);
56          final int _match_length = getMatch_length();
57          out.writeInt(_match_length); // npairs
58          for (int i = 0; i < _match_length; i++) {
59              out.writeInt(super.getMatch(i)); // match-offset pairs
60              out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
61          }
62      }
63  
64  
65      /**
66       * Read needed data (e.g. index) from file.
67       */
68      @Override
69      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
70          super.initFromFile(bytes, wide); // reads padding
71          final int _match_length = bytes.readInt();
72          setMatch_length(_match_length);
73          final short _fixed_length = (short) (9 + _match_length * 8);
74          setFixed_length(_fixed_length);
75          final short _length = (short) (_match_length + super.getPadding());
76          super.setLength(_length);
77          super.setMatches(new int[_match_length]);
78          super.setIndices(new int[_match_length]);
79          super.setTargets(new InstructionHandle[_match_length]);
80          for (int i = 0; i < _match_length; i++) {
81              super.setMatch(i, bytes.readInt());
82              super.setIndices(i, bytes.readInt());
83          }
84      }
85  
86  
87      /**
88       * Call corresponding visitor method(s). The order is:
89       * Call visitor methods of implemented interfaces first, then
90       * call methods according to the class hierarchy in descending order,
91       * i.e., the most specific visitXXX() call comes last.
92       *
93       * @param v Visitor object
94       */
95      @Override
96      public void accept( final Visitor v ) {
97          v.visitVariableLengthInstruction(this);
98          v.visitStackConsumer(this);
99          v.visitBranchInstruction(this);
100         v.visitSelect(this);
101         v.visitLOOKUPSWITCH(this);
102     }
103 }