LOOKUPSWITCH.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.  * LOOKUPSWITCH - Switch with unordered set of values
  23.  *
  24.  * @see SWITCH
  25.  */
  26. public class LOOKUPSWITCH extends Select {

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

  32.     public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
  33.         super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget);
  34.         /* alignment remainder assumed 0 here, until dump time. */
  35.         final short length = (short) (9 + getMatchLength() * 8);
  36.         super.setLength(length);
  37.         setFixedLength(length);
  38.     }

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

  53.     /**
  54.      * Dump instruction as byte code to stream out.
  55.      *
  56.      * @param out Output stream
  57.      */
  58.     @Override
  59.     public void dump(final DataOutputStream out) throws IOException {
  60.         super.dump(out);
  61.         final int matchLength = getMatchLength();
  62.         out.writeInt(matchLength); // npairs
  63.         for (int i = 0; i < matchLength; i++) {
  64.             out.writeInt(super.getMatch(i)); // match-offset pairs
  65.             out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
  66.         }
  67.     }

  68.     /**
  69.      * Read needed data (e.g. index) from file.
  70.      */
  71.     @Override
  72.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  73.         super.initFromFile(bytes, wide); // reads padding
  74.         final int matchLength = bytes.readInt();
  75.         setMatchLength(matchLength);
  76.         final short fixedLength = (short) (9 + matchLength * 8);
  77.         setFixedLength(fixedLength);
  78.         final short length = (short) (matchLength + super.getPadding());
  79.         super.setLength(length);
  80.         super.setMatches(new int[matchLength]);
  81.         super.setIndices(new int[matchLength]);
  82.         super.setTargets(new InstructionHandle[matchLength]);
  83.         for (int i = 0; i < matchLength; i++) {
  84.             super.setMatch(i, bytes.readInt());
  85.             super.setIndices(i, bytes.readInt());
  86.         }
  87.     }
  88. }