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     * TABLESWITCH - Switch within given range of values, i.e., low..high
026     *
027     * @version $Id: TABLESWITCH.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 TABLESWITCH extends Select {
032    
033        private static final long serialVersionUID = -1178229029789923698L;
034    
035    
036        /**
037         * Empty constructor needed for the Class.newInstance() statement in
038         * Instruction.readInstruction(). Not to be used otherwise.
039         */
040        TABLESWITCH() {
041        }
042    
043    
044        /**
045         * @param match sorted array of match values, match[0] must be low value, 
046         * match[match_length - 1] high value
047         * @param targets where to branch for matched values
048         * @param defaultTarget default branch
049         */
050        public TABLESWITCH(int[] match, InstructionHandle[] targets, InstructionHandle defaultTarget) {
051            super(org.apache.bcel.Constants.TABLESWITCH, match, targets, defaultTarget);
052            length = (short) (13 + match_length * 4); /* Alignment remainder assumed
053             * 0 here, until dump time */
054            fixed_length = length;
055        }
056    
057    
058        /**
059         * Dump instruction as byte code to stream out.
060         * @param out Output stream
061         */
062        @Override
063        public void dump( DataOutputStream out ) throws IOException {
064            super.dump(out);
065            int low = (match_length > 0) ? match[0] : 0;
066            out.writeInt(low);
067            int high = (match_length > 0) ? match[match_length - 1] : 0;
068            out.writeInt(high);
069            for (int i = 0; i < match_length; i++) {
070                out.writeInt(indices[i] = getTargetOffset(targets[i]));
071            }
072        }
073    
074    
075        /**
076         * Read needed data (e.g. index) from file.
077         */
078        @Override
079        protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
080            super.initFromFile(bytes, wide);
081            int low = bytes.readInt();
082            int high = bytes.readInt();
083            match_length = high - low + 1;
084            fixed_length = (short) (13 + match_length * 4);
085            length = (short) (fixed_length + padding);
086            match = new int[match_length];
087            indices = new int[match_length];
088            targets = new InstructionHandle[match_length];
089            for (int i = 0; i < match_length; i++) {
090                match[i] = low + i;
091                indices[i] = bytes.readInt();
092            }
093        }
094    
095    
096        /**
097         * Call corresponding visitor method(s). The order is:
098         * Call visitor methods of implemented interfaces first, then
099         * call methods according to the class hierarchy in descending order,
100         * i.e., the most specific visitXXX() call comes last.
101         *
102         * @param v Visitor object
103         */
104        @Override
105        public void accept( Visitor v ) {
106            v.visitVariableLengthInstruction(this);
107            v.visitStackConsumer(this);
108            v.visitBranchInstruction(this);
109            v.visitSelect(this);
110            v.visitTABLESWITCH(this);
111        }
112    }