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