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 
  21 package com.sun.org.apache.bcel.internal.generic;
  22 
  23 import java.io.DataOutputStream;
  24 import java.io.IOException;
  25 
  26 import com.sun.org.apache.bcel.internal.util.ByteSequence;
  27 
  28 /**
  29  * SIPUSH - Push short
  30  *
  31  * <PRE>Stack: ... -&gt; ..., value</PRE>
  32  *
  33  */
  34 public class SIPUSH extends Instruction implements ConstantPushInstruction {
  35 
  36     private short b;
  37 
  38 
  39     /**
  40      * Empty constructor needed for Instruction.readInstruction.
  41      * Not to be used otherwise.
  42      */
  43     SIPUSH() {
  44     }
  45 
  46 
  47     public SIPUSH(final short b) {
  48         super(com.sun.org.apache.bcel.internal.Const.SIPUSH, (short) 3);
  49         this.b = b;
  50     }
  51 
  52 
  53     /**
  54      * Dump instruction as short code to stream out.
  55      */
  56     @Override
  57     public void dump( final DataOutputStream out ) throws IOException {
  58         super.dump(out);
  59         out.writeShort(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(3);
  78         b = bytes.readShort();
  79     }
  80 
  81 
  82     @Override
  83     public Number getValue() {
  84         return Integer.valueOf(b);
  85     }
  86 
  87 
  88     /** @return Type.SHORT
  89      */
  90     @Override
  91     public Type getType( final ConstantPoolGen cp ) {
  92         return Type.SHORT;
  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.visitSIPUSH(this);
 111     }
 112 }