1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.generic;
  23 
  24 import java.io.*;
  25 import com.sun.org.apache.bcel.internal.util.ByteSequence;
  26 
  27 /**
  28  * LOOKUPSWITCH - Switch with unordered set of values
  29  *
  30  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  31  * @see SWITCH
  32  */
  33 public class LOOKUPSWITCH extends Select {
  34   /**
  35    * Empty constructor needed for the Class.newInstance() statement in
  36    * Instruction.readInstruction(). Not to be used otherwise.
  37    */
  38   LOOKUPSWITCH() {}
  39 
  40   public LOOKUPSWITCH(int[] match, InstructionHandle[] targets,
  41                       InstructionHandle target) {
  42     super(com.sun.org.apache.bcel.internal.Constants.LOOKUPSWITCH, match, targets, target);
  43 
  44     length = (short)(9 + match_length * 8); /* alignment remainder assumed
  45                                              * 0 here, until dump time. */
  46     fixed_length = length;
  47   }
  48 
  49   /**
  50    * Dump instruction as byte code to stream out.
  51    * @param out Output stream
  52    */
  53   public void dump(DataOutputStream out) throws IOException {
  54     super.dump(out);
  55     out.writeInt(match_length);       // npairs
  56 
  57     for(int i=0; i < match_length; i++) {
  58       out.writeInt(match[i]);         // match-offset pairs
  59       out.writeInt(indices[i] = getTargetOffset(targets[i]));
  60     }
  61   }
  62 
  63   /**
  64    * Read needed data (e.g. index) from file.
  65    */
  66   protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
  67   {
  68     super.initFromFile(bytes, wide); // reads padding
  69 
  70     match_length = bytes.readInt();
  71     fixed_length = (short)(9 + match_length * 8);
  72     length       = (short)(fixed_length + padding);
  73 
  74     match   = new int[match_length];
  75     indices = new int[match_length];
  76     targets = new InstructionHandle[match_length];
  77 
  78     for(int i=0; i < match_length; i++) {
  79       match[i]   = bytes.readInt();
  80       indices[i] = bytes.readInt();
  81     }
  82   }
  83 
  84 
  85   /**
  86    * Call corresponding visitor method(s). The order is:
  87    * Call visitor methods of implemented interfaces first, then
  88    * call methods according to the class hierarchy in descending order,
  89    * i.e., the most specific visitXXX() call comes last.
  90    *
  91    * @param v Visitor object
  92    */
  93   public void accept(Visitor v) {
  94     v.visitVariableLengthInstruction(this);
  95     v.visitStackProducer(this);
  96     v.visitBranchInstruction(this);
  97     v.visitSelect(this);
  98     v.visitLOOKUPSWITCH(this);
  99   }
 100 }