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.DataOutputStream;
  25 import java.io.IOException;
  26 
  27 import com.sun.org.apache.bcel.internal.util.ByteSequence;
  28 
  29 /**
  30  * LOOKUPSWITCH - Switch with unordered set of values
  31  *
  32  * @version $Id$
  33  * @see SWITCH
  34  */
  35 public class LOOKUPSWITCH extends Select {
  36 
  37     /**
  38      * Empty constructor needed for Instruction.readInstruction.
  39      * Not to be used otherwise.
  40      */
  41     LOOKUPSWITCH() {
  42     }
  43 
  44 
  45     public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets,
  46             final InstructionHandle defaultTarget) {
  47         super(com.sun.org.apache.bcel.internal.Const.LOOKUPSWITCH, match, targets, defaultTarget);
  48         /* alignment remainder assumed 0 here, until dump time. */
  49         final short _length = (short) (9 + getMatch_length() * 8);
  50         super.setLength(_length);
  51         setFixed_length(_length);
  52     }
  53 
  54 
  55     /**
  56      * Dump instruction as byte code to stream out.
  57      * @param out Output stream
  58      */
  59     @Override
  60     public void dump( final DataOutputStream out ) throws IOException {
  61         super.dump(out);
  62         final int _match_length = getMatch_length();
  63         out.writeInt(_match_length); // npairs
  64         for (int i = 0; i < _match_length; i++) {
  65             out.writeInt(super.getMatch(i)); // match-offset pairs
  66             out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
  67         }
  68     }
  69 
  70 
  71     /**
  72      * Read needed data (e.g. index) from file.
  73      */
  74     @Override
  75     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
  76         super.initFromFile(bytes, wide); // reads padding
  77         final int _match_length = bytes.readInt();
  78         setMatch_length(_match_length);
  79         final short _fixed_length = (short) (9 + _match_length * 8);
  80         setFixed_length(_fixed_length);
  81         final short _length = (short) (_match_length + super.getPadding());
  82         super.setLength(_length);
  83         super.setMatches(new int[_match_length]);
  84         super.setIndices(new int[_match_length]);
  85         super.setTargets(new InstructionHandle[_match_length]);
  86         for (int i = 0; i < _match_length; i++) {
  87             super.setMatch(i, bytes.readInt());
  88             super.setIndices(i, bytes.readInt());
  89         }
  90     }
  91 
  92 
  93     /**
  94      * Call corresponding visitor method(s). The order is:
  95      * Call visitor methods of implemented interfaces first, then
  96      * call methods according to the class hierarchy in descending order,
  97      * i.e., the most specific visitXXX() call comes last.
  98      *
  99      * @param v Visitor object
 100      */
 101     @Override
 102     public void accept( final Visitor v ) {
 103         v.visitVariableLengthInstruction(this);
 104         v.visitStackConsumer(this);
 105         v.visitBranchInstruction(this);
 106         v.visitSelect(this);
 107         v.visitLOOKUPSWITCH(this);
 108     }
 109 }