1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 package org.apache.bcel.generic; 19 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 23 import org.apache.bcel.util.ByteSequence; 24 25 /** 26 * TABLESWITCH - Switch within given range of values, i.e., low..high 27 * 28 * @version $Id$ 29 * @see SWITCH 30 */ 31 public class TABLESWITCH extends Select { 32 33 /** 34 * Empty constructor needed for Instruction.readInstruction. 35 * Not to be used otherwise. 36 */ 37 TABLESWITCH() { 38 } 39 40 41 /** 42 * @param match sorted array of match values, match[0] must be low value, 43 * match[match_length - 1] high value 44 * @param targets where to branch for matched values 45 * @param defaultTarget default branch 46 */ 47 public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) { 48 super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget); 49 /* Alignment remainder assumed 0 here, until dump time */ 50 final short _length = (short) (13 + getMatch_length() * 4); 51 super.setLength(_length); 52 setFixed_length(_length); 53 } 54 55 56 /** 57 * Dump instruction as byte code to stream out. 58 * @param out Output stream 59 */ 60 @Override 61 public void dump( final DataOutputStream out ) throws IOException { 62 super.dump(out); 63 final int _match_length = getMatch_length(); 64 final int low = (_match_length > 0) ? super.getMatch(0) : 0; 65 out.writeInt(low); 66 final int high = (_match_length > 0) ? super.getMatch(_match_length - 1) : 0; 67 out.writeInt(high); 68 for (int i = 0; i < _match_length; i++) { 69 out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i)))); 70 } 71 } 72 73 74 /** 75 * Read needed data (e.g. index) from file. 76 */ 77 @Override 78 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { 79 super.initFromFile(bytes, wide); 80 final int low = bytes.readInt(); 81 final int high = bytes.readInt(); 82 final int _match_length = high - low + 1; 83 setMatch_length(_match_length); 84 final short _fixed_length = (short) (13 + _match_length * 4); 85 setFixed_length(_fixed_length); 86 super.setLength((short) (_fixed_length + super.getPadding())); 87 super.setMatches(new int[_match_length]); 88 super.setIndices(new int[_match_length]); 89 super.setTargets(new InstructionHandle[_match_length]); 90 for (int i = 0; i < _match_length; i++) { 91 super.setMatch(i, low + i); 92 super.setIndices(i, bytes.readInt()); 93 } 94 } 95 96 97 /** 98 * Call corresponding visitor method(s). The order is: 99 * Call visitor methods of implemented interfaces first, then 100 * call methods according to the class hierarchy in descending order, 101 * i.e., the most specific visitXXX() call comes last. 102 * 103 * @param v Visitor object 104 */ 105 @Override 106 public void accept( final Visitor v ) { 107 v.visitVariableLengthInstruction(this); 108 v.visitStackConsumer(this); 109 v.visitBranchInstruction(this); 110 v.visitSelect(this); 111 v.visitTABLESWITCH(this); 112 } 113 } 114