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