001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.Const;
025import org.apache.bcel.util.ByteSequence;
026
027/**
028 * IINC - Increment local variable by constant
029 */
030public class IINC extends LocalVariableInstruction {
031
032    private boolean wide;
033    private int c;
034
035    /**
036     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
037     */
038    IINC() {
039    }
040
041    /**
042     * Constructs an IINC instruction.
043     *
044     * @param n index of local variable.
045     * @param c increment factor.
046     */
047    public IINC(final int n, final int c) {
048        // Default behavior of LocalVariableInstruction causes error
049        super.setOpcode(Const.IINC);
050        super.setLength((short) 3);
051        setIndex(n); // May set wide as side effect
052        setIncrement(c);
053    }
054
055    /**
056     * Calls corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
057     * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
058     *
059     * @param v Visitor object.
060     */
061    @Override
062    public void accept(final Visitor v) {
063        v.visitLocalVariableInstruction(this);
064        v.visitIINC(this);
065    }
066
067    /**
068     * Dumps instruction as byte code to stream out.
069     *
070     * @param out Output stream.
071     */
072    @Override
073    public void dump(final DataOutputStream out) throws IOException {
074        if (wide) {
075            out.writeByte(Const.WIDE);
076        }
077        out.writeByte(super.getOpcode());
078        if (wide) {
079            out.writeShort(super.getIndex());
080            out.writeShort(c);
081        } else {
082            out.writeByte(super.getIndex());
083            out.writeByte(c);
084        }
085    }
086
087    /**
088     * Gets the increment factor.
089     *
090     * @return increment factor.
091     */
092    public final int getIncrement() {
093        return c;
094    }
095
096    /**
097     * Gets the int type.
098     *
099     * @return int type.
100     */
101    @Override
102    public Type getType(final ConstantPoolGen cp) {
103        return Type.INT;
104    }
105
106    /**
107     * Reads needed data (for example index) from file.
108     */
109    @Override
110    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
111        this.wide = wide;
112        if (wide) {
113            super.setLength(6);
114            super.setIndexOnly(bytes.readUnsignedShort());
115            c = bytes.readShort();
116        } else {
117            super.setLength(3);
118            super.setIndexOnly(bytes.readUnsignedByte());
119            c = bytes.readByte();
120        }
121    }
122
123    /**
124     * Sets increment factor.
125     *
126     * @param c The increment factor.
127     * @throws ClassGenException Thrown if the increment is out of bounds.
128     */
129    public final void setIncrement(final int c) {
130        if (!isValidShort(c)) {
131            throw new ClassGenException("Illegal increment: " + c);
132        }
133        this.c = c;
134        setWide();
135    }
136
137    /**
138     * Sets index of local variable.
139     *
140     * @param index index of local variable.
141     * @throws ClassGenException Thrown if index is out of bounds.
142     */
143    @Override
144    public final void setIndex(final int index) {
145        if (!isNonNegativeUShort(index)) {
146            throw new ClassGenException("Illegal index: " + index);
147        }
148        super.setIndexOnly(index);
149        setWide();
150    }
151
152    private void setWide() {
153        wide = super.getIndex() > Const.MAX_BYTE;
154        if (c > 0) {
155            wide = wide || c > Byte.MAX_VALUE;
156        } else {
157            wide = wide || c < Byte.MIN_VALUE;
158        }
159        if (wide) {
160            super.setLength(6); // wide byte included
161        } else {
162            super.setLength(3);
163        }
164    }
165
166    /**
167     * Returns mnemonic for instruction.
168     *
169     * @return mnemonic for instruction.
170     */
171    @Override
172    public String toString(final boolean verbose) {
173        return super.toString(verbose) + " " + c;
174    }
175}