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     */
018    package org.apache.bcel.generic;
019    
020    import java.io.DataOutputStream;
021    import java.io.IOException;
022    import org.apache.bcel.util.ByteSequence;
023    
024    /** 
025     * LOOKUPSWITCH - Switch with unordered set of values
026     *
027     * @version $Id: LOOKUPSWITCH.java 1152072 2011-07-29 01:54:05Z dbrosius $
028     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
029     * @see SWITCH
030     */
031    public class LOOKUPSWITCH extends Select {
032    
033        private static final long serialVersionUID = -8263209830709467636L;
034    
035    
036        /**
037         * Empty constructor needed for the Class.newInstance() statement in
038         * Instruction.readInstruction(). Not to be used otherwise.
039         */
040        LOOKUPSWITCH() {
041        }
042    
043    
044        public LOOKUPSWITCH(int[] match, InstructionHandle[] targets, InstructionHandle defaultTarget) {
045            super(org.apache.bcel.Constants.LOOKUPSWITCH, match, targets, defaultTarget);
046            length = (short) (9 + match_length * 8); /* alignment remainder assumed
047             * 0 here, until dump time. */
048            fixed_length = length;
049        }
050    
051    
052        /**
053         * Dump instruction as byte code to stream out.
054         * @param out Output stream
055         */
056        @Override
057        public void dump( DataOutputStream out ) throws IOException {
058            super.dump(out);
059            out.writeInt(match_length); // npairs
060            for (int i = 0; i < match_length; i++) {
061                out.writeInt(match[i]); // match-offset pairs
062                out.writeInt(indices[i] = getTargetOffset(targets[i]));
063            }
064        }
065    
066    
067        /**
068         * Read needed data (e.g. index) from file.
069         */
070        @Override
071        protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
072            super.initFromFile(bytes, wide); // reads padding
073            match_length = bytes.readInt();
074            fixed_length = (short) (9 + match_length * 8);
075            length = (short) (fixed_length + padding);
076            match = new int[match_length];
077            indices = new int[match_length];
078            targets = new InstructionHandle[match_length];
079            for (int i = 0; i < match_length; i++) {
080                match[i] = bytes.readInt();
081                indices[i] = bytes.readInt();
082            }
083        }
084    
085    
086        /**
087         * Call corresponding visitor method(s). The order is:
088         * Call visitor methods of implemented interfaces first, then
089         * call methods according to the class hierarchy in descending order,
090         * i.e., the most specific visitXXX() call comes last.
091         *
092         * @param v Visitor object
093         */
094        @Override
095        public void accept( Visitor v ) {
096            v.visitVariableLengthInstruction(this);
097            v.visitStackConsumer(this);
098            v.visitBranchInstruction(this);
099            v.visitSelect(this);
100            v.visitLOOKUPSWITCH(this);
101        }
102    }