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 */ 018package org.apache.bcel.generic; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.util.ByteSequence; 024 025/** 026 * TABLESWITCH - Switch within given range of values, i.e., low..high 027 * 028 * @version $Id: TABLESWITCH.java 1812166 2017-10-13 23:48:11Z ggregory $ 029 * @see SWITCH 030 */ 031public class TABLESWITCH extends Select { 032 033 /** 034 * Empty constructor needed for Instruction.readInstruction. 035 * Not to be used otherwise. 036 */ 037 TABLESWITCH() { 038 } 039 040 041 /** 042 * @param match sorted array of match values, match[0] must be low value, 043 * match[match_length - 1] high value 044 * @param targets where to branch for matched values 045 * @param defaultTarget default branch 046 */ 047 public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) { 048 super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget); 049 /* Alignment remainder assumed 0 here, until dump time */ 050 final short _length = (short) (13 + getMatch_length() * 4); 051 super.setLength(_length); 052 setFixed_length(_length); 053 } 054 055 056 /** 057 * Dump instruction as byte code to stream out. 058 * @param out Output stream 059 */ 060 @Override 061 public void dump( final DataOutputStream out ) throws IOException { 062 super.dump(out); 063 final int _match_length = getMatch_length(); 064 final int low = (_match_length > 0) ? super.getMatch(0) : 0; 065 out.writeInt(low); 066 final int high = (_match_length > 0) ? super.getMatch(_match_length - 1) : 0; 067 out.writeInt(high); 068 for (int i = 0; i < _match_length; i++) { 069 out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i)))); 070 } 071 } 072 073 074 /** 075 * Read needed data (e.g. index) from file. 076 */ 077 @Override 078 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { 079 super.initFromFile(bytes, wide); 080 final int low = bytes.readInt(); 081 final int high = bytes.readInt(); 082 final int _match_length = high - low + 1; 083 setMatch_length(_match_length); 084 final short _fixed_length = (short) (13 + _match_length * 4); 085 setFixed_length(_fixed_length); 086 super.setLength((short) (_fixed_length + super.getPadding())); 087 super.setMatches(new int[_match_length]); 088 super.setIndices(new int[_match_length]); 089 super.setTargets(new InstructionHandle[_match_length]); 090 for (int i = 0; i < _match_length; i++) { 091 super.setMatch(i, low + i); 092 super.setIndices(i, bytes.readInt()); 093 } 094 } 095 096 097 /** 098 * Call corresponding visitor method(s). The order is: 099 * 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}