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  import org.apache.bcel.util.ByteSequence;
23  
24  /** 
25   * BIPUSH - Push byte on stack
26   *
27   * <PRE>Stack: ... -&gt; ..., value</PRE>
28   *
29   * @version $Id: BIPUSH.html 898356 2014-02-18 05:44:40Z ggregory $
30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31   */
32  public class BIPUSH extends Instruction implements ConstantPushInstruction {
33  
34      private static final long serialVersionUID = -6859389515217572656L;
35      private byte b;
36  
37  
38      /**
39       * Empty constructor needed for the Class.newInstance() statement in
40       * Instruction.readInstruction(). Not to be used otherwise.
41       */
42      BIPUSH() {
43      }
44  
45  
46      /** Push byte on stack
47       */
48      public BIPUSH(byte b) {
49          super(org.apache.bcel.Constants.BIPUSH, (short) 2);
50          this.b = b;
51      }
52  
53  
54      /**
55       * Dump instruction as byte code to stream out.
56       */
57      @Override
58      public void dump( DataOutputStream out ) throws IOException {
59          super.dump(out);
60          out.writeByte(b);
61      }
62  
63  
64      /**
65       * @return mnemonic for instruction
66       */
67      @Override
68      public String toString( boolean verbose ) {
69          return super.toString(verbose) + " " + b;
70      }
71  
72  
73      /**
74       * Read needed data (e.g. index) from file.
75       */
76      @Override
77      protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
78          length = 2;
79          b = bytes.readByte();
80      }
81  
82  
83      public Number getValue() {
84          return Integer.valueOf(b);
85      }
86  
87  
88      /** @return Type.BYTE
89       */
90      public Type getType( ConstantPoolGen cp ) {
91          return Type.BYTE;
92      }
93  
94  
95      /**
96       * Call corresponding visitor method(s). The order is:
97       * Call visitor methods of implemented interfaces first, then
98       * call methods according to the class hierarchy in descending order,
99       * i.e., the most specific visitXXX() call comes last.
100      *
101      * @param v Visitor object
102      */
103     @Override
104     public void accept( Visitor v ) {
105         v.visitPushInstruction(this);
106         v.visitStackProducer(this);
107         v.visitTypedInstruction(this);
108         v.visitConstantPushInstruction(this);
109         v.visitBIPUSH(this);
110     }
111 }