001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.generic; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.util.ByteSequence; 025 026/** 027 * LOOKUPSWITCH - Switch with unordered set of values 028 * 029 * @see SWITCH 030 */ 031public class LOOKUPSWITCH extends Select { 032 033 /** 034 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. 035 */ 036 LOOKUPSWITCH() { 037 } 038 039 public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) { 040 super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget); 041 /* alignment remainder assumed 0 here, until dump time. */ 042 final short length = (short) (9 + getMatchLength() * 8); 043 super.setLength(length); 044 setFixedLength(length); 045 } 046 047 /** 048 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call 049 * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last. 050 * 051 * @param v Visitor object 052 */ 053 @Override 054 public void accept(final Visitor v) { 055 v.visitVariableLengthInstruction(this); 056 v.visitStackConsumer(this); 057 v.visitBranchInstruction(this); 058 v.visitSelect(this); 059 v.visitLOOKUPSWITCH(this); 060 } 061 062 /** 063 * Dump instruction as byte code to stream out. 064 * 065 * @param out Output stream 066 */ 067 @Override 068 public void dump(final DataOutputStream out) throws IOException { 069 super.dump(out); 070 final int matchLength = getMatchLength(); 071 out.writeInt(matchLength); // npairs 072 for (int i = 0; i < matchLength; i++) { 073 out.writeInt(super.getMatch(i)); // match-offset pairs 074 out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i)))); 075 } 076 } 077 078 /** 079 * Reads needed data (for example index) from file. 080 */ 081 @Override 082 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 083 super.initFromFile(bytes, wide); // reads padding 084 final int matchLength = bytes.readInt(); 085 setMatchLength(matchLength); 086 final short fixedLength = (short) (9 + matchLength * 8); 087 setFixedLength(fixedLength); 088 final short length = (short) (matchLength + super.getPadding()); 089 super.setLength(length); 090 super.setMatches(new int[matchLength]); 091 super.setIndices(new int[matchLength]); 092 super.setTargets(new InstructionHandle[matchLength]); 093 for (int i = 0; i < matchLength; i++) { 094 super.setMatch(i, bytes.readInt()); 095 super.setIndices(i, bytes.readInt()); 096 } 097 } 098}