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