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