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.
034     * Not to be used otherwise.
035     */
036    TABLESWITCH() {
037    }
038
039
040    /**
041     * @param match sorted array of match values, match[0] must be low value,
042     * match[match_length - 1] high value
043     * @param targets where to branch for matched values
044     * @param defaultTarget default branch
045     */
046    public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
047        super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget);
048        /* Alignment remainder assumed 0 here, until dump time */
049        final short _length = (short) (13 + getMatch_length() * 4);
050        super.setLength(_length);
051        setFixed_length(_length);
052    }
053
054
055    /**
056     * Dump instruction as byte code to stream out.
057     * @param out Output stream
058     */
059    @Override
060    public void dump( final DataOutputStream out ) throws IOException {
061        super.dump(out);
062        final int _match_length = getMatch_length();
063        final int low = (_match_length > 0) ? super.getMatch(0) : 0;
064        out.writeInt(low);
065        final int high = (_match_length > 0) ? super.getMatch(_match_length - 1) : 0;
066        out.writeInt(high);
067        for (int i = 0; i < _match_length; i++) {
068            out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
069        }
070    }
071
072
073    /**
074     * Read needed data (e.g. index) from file.
075     */
076    @Override
077    protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
078        super.initFromFile(bytes, wide);
079        final int low = bytes.readInt();
080        final int high = bytes.readInt();
081        final int _match_length = high - low + 1;
082        setMatch_length(_match_length);
083        final short _fixed_length = (short) (13 + _match_length * 4);
084        setFixed_length(_fixed_length);
085        super.setLength((short) (_fixed_length + super.getPadding()));
086        super.setMatches(new int[_match_length]);
087        super.setIndices(new int[_match_length]);
088        super.setTargets(new InstructionHandle[_match_length]);
089        for (int i = 0; i < _match_length; i++) {
090            super.setMatch(i, low + i);
091            super.setIndices(i, bytes.readInt());
092        }
093    }
094
095
096    /**
097     * Call corresponding visitor method(s). The order is:
098     * Call visitor methods of implemented interfaces first, then
099     * call methods according to the class hierarchy in descending order,
100     * i.e., the most specific visitXXX() call comes last.
101     *
102     * @param v Visitor object
103     */
104    @Override
105    public void accept( final Visitor v ) {
106        v.visitVariableLengthInstruction(this);
107        v.visitStackConsumer(this);
108        v.visitBranchInstruction(this);
109        v.visitSelect(this);
110        v.visitTABLESWITCH(this);
111    }
112}