1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package com.sun.org.apache.bcel.internal.generic;
  21 
  22 import java.io.DataOutputStream;
  23 import java.io.IOException;
  24 
  25 import com.sun.org.apache.bcel.internal.util.ByteSequence;
  26 
  27 /**
  28  * BIPUSH - Push byte on stack
  29  *
  30  * <PRE>Stack: ... -&gt; ..., value</PRE>
  31  *
  32  * @version $Id$
  33  */
  34 public class BIPUSH extends Instruction implements ConstantPushInstruction {
  35 
  36     private byte b;
  37 
  38 
  39     /**
  40      * Empty constructor needed for Instruction.readInstruction.
  41      * Not to be used otherwise.
  42      */
  43     BIPUSH() {
  44     }
  45 
  46 
  47     /** Push byte on stack
  48      */
  49     public BIPUSH(final byte b) {
  50         super(com.sun.org.apache.bcel.internal.Const.BIPUSH, (short) 2);
  51         this.b = b;
  52     }
  53 
  54 
  55     /**
  56      * Dump instruction as byte code to stream out.
  57      */
  58     @Override
  59     public void dump( final DataOutputStream out ) throws IOException {
  60         super.dump(out);
  61         out.writeByte(b);
  62     }
  63 
  64 
  65     /**
  66      * @return mnemonic for instruction
  67      */
  68     @Override
  69     public String toString( final boolean verbose ) {
  70         return super.toString(verbose) + " " + b;
  71     }
  72 
  73 
  74     /**
  75      * Read needed data (e.g. index) from file.
  76      */
  77     @Override
  78     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
  79         super.setLength(2);
  80         b = bytes.readByte();
  81     }
  82 
  83 
  84     @Override
  85     public Number getValue() {
  86         return Integer.valueOf(b);
  87     }
  88 
  89 
  90     /** @return Type.BYTE
  91      */
  92     @Override
  93     public Type getType( final ConstantPoolGen cp ) {
  94         return Type.BYTE;
  95     }
  96 
  97 
  98     /**
  99      * Call corresponding visitor method(s). The order is:
 100      * Call visitor methods of implemented interfaces first, then
 101      * call methods according to the class hierarchy in descending order,
 102      * i.e., the most specific visitXXX() call comes last.
 103      *
 104      * @param v Visitor object
 105      */
 106     @Override
 107     public void accept( final Visitor v ) {
 108         v.visitPushInstruction(this);
 109         v.visitStackProducer(this);
 110         v.visitTypedInstruction(this);
 111         v.visitConstantPushInstruction(this);
 112         v.visitBIPUSH(this);
 113     }
 114 }