001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.util.ByteSequence;
025
026/**
027 * TABLESWITCH - Switch within given range of values, i.e., low..high
028 *
029 * @see SWITCH
030 */
031public class TABLESWITCH extends Select {
032
033    /**
034     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
035     */
036    TABLESWITCH() {
037    }
038
039    /**
040     * @param match sorted array of match values, match[0] must be low value, match[match_length - 1] high value
041     * @param targets where to branch for matched values
042     * @param defaultTarget default branch
043     */
044    public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
045        super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget);
046        /* Alignment remainder assumed 0 here, until dump time */
047        final short length = (short) (13 + getMatchLength() * 4);
048        super.setLength(length);
049        setFixedLength(length);
050    }
051
052    /**
053     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
054     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
055     *
056     * @param v Visitor object
057     */
058    @Override
059    public void accept(final Visitor v) {
060        v.visitVariableLengthInstruction(this);
061        v.visitStackConsumer(this);
062        v.visitBranchInstruction(this);
063        v.visitSelect(this);
064        v.visitTABLESWITCH(this);
065    }
066
067    /**
068     * Dump instruction as byte code to stream out.
069     *
070     * @param out Output stream
071     */
072    @Override
073    public void dump(final DataOutputStream out) throws IOException {
074        super.dump(out);
075        final int matchLength = getMatchLength();
076        final int low = matchLength > 0 ? super.getMatch(0) : 0;
077        out.writeInt(low);
078        final int high = matchLength > 0 ? super.getMatch(matchLength - 1) : 0;
079        out.writeInt(high);
080        for (int i = 0; i < matchLength; i++) {
081            out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
082        }
083    }
084
085    /**
086     * Reads needed data (for example index) from file.
087     */
088    @Override
089    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
090        super.initFromFile(bytes, wide);
091        final int low = bytes.readInt();
092        final int high = bytes.readInt();
093        final int matchLength = high - low + 1;
094        setMatchLength(matchLength);
095        final short fixedLength = (short) (13 + matchLength * 4);
096        setFixedLength(fixedLength);
097        super.setLength((short) (fixedLength + super.getPadding()));
098        super.setMatches(new int[matchLength]);
099        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}