001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.generic;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022import org.apache.bcel.util.ByteSequence;
023
024/**
025 * SIPUSH - Push short
026 *
027 * <PRE>
028 * Stack: ... -&gt; ..., value
029 * </PRE>
030 */
031public class SIPUSH extends Instruction implements ConstantPushInstruction {
032
033    private short b;
034
035    /**
036     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
037     */
038    SIPUSH() {
039    }
040
041    public SIPUSH(final short b) {
042        super(org.apache.bcel.Const.SIPUSH, (short) 3);
043        this.b = b;
044    }
045
046    /**
047     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
048     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
049     *
050     * @param v Visitor object
051     */
052    @Override
053    public void accept(final Visitor v) {
054        v.visitPushInstruction(this);
055        v.visitStackProducer(this);
056        v.visitTypedInstruction(this);
057        v.visitConstantPushInstruction(this);
058        v.visitSIPUSH(this);
059    }
060
061    /**
062     * Dump instruction as short code to stream out.
063     */
064    @Override
065    public void dump(final DataOutputStream out) throws IOException {
066        super.dump(out);
067        out.writeShort(b);
068    }
069
070    /**
071     * @return Type.SHORT
072     */
073    @Override
074    public Type getType(final ConstantPoolGen cp) {
075        return Type.SHORT;
076    }
077
078    @Override
079    public Number getValue() {
080        return Integer.valueOf(b);
081    }
082
083    /**
084     * Read needed data (e.g. index) from file.
085     */
086    @Override
087    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
088        super.setLength(3);
089        b = bytes.readShort();
090    }
091
092    /**
093     * @return mnemonic for instruction
094     */
095    @Override
096    public String toString(final boolean verbose) {
097        return super.toString(verbose) + " " + b;
098    }
099}