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 * BIPUSH - Push byte on stack
026 *
027 * <PRE>
028 * Stack: ... -&gt; ..., value
029 * </PRE>
030 */
031public class BIPUSH extends Instruction implements ConstantPushInstruction {
032
033    private byte b;
034
035    /**
036     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
037     */
038    BIPUSH() {
039    }
040
041    /**
042     * Push byte on stack
043     */
044    public BIPUSH(final byte b) {
045        super(org.apache.bcel.Const.BIPUSH, (short) 2);
046        this.b = b;
047    }
048
049    /**
050     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
051     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
052     *
053     * @param v Visitor object
054     */
055    @Override
056    public void accept(final Visitor v) {
057        v.visitPushInstruction(this);
058        v.visitStackProducer(this);
059        v.visitTypedInstruction(this);
060        v.visitConstantPushInstruction(this);
061        v.visitBIPUSH(this);
062    }
063
064    /**
065     * Dump instruction as byte code to stream out.
066     */
067    @Override
068    public void dump(final DataOutputStream out) throws IOException {
069        super.dump(out);
070        out.writeByte(b);
071    }
072
073    /**
074     * @return Type.BYTE
075     */
076    @Override
077    public Type getType(final ConstantPoolGen cp) {
078        return Type.BYTE;
079    }
080
081    @Override
082    public Number getValue() {
083        return Integer.valueOf(b);
084    }
085
086    /**
087     * Read needed data (e.g. index) from file.
088     */
089    @Override
090    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
091        super.setLength(2);
092        b = bytes.readByte();
093    }
094
095    /**
096     * @return mnemonic for instruction
097     */
098    @Override
099    public String toString(final boolean verbose) {
100        return super.toString(verbose) + " " + b;
101    }
102}