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   * TABLESWITCH - Switch within given range of values, i.e., low..high
26   *
27   * @see SWITCH
28   */
29  public class TABLESWITCH extends Select {
30  
31      /**
32       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
33       */
34      TABLESWITCH() {
35      }
36  
37      /**
38       * @param match sorted array of match values, match[0] must be low value, match[match_length - 1] high value
39       * @param targets where to branch for matched values
40       * @param defaultTarget default branch
41       */
42      public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
43          super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget);
44          /* Alignment remainder assumed 0 here, until dump time */
45          final short length = (short) (13 + getMatchLength() * 4);
46          super.setLength(length);
47          setFixedLength(length);
48      }
49  
50      /**
51       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
52       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
53       *
54       * @param v Visitor object
55       */
56      @Override
57      public void accept(final Visitor v) {
58          v.visitVariableLengthInstruction(this);
59          v.visitStackConsumer(this);
60          v.visitBranchInstruction(this);
61          v.visitSelect(this);
62          v.visitTABLESWITCH(this);
63      }
64  
65      /**
66       * Dump instruction as byte code to stream out.
67       *
68       * @param out Output stream
69       */
70      @Override
71      public void dump(final DataOutputStream out) throws IOException {
72          super.dump(out);
73          final int matchLength = getMatchLength();
74          final int low = matchLength > 0 ? super.getMatch(0) : 0;
75          out.writeInt(low);
76          final int high = matchLength > 0 ? super.getMatch(matchLength - 1) : 0;
77          out.writeInt(high);
78          for (int i = 0; i < matchLength; i++) {
79              out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
80          }
81      }
82  
83      /**
84       * Read needed data (e.g. index) from file.
85       */
86      @Override
87      protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
88          super.initFromFile(bytes, wide);
89          final int low = bytes.readInt();
90          final int high = bytes.readInt();
91          final int matchLength = high - low + 1;
92          setMatchLength(matchLength);
93          final short fixedLength = (short) (13 + matchLength * 4);
94          setFixedLength(fixedLength);
95          super.setLength((short) (fixedLength + super.getPadding()));
96          super.setMatches(new int[matchLength]);
97          super.setIndices(new int[matchLength]);
98          super.setTargets(new InstructionHandle[matchLength]);
99          for (int i = 0; i < matchLength; i++) {
100             super.setMatch(i, low + i);
101             super.setIndices(i, bytes.readInt());
102         }
103     }
104 }