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  package org.apache.bcel.generic;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.bcel.Const;
23  import org.apache.bcel.util.ByteSequence;
24  
25  /**
26   * IINC - Increment local variable by constant
27   */
28  public class IINC extends LocalVariableInstruction {
29  
30      private boolean wide;
31      private int c;
32  
33      /**
34       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
35       */
36      IINC() {
37      }
38  
39      /**
40       * @param n index of local variable
41       * @param c increment factor
42       */
43      public IINC(final int n, final int c) {
44          // Default behavior of LocalVariableInstruction causes error
45          super.setOpcode(Const.IINC);
46          super.setLength((short) 3);
47          setIndex(n); // May set wide as side effect
48          setIncrement(c);
49      }
50  
51      /**
52       * Calls corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
53       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
54       *
55       * @param v Visitor object
56       */
57      @Override
58      public void accept(final Visitor v) {
59          v.visitLocalVariableInstruction(this);
60          v.visitIINC(this);
61      }
62  
63      /**
64       * Dumps instruction as byte code to stream out.
65       *
66       * @param out Output stream
67       */
68      @Override
69      public void dump(final DataOutputStream out) throws IOException {
70          if (wide) {
71              out.writeByte(Const.WIDE);
72          }
73          out.writeByte(super.getOpcode());
74          if (wide) {
75              out.writeShort(super.getIndex());
76              out.writeShort(c);
77          } else {
78              out.writeByte(super.getIndex());
79              out.writeByte(c);
80          }
81      }
82  
83      /**
84       * @return increment factor
85       */
86      public final int getIncrement() {
87          return c;
88      }
89  
90      /**
91       * @return int type
92       */
93      @Override
94      public Type getType(final ConstantPoolGen cp) {
95          return Type.INT;
96      }
97  
98      /**
99       * Reads needed data (e.g. index) from file.
100      */
101     @Override
102     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
103         this.wide = wide;
104         if (wide) {
105             super.setLength(6);
106             super.setIndexOnly(bytes.readUnsignedShort());
107             c = bytes.readShort();
108         } else {
109             super.setLength(3);
110             super.setIndexOnly(bytes.readUnsignedByte());
111             c = bytes.readByte();
112         }
113     }
114 
115     /**
116      * Sets increment factor.
117      */
118     public final void setIncrement(final int c) {
119         this.c = c;
120         setWide();
121     }
122 
123     /**
124      * Sets index of local variable.
125      */
126     @Override
127     public final void setIndex(final int n) {
128         if (n < 0) {
129             throw new ClassGenException("Negative index value: " + n);
130         }
131         super.setIndexOnly(n);
132         setWide();
133     }
134 
135     private void setWide() {
136         wide = super.getIndex() > Const.MAX_BYTE;
137         if (c > 0) {
138             wide = wide || c > Byte.MAX_VALUE;
139         } else {
140             wide = wide || c < Byte.MIN_VALUE;
141         }
142         if (wide) {
143             super.setLength(6); // wide byte included
144         } else {
145             super.setLength(3);
146         }
147     }
148 
149     /**
150      * Returns mnemonic for instruction.
151      *
152      * @return mnemonic for instruction.
153      */
154     @Override
155     public String toString(final boolean verbose) {
156         return super.toString(verbose) + " " + c;
157     }
158 }