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 */
017package org.apache.bcel.generic;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022import org.apache.bcel.util.ByteSequence;
023
024/**
025 * LOOKUPSWITCH - Switch with unordered set of values
026 *
027 * @see SWITCH
028 */
029public class LOOKUPSWITCH extends Select {
030
031    /**
032     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
033     */
034    LOOKUPSWITCH() {
035    }
036
037    public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
038        super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget);
039        /* alignment remainder assumed 0 here, until dump time. */
040        final short length = (short) (9 + getMatchLength() * 8);
041        super.setLength(length);
042        setFixedLength(length);
043    }
044
045    /**
046     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
047     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
048     *
049     * @param v Visitor object
050     */
051    @Override
052    public void accept(final Visitor v) {
053        v.visitVariableLengthInstruction(this);
054        v.visitStackConsumer(this);
055        v.visitBranchInstruction(this);
056        v.visitSelect(this);
057        v.visitLOOKUPSWITCH(this);
058    }
059
060    /**
061     * Dump instruction as byte code to stream out.
062     *
063     * @param out Output stream
064     */
065    @Override
066    public void dump(final DataOutputStream out) throws IOException {
067        super.dump(out);
068        final int matchLength = getMatchLength();
069        out.writeInt(matchLength); // npairs
070        for (int i = 0; i < matchLength; i++) {
071            out.writeInt(super.getMatch(i)); // match-offset pairs
072            out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
073        }
074    }
075
076    /**
077     * Read needed data (e.g. index) from file.
078     */
079    @Override
080    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
081        super.initFromFile(bytes, wide); // reads padding
082        final int matchLength = bytes.readInt();
083        setMatchLength(matchLength);
084        final short fixedLength = (short) (9 + matchLength * 8);
085        setFixedLength(fixedLength);
086        final short length = (short) (matchLength + super.getPadding());
087        super.setLength(length);
088        super.setMatches(new int[matchLength]);
089        super.setIndices(new int[matchLength]);
090        super.setTargets(new InstructionHandle[matchLength]);
091        for (int i = 0; i < matchLength; i++) {
092            super.setMatch(i, bytes.readInt());
093            super.setIndices(i, bytes.readInt());
094        }
095    }
096}