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
52
53 public PUSH(final ConstantPoolGen cp, final boolean value) {
54 Objects.requireNonNull(cp, "cp");
55 instruction = InstructionConst.getInstruction(Const.ICONST_0 + (value ? 1 : 0));
56 }
57
58
59
60
61
62
63
64 public PUSH(final ConstantPoolGen cp, final Boolean value) {
65 this(cp, value.booleanValue());
66 }
67
68
69
70
71
72
73
74
75 public PUSH(final ConstantPoolGen cp, final Character value) {
76 this(cp, value.charValue());
77 }
78
79
80
81
82
83
84
85 public PUSH(final ConstantPoolGen cp, final double value) {
86 if (value == 0.0) {
87 instruction = InstructionConst.DCONST_0;
88 } else if (value == 1.0) {
89 instruction = InstructionConst.DCONST_1;
90 } else {
91 instruction = new LDC2_W(cp.addDouble(value));
92 }
93 }
94
95
96
97
98
99
100
101 public PUSH(final ConstantPoolGen cp, final float value) {
102 if (value == 0.0) {
103 instruction = InstructionConst.FCONST_0;
104 } else if (value == 1.0) {
105 instruction = InstructionConst.FCONST_1;
106 } else if (value == 2.0) {
107 instruction = InstructionConst.FCONST_2;
108 } else {
109 instruction = new LDC(cp.addFloat(value));
110 }
111 }
112
113
114
115
116
117
118
119 public PUSH(final ConstantPoolGen cp, final int value) {
120 if (value >= -1 && value <= 5) {
121 instruction = InstructionConst.getInstruction(Const.ICONST_0 + value);
122 } else if (Instruction.isValidByte(value)) {
123 instruction = new BIPUSH((byte) value);
124 } else if (Instruction.isValidShort(value)) {
125 instruction = new SIPUSH((short) value);
126 } else {
127 instruction = new LDC(cp.addInteger(value));
128 }
129 }
130
131
132
133
134
135
136
137 public PUSH(final ConstantPoolGen cp, final long value) {
138 if (value == 0) {
139 instruction = InstructionConst.LCONST_0;
140 } else if (value == 1) {
141 instruction = InstructionConst.LCONST_1;
142 } else {
143 instruction = new LDC2_W(cp.addLong(value));
144 }
145 }
146
147
148
149
150
151
152
153 public PUSH(final ConstantPoolGen cp, final Number value) {
154 if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
155 instruction = new PUSH(cp, value.intValue()).instruction;
156 } else if (value instanceof Double) {
157 instruction = new PUSH(cp, value.doubleValue()).instruction;
158 } else if (value instanceof Float) {
159 instruction = new PUSH(cp, value.floatValue()).instruction;
160 } else if (value instanceof Long) {
161 instruction = new PUSH(cp, value.longValue()).instruction;
162 } else {
163 throw new ClassGenException("What's this: " + value);
164 }
165 }
166
167
168
169
170
171
172
173
174 public PUSH(final ConstantPoolGen cp, final ObjectType value) {
175 if (value == null) {
176 instruction = InstructionConst.ACONST_NULL;
177 } else {
178 instruction = new LDC(cp.addClass(value));
179 }
180 }
181
182
183
184
185
186
187
188 public PUSH(final ConstantPoolGen cp, final String value) {
189 if (value == null) {
190 instruction = InstructionConst.ACONST_NULL;
191 } else {
192 instruction = new LDC(cp.addString(value));
193 }
194 }
195
196
197
198
199
200
201 public Instruction getInstruction() {
202 return instruction;
203 }
204
205 @Override
206 public InstructionList getInstructionList() {
207 return new InstructionList(instruction);
208 }
209
210
211
212
213 @Override
214 public String toString() {
215 return instruction + " (PUSH)";
216 }
217 }