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