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