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
21package com.sun.org.apache.bcel.internal.generic;
22
23import java.io.DataOutputStream;
24import java.io.IOException;
25
26import com.sun.org.apache.bcel.internal.util.ByteSequence;
27
28/**
29 * SIPUSH - Push short
30 *
31 * <PRE>Stack: ... -&gt; ..., value</PRE>
32 *
33 * @version $Id: SIPUSH.java 1747278 2016-06-07 17:28:43Z britter $
34 */
35public class SIPUSH extends Instruction implements ConstantPushInstruction {
36
37    private short b;
38
39
40    /**
41     * Empty constructor needed for the Class.newInstance() statement in
42     * Instruction.readInstruction(). Not to be used otherwise.
43     */
44    SIPUSH() {
45    }
46
47
48    public SIPUSH(final short b) {
49        super(com.sun.org.apache.bcel.internal.Const.SIPUSH, (short) 3);
50        this.b = b;
51    }
52
53
54    /**
55     * Dump instruction as short code to stream out.
56     */
57    @Override
58    public void dump( final DataOutputStream out ) throws IOException {
59        super.dump(out);
60        out.writeShort(b);
61    }
62
63
64    /**
65     * @return mnemonic for instruction
66     */
67    @Override
68    public String toString( final 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( final ByteSequence bytes, final boolean wide ) throws IOException {
78        super.setLength(3);
79        b = bytes.readShort();
80    }
81
82
83    @Override
84    public Number getValue() {
85        return Integer.valueOf(b);
86    }
87
88
89    /** @return Type.SHORT
90     */
91    @Override
92    public Type getType( final ConstantPoolGen cp ) {
93        return Type.SHORT;
94    }
95
96
97    /**
98     * Call corresponding visitor method(s). The order is:
99     * Call visitor methods of implemented interfaces first, then
100     * call methods according to the class hierarchy in descending order,
101     * i.e., the most specific visitXXX() call comes last.
102     *
103     * @param v Visitor object
104     */
105    @Override
106    public void accept( final Visitor v ) {
107        v.visitPushInstruction(this);
108        v.visitStackProducer(this);
109        v.visitTypedInstruction(this);
110        v.visitConstantPushInstruction(this);
111        v.visitSIPUSH(this);
112    }
113}
114