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 */ 128 public final void setIncrement(final int c) { 129 this.c = c; 130 setWide(); 131 } 132 133 /** 134 * Sets index of local variable. 135 */ 136 @Override 137 public final void setIndex(final int n) { 138 if (n < 0) { 139 throw new ClassGenException("Negative index value: " + n); 140 } 141 super.setIndexOnly(n); 142 setWide(); 143 } 144 145 private void setWide() { 146 wide = super.getIndex() > Const.MAX_BYTE; 147 if (c > 0) { 148 wide = wide || c > Byte.MAX_VALUE; 149 } else { 150 wide = wide || c < Byte.MIN_VALUE; 151 } 152 if (wide) { 153 super.setLength(6); // wide byte included 154 } else { 155 super.setLength(3); 156 } 157 } 158 159 /** 160 * Returns mnemonic for instruction. 161 * 162 * @return mnemonic for instruction. 163 */ 164 @Override 165 public String toString(final boolean verbose) { 166 return super.toString(verbose) + " " + c; 167 } 168}