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