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