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