TABLESWITCH.java

  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. package org.apache.bcel.generic;

  18. import java.io.DataOutputStream;
  19. import java.io.IOException;

  20. import org.apache.bcel.util.ByteSequence;

  21. /**
  22.  * TABLESWITCH - Switch within given range of values, i.e., low..high
  23.  *
  24.  * @see SWITCH
  25.  */
  26. public class TABLESWITCH extends Select {

  27.     /**
  28.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  29.      */
  30.     TABLESWITCH() {
  31.     }

  32.     /**
  33.      * @param match sorted array of match values, match[0] must be low value, match[match_length - 1] high value
  34.      * @param targets where to branch for matched values
  35.      * @param defaultTarget default branch
  36.      */
  37.     public TABLESWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
  38.         super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget);
  39.         /* Alignment remainder assumed 0 here, until dump time */
  40.         final short length = (short) (13 + getMatchLength() * 4);
  41.         super.setLength(length);
  42.         setFixedLength(length);
  43.     }

  44.     /**
  45.      * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
  46.      * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
  47.      *
  48.      * @param v Visitor object
  49.      */
  50.     @Override
  51.     public void accept(final Visitor v) {
  52.         v.visitVariableLengthInstruction(this);
  53.         v.visitStackConsumer(this);
  54.         v.visitBranchInstruction(this);
  55.         v.visitSelect(this);
  56.         v.visitTABLESWITCH(this);
  57.     }

  58.     /**
  59.      * Dump instruction as byte code to stream out.
  60.      *
  61.      * @param out Output stream
  62.      */
  63.     @Override
  64.     public void dump(final DataOutputStream out) throws IOException {
  65.         super.dump(out);
  66.         final int matchLength = getMatchLength();
  67.         final int low = matchLength > 0 ? super.getMatch(0) : 0;
  68.         out.writeInt(low);
  69.         final int high = matchLength > 0 ? super.getMatch(matchLength - 1) : 0;
  70.         out.writeInt(high);
  71.         for (int i = 0; i < matchLength; i++) {
  72.             out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
  73.         }
  74.     }

  75.     /**
  76.      * Read needed data (e.g. index) from file.
  77.      */
  78.     @Override
  79.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  80.         super.initFromFile(bytes, wide);
  81.         final int low = bytes.readInt();
  82.         final int high = bytes.readInt();
  83.         final int matchLength = high - low + 1;
  84.         setMatchLength(matchLength);
  85.         final short fixedLength = (short) (13 + matchLength * 4);
  86.         setFixedLength(fixedLength);
  87.         super.setLength((short) (fixedLength + super.getPadding()));
  88.         super.setMatches(new int[matchLength]);
  89.         super.setIndices(new int[matchLength]);
  90.         super.setTargets(new InstructionHandle[matchLength]);
  91.         for (int i = 0; i < matchLength; i++) {
  92.             super.setMatch(i, low + i);
  93.             super.setIndices(i, bytes.readInt());
  94.         }
  95.     }
  96. }