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 */ 017package org.apache.bcel.generic; 018 019import java.io.DataOutputStream; 020import java.io.IOException; 021 022import org.apache.bcel.util.ByteSequence; 023 024/** 025 * TABLESWITCH - Switch within given range of values, i.e., low..high 026 * 027 * @see SWITCH 028 */ 029public class TABLESWITCH extends Select { 030 031 /** 032 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. 033 */ 034 TABLESWITCH() { 035 } 036 037 /** 038 * @param match sorted array of match values, match[0] must be low value, match[match_length - 1] high value 039 * @param targets where to branch for matched values 040 * @param defaultTarget default branch 041 */ 042 public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) { 043 super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget); 044 /* Alignment remainder assumed 0 here, until dump time */ 045 final short length = (short) (13 + getMatchLength() * 4); 046 super.setLength(length); 047 setFixedLength(length); 048 } 049 050 /** 051 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call 052 * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last. 053 * 054 * @param v Visitor object 055 */ 056 @Override 057 public void accept(final Visitor v) { 058 v.visitVariableLengthInstruction(this); 059 v.visitStackConsumer(this); 060 v.visitBranchInstruction(this); 061 v.visitSelect(this); 062 v.visitTABLESWITCH(this); 063 } 064 065 /** 066 * Dump instruction as byte code to stream out. 067 * 068 * @param out Output stream 069 */ 070 @Override 071 public void dump(final DataOutputStream out) throws IOException { 072 super.dump(out); 073 final int matchLength = getMatchLength(); 074 final int low = matchLength > 0 ? super.getMatch(0) : 0; 075 out.writeInt(low); 076 final int high = matchLength > 0 ? super.getMatch(matchLength - 1) : 0; 077 out.writeInt(high); 078 for (int i = 0; i < matchLength; i++) { 079 out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i)))); 080 } 081 } 082 083 /** 084 * Read needed data (e.g. index) from file. 085 */ 086 @Override 087 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 088 super.initFromFile(bytes, wide); 089 final int low = bytes.readInt(); 090 final int high = bytes.readInt(); 091 final int matchLength = high - low + 1; 092 setMatchLength(matchLength); 093 final short fixedLength = (short) (13 + matchLength * 4); 094 setFixedLength(fixedLength); 095 super.setLength((short) (fixedLength + super.getPadding())); 096 super.setMatches(new int[matchLength]); 097 super.setIndices(new int[matchLength]); 098 super.setTargets(new InstructionHandle[matchLength]); 099 for (int i = 0; i < matchLength; i++) { 100 super.setMatch(i, low + i); 101 super.setIndices(i, bytes.readInt()); 102 } 103 } 104}