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