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