View Javadoc
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   * TABLESWITCH - Switch within given range of values, i.e., low..high
27   *
28   * @see SWITCH
29   */
30  public class TABLESWITCH extends Select {
31  
32      /**
33       * Empty constructor needed for Instruction.readInstruction.
34       * Not to be used otherwise.
35       */
36      TABLESWITCH() {
37      }
38  
39  
40      /**
41       * @param match sorted array of match values, match[0] must be low value,
42       * match[match_length - 1] high value
43       * @param targets where to branch for matched values
44       * @param defaultTarget default branch
45       */
46      public TABLESWITCH(final int[] match, final InstructionHandletml#InstructionHandle">InstructionHandle[] targets, final InstructionHandle defaultTarget) {
47          super(org.apache.bcel.Const.TABLESWITCH, match, targets, defaultTarget);
48          /* Alignment remainder assumed 0 here, until dump time */
49          final short _length = (short) (13 + getMatch_length() * 4);
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          final int low = (_match_length > 0) ? super.getMatch(0) : 0;
64          out.writeInt(low);
65          final int high = (_match_length > 0) ? super.getMatch(_match_length - 1) : 0;
66          out.writeInt(high);
67          for (int i = 0; i < _match_length; i++) {
68              out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
69          }
70      }
71  
72  
73      /**
74       * Read needed data (e.g. index) from file.
75       */
76      @Override
77      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
78          super.initFromFile(bytes, wide);
79          final int low = bytes.readInt();
80          final int high = bytes.readInt();
81          final int _match_length = high - low + 1;
82          setMatch_length(_match_length);
83          final short _fixed_length = (short) (13 + _match_length * 4);
84          setFixed_length(_fixed_length);
85          super.setLength((short) (_fixed_length + super.getPadding()));
86          super.setMatches(new int[_match_length]);
87          super.setIndices(new int[_match_length]);
88          super.setTargets(new InstructionHandle[_match_length]);
89          for (int i = 0; i < _match_length; i++) {
90              super.setMatch(i, low + i);
91              super.setIndices(i, bytes.readInt());
92          }
93      }
94  
95  
96      /**
97       * Call corresponding visitor method(s). The order is:
98       * Call visitor methods of implemented interfaces first, then
99       * call methods according to the class hierarchy in descending order,
100      * i.e., the most specific visitXXX() call comes last.
101      *
102      * @param v Visitor object
103      */
104     @Override
105     public void accept( final Visitor v ) {
106         v.visitVariableLengthInstruction(this);
107         v.visitStackConsumer(this);
108         v.visitBranchInstruction(this);
109         v.visitSelect(this);
110         v.visitTABLESWITCH(this);
111     }
112 }