1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import java.util.Objects;
22
23 import org.apache.bcel.Const;
24
25
26
27
28 public final class PUSH implements CompoundInstruction, VariableLengthInstruction, InstructionConstants {
29
30 private final Instruction instruction;
31
32
33
34
35
36
37
38
39 public PUSH(final ConstantPoolGen cp, final ArrayType value) {
40 if (value == null) {
41 instruction = InstructionConst.ACONST_NULL;
42 } else {
43 instruction = new LDC(cp.addArrayClass(value));
44 }
45 }
46
47
48
49
50
51 public PUSH(final ConstantPoolGen cp, final boolean value) {
52 Objects.requireNonNull(cp, "cp");
53 instruction = InstructionConst.getInstruction(Const.ICONST_0 + (value ? 1 : 0));
54 }
55
56
57
58
59
60 public PUSH(final ConstantPoolGen cp, final Boolean value) {
61 this(cp, value.booleanValue());
62 }
63
64
65
66
67
68
69
70
71 public PUSH(final ConstantPoolGen cp, final Character value) {
72 this(cp, value.charValue());
73 }
74
75
76
77
78
79 public PUSH(final ConstantPoolGen cp, final double value) {
80 if (value == 0.0) {
81 instruction = InstructionConst.DCONST_0;
82 } else if (value == 1.0) {
83 instruction = InstructionConst.DCONST_1;
84 } else {
85 instruction = new LDC2_W(cp.addDouble(value));
86 }
87 }
88
89
90
91
92
93 public PUSH(final ConstantPoolGen cp, final float value) {
94 if (value == 0.0) {
95 instruction = InstructionConst.FCONST_0;
96 } else if (value == 1.0) {
97 instruction = InstructionConst.FCONST_1;
98 } else if (value == 2.0) {
99 instruction = InstructionConst.FCONST_2;
100 } else {
101 instruction = new LDC(cp.addFloat(value));
102 }
103 }
104
105
106
107
108
109
110
111 public PUSH(final ConstantPoolGen cp, final int value) {
112 if (value >= -1 && value <= 5) {
113 instruction = InstructionConst.getInstruction(Const.ICONST_0 + value);
114 } else if (Instruction.isValidByte(value)) {
115 instruction = new BIPUSH((byte) value);
116 } else if (Instruction.isValidShort(value)) {
117 instruction = new SIPUSH((short) value);
118 } else {
119 instruction = new LDC(cp.addInteger(value));
120 }
121 }
122
123
124
125
126
127 public PUSH(final ConstantPoolGen cp, final long value) {
128 if (value == 0) {
129 instruction = InstructionConst.LCONST_0;
130 } else if (value == 1) {
131 instruction = InstructionConst.LCONST_1;
132 } else {
133 instruction = new LDC2_W(cp.addLong(value));
134 }
135 }
136
137
138
139
140
141 public PUSH(final ConstantPoolGen cp, final Number value) {
142 if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
143 instruction = new PUSH(cp, value.intValue()).instruction;
144 } else if (value instanceof Double) {
145 instruction = new PUSH(cp, value.doubleValue()).instruction;
146 } else if (value instanceof Float) {
147 instruction = new PUSH(cp, value.floatValue()).instruction;
148 } else if (value instanceof Long) {
149 instruction = new PUSH(cp, value.longValue()).instruction;
150 } else {
151 throw new ClassGenException("What's this: " + value);
152 }
153 }
154
155
156
157
158
159
160
161 public PUSH(final ConstantPoolGen cp, final ObjectType value) {
162 if (value == null) {
163 instruction = InstructionConst.ACONST_NULL;
164 } else {
165 instruction = new LDC(cp.addClass(value));
166 }
167 }
168
169
170
171
172
173 public PUSH(final ConstantPoolGen cp, final String value) {
174 if (value == null) {
175 instruction = InstructionConst.ACONST_NULL;
176 } else {
177 instruction = new LDC(cp.addString(value));
178 }
179 }
180
181 public Instruction getInstruction() {
182 return instruction;
183 }
184
185 @Override
186 public InstructionList getInstructionList() {
187 return new InstructionList(instruction);
188 }
189
190
191
192
193 @Override
194 public String toString() {
195 return instruction + " (PUSH)";
196 }
197 }