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.io.IOException;
22 import java.io.OutputStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26
27 import org.apache.bcel.Const;
28 import org.apache.bcel.classfile.StackMap;
29 import org.apache.bcel.classfile.StackMapEntry;
30 import org.apache.bcel.classfile.StackMapType;
31 import org.apache.commons.lang.ArrayUtils;
32
33 public class BinaryOpCreator {
34
35 private static final String ORG_APACHE_BCEL_GENERIC_BINARY_OP = "org.apache.bcel.generic.BinaryOp";
36
37 public static void main(final String[] args) throws Exception {
38 final BinaryOpCreator creator = new BinaryOpCreator();
39 final Path path = Paths.get("target/test-classes/org/apache/bcel/generic/BinaryOp.class");
40 Files.deleteIfExists(path);
41 try (OutputStream out = Files.newOutputStream(path)) {
42 creator.create(out);
43 }
44 }
45
46 private final InstructionFactory factory;
47 private final ConstantPoolGen cp;
48
49 private final ClassGen cg;
50
51 public BinaryOpCreator() {
52 cg = new ClassGen(ORG_APACHE_BCEL_GENERIC_BINARY_OP, "java.lang.Object", "BinaryOp.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
53 ArrayUtils.EMPTY_STRING_ARRAY);
54 cg.setMajor(Const.MAJOR_1_8);
55 cg.setMinor(Const.MINOR_1_8);
56
57 cp = cg.getConstantPool();
58 factory = new InstructionFactory(cg, cp);
59 }
60
61 public void create(final OutputStream out) throws IOException {
62 createConstructor();
63 createMethodISUB();
64 createMethodIADD();
65 createMethodIREM();
66 createMethodIMUL();
67 createMethodIDIV();
68 createMethodIAND();
69 createMethodIOR();
70 createMethodIXOR();
71 createMethodISHL();
72 createMethodISHR();
73 createMethodIUSHR();
74 createMethodLSUB();
75 createMethodLADD();
76 createMethodLREM();
77 createMethodLMUL();
78 createMethodLDIV();
79 createMethodLAND();
80 createMethodLOR();
81 createMethodLXOR();
82 createMethodLSHL();
83 createMethodLSHR();
84 createMethodLUSHR();
85 createMethodFSUB();
86 createMethodFADD();
87 createMethodFREM();
88 createMethodFMUL();
89 createMethodFDIV();
90 createMethodDSUB();
91 createMethodDADD();
92 createMethodDREM();
93 createMethodDMUL();
94 createMethodDDIV();
95 createMethodCalculate();
96 createMethodMain();
97 cg.getJavaClass().dump(out);
98 }
99
100 private void createConstructor() {
101 final InstructionList il = new InstructionList();
102 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, ArrayUtils.EMPTY_STRING_ARRAY, "<init>",
103 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
104
105 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
106 il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
107 il.append(InstructionFactory.createReturn(Type.VOID));
108 method.setMaxStack();
109 method.setMaxLocals();
110 cg.addMethod(method.getMethod());
111 il.dispose();
112 }
113
114 private void createMethodCalculate() {
115 final InstructionList il = new InstructionList();
116 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.OBJECT, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] { "args" },
117 "calculate", ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
118 method.addException("java.lang.Exception");
119 final StackMapType[] typesOfLocals = { new StackMapType((byte) 7, cp.addClass("java.lang.String"), cp.getConstantPool()) };
120 final StackMapEntry[] table = { new StackMapEntry(252, 70, typesOfLocals, null, cp.getConstantPool()),
121 new StackMapEntry(251, 65, null, null, cp.getConstantPool()),
122 new StackMapEntry(251, 65, null, null, cp.getConstantPool()),
123 new StackMapEntry(251, 65, null, null, cp.getConstantPool()) };
124 final StackMap stackMap = new StackMap(cp.addUtf8("StackMapTable"), 17, table, cp.getConstantPool());
125 stackMap.setStackMap(table);
126 method.addCodeAttribute(stackMap);
127
128 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
129 il.append(new PUSH(cp, 0));
130 il.append(InstructionConst.AALOAD);
131 il.append(InstructionFactory.createStore(Type.OBJECT, 2));
132 il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
133 il.append(new PUSH(cp, "i"));
134 il.append(factory.createInvoke("java.lang.String", "startsWith", Type.BOOLEAN, new Type[] { Type.STRING }, Const.INVOKEVIRTUAL));
135 final BranchInstruction ifeq1 = InstructionFactory.createBranchInstruction(Const.IFEQ, null);
136 il.append(ifeq1);
137 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
138 il.append(factory.createInvoke("java.lang.Object", "getClass", new ObjectType("java.lang.Class"), Type.NO_ARGS, Const.INVOKEVIRTUAL));
139 il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
140 il.append(new PUSH(cp, 2));
141 il.append(factory.createNewArray(new ObjectType("java.lang.Class"), (short) 1));
142 il.append(InstructionConst.DUP);
143 il.append(new PUSH(cp, 0));
144 il.append(factory.createFieldAccess("java.lang.Integer", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
145 il.append(InstructionConst.AASTORE);
146 il.append(InstructionConst.DUP);
147 il.append(new PUSH(cp, 1));
148 il.append(factory.createFieldAccess("java.lang.Integer", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
149 il.append(InstructionConst.AASTORE);
150 il.append(factory.createInvoke("java.lang.Class", "getMethod", new ObjectType("java.lang.reflect.Method"),
151 new Type[] { Type.STRING, new ArrayType(new ObjectType("java.lang.Class"), 1) }, Const.INVOKEVIRTUAL));
152 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
153 il.append(new PUSH(cp, 2));
154 il.append(factory.createNewArray(Type.OBJECT, (short) 1));
155 il.append(InstructionConst.DUP);
156 il.append(new PUSH(cp, 0));
157 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
158 il.append(new PUSH(cp, 1));
159 il.append(InstructionConst.AALOAD);
160 il.append(factory.createInvoke("java.lang.Integer", "parseInt", Type.INT, new Type[] { Type.STRING }, Const.INVOKESTATIC));
161 il.append(factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Const.INVOKESTATIC));
162 il.append(InstructionConst.AASTORE);
163 il.append(InstructionConst.DUP);
164 il.append(new PUSH(cp, 1));
165 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
166 il.append(new PUSH(cp, 2));
167 il.append(InstructionConst.AALOAD);
168 il.append(factory.createInvoke("java.lang.Integer", "parseInt", Type.INT, new Type[] { Type.STRING }, Const.INVOKESTATIC));
169 il.append(factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Const.INVOKESTATIC));
170 il.append(InstructionConst.AASTORE);
171 il.append(factory.createInvoke("java.lang.reflect.Method", "invoke", Type.OBJECT, new Type[] { Type.OBJECT, new ArrayType(Type.OBJECT, 1) },
172 Const.INVOKEVIRTUAL));
173 il.append(InstructionFactory.createReturn(Type.OBJECT));
174 final InstructionHandle ih1 = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
175 il.append(new PUSH(cp, "l"));
176 il.append(factory.createInvoke("java.lang.String", "startsWith", Type.BOOLEAN, new Type[] { Type.STRING }, Const.INVOKEVIRTUAL));
177 final BranchInstruction ifeq2 = InstructionFactory.createBranchInstruction(Const.IFEQ, null);
178 il.append(ifeq2);
179 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
180 il.append(factory.createInvoke("java.lang.Object", "getClass", new ObjectType("java.lang.Class"), Type.NO_ARGS, Const.INVOKEVIRTUAL));
181 il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
182 il.append(new PUSH(cp, 2));
183 il.append(factory.createNewArray(new ObjectType("java.lang.Class"), (short) 1));
184 il.append(InstructionConst.DUP);
185 il.append(new PUSH(cp, 0));
186 il.append(factory.createFieldAccess("java.lang.Long", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
187 il.append(InstructionConst.AASTORE);
188 il.append(InstructionConst.DUP);
189 il.append(new PUSH(cp, 1));
190 il.append(factory.createFieldAccess("java.lang.Long", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
191 il.append(InstructionConst.AASTORE);
192 il.append(factory.createInvoke("java.lang.Class", "getMethod", new ObjectType("java.lang.reflect.Method"),
193 new Type[] { Type.STRING, new ArrayType(new ObjectType("java.lang.Class"), 1) }, Const.INVOKEVIRTUAL));
194 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
195 il.append(new PUSH(cp, 2));
196 il.append(factory.createNewArray(Type.OBJECT, (short) 1));
197 il.append(InstructionConst.DUP);
198 il.append(new PUSH(cp, 0));
199 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
200 il.append(new PUSH(cp, 1));
201 il.append(InstructionConst.AALOAD);
202 il.append(factory.createInvoke("java.lang.Long", "parseLong", Type.LONG, new Type[] { Type.STRING }, Const.INVOKESTATIC));
203 il.append(factory.createInvoke("java.lang.Long", "valueOf", new ObjectType("java.lang.Long"), new Type[] { Type.LONG }, Const.INVOKESTATIC));
204 il.append(InstructionConst.AASTORE);
205 il.append(InstructionConst.DUP);
206 il.append(new PUSH(cp, 1));
207 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
208 il.append(new PUSH(cp, 2));
209 il.append(InstructionConst.AALOAD);
210 il.append(factory.createInvoke("java.lang.Long", "parseLong", Type.LONG, new Type[] { Type.STRING }, Const.INVOKESTATIC));
211 il.append(factory.createInvoke("java.lang.Long", "valueOf", new ObjectType("java.lang.Long"), new Type[] { Type.LONG }, Const.INVOKESTATIC));
212 il.append(InstructionConst.AASTORE);
213 il.append(factory.createInvoke("java.lang.reflect.Method", "invoke", Type.OBJECT, new Type[] { Type.OBJECT, new ArrayType(Type.OBJECT, 1) },
214 Const.INVOKEVIRTUAL));
215 il.append(InstructionFactory.createReturn(Type.OBJECT));
216 final InstructionHandle ih2 = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
217 il.append(new PUSH(cp, "f"));
218 il.append(factory.createInvoke("java.lang.String", "startsWith", Type.BOOLEAN, new Type[] { Type.STRING }, Const.INVOKEVIRTUAL));
219 final BranchInstruction ifeq3 = InstructionFactory.createBranchInstruction(Const.IFEQ, null);
220 il.append(ifeq3);
221 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
222 il.append(factory.createInvoke("java.lang.Object", "getClass", new ObjectType("java.lang.Class"), Type.NO_ARGS, Const.INVOKEVIRTUAL));
223 il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
224 il.append(new PUSH(cp, 2));
225 il.append(factory.createNewArray(new ObjectType("java.lang.Class"), (short) 1));
226 il.append(InstructionConst.DUP);
227 il.append(new PUSH(cp, 0));
228 il.append(factory.createFieldAccess("java.lang.Float", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
229 il.append(InstructionConst.AASTORE);
230 il.append(InstructionConst.DUP);
231 il.append(new PUSH(cp, 1));
232 il.append(factory.createFieldAccess("java.lang.Float", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
233 il.append(InstructionConst.AASTORE);
234 il.append(factory.createInvoke("java.lang.Class", "getMethod", new ObjectType("java.lang.reflect.Method"),
235 new Type[] { Type.STRING, new ArrayType(new ObjectType("java.lang.Class"), 1) }, Const.INVOKEVIRTUAL));
236 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
237 il.append(new PUSH(cp, 2));
238 il.append(factory.createNewArray(Type.OBJECT, (short) 1));
239 il.append(InstructionConst.DUP);
240 il.append(new PUSH(cp, 0));
241 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
242 il.append(new PUSH(cp, 1));
243 il.append(InstructionConst.AALOAD);
244 il.append(factory.createInvoke("java.lang.Float", "parseFloat", Type.FLOAT, new Type[] { Type.STRING }, Const.INVOKESTATIC));
245 il.append(factory.createInvoke("java.lang.Float", "valueOf", new ObjectType("java.lang.Float"), new Type[] { Type.FLOAT }, Const.INVOKESTATIC));
246 il.append(InstructionConst.AASTORE);
247 il.append(InstructionConst.DUP);
248 il.append(new PUSH(cp, 1));
249 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
250 il.append(new PUSH(cp, 2));
251 il.append(InstructionConst.AALOAD);
252 il.append(factory.createInvoke("java.lang.Float", "parseFloat", Type.FLOAT, new Type[] { Type.STRING }, Const.INVOKESTATIC));
253 il.append(factory.createInvoke("java.lang.Float", "valueOf", new ObjectType("java.lang.Float"), new Type[] { Type.FLOAT }, Const.INVOKESTATIC));
254 il.append(InstructionConst.AASTORE);
255 il.append(factory.createInvoke("java.lang.reflect.Method", "invoke", Type.OBJECT, new Type[] { Type.OBJECT, new ArrayType(Type.OBJECT, 1) },
256 Const.INVOKEVIRTUAL));
257 il.append(InstructionFactory.createReturn(Type.OBJECT));
258 final InstructionHandle ih3 = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
259 il.append(new PUSH(cp, "d"));
260 il.append(factory.createInvoke("java.lang.String", "startsWith", Type.BOOLEAN, new Type[] { Type.STRING }, Const.INVOKEVIRTUAL));
261 final BranchInstruction ifeq4 = InstructionFactory.createBranchInstruction(Const.IFEQ, null);
262 il.append(ifeq4);
263 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
264 il.append(factory.createInvoke("java.lang.Object", "getClass", new ObjectType("java.lang.Class"), Type.NO_ARGS, Const.INVOKEVIRTUAL));
265 il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
266 il.append(new PUSH(cp, 2));
267 il.append(factory.createNewArray(new ObjectType("java.lang.Class"), (short) 1));
268 il.append(InstructionConst.DUP);
269 il.append(new PUSH(cp, 0));
270 il.append(factory.createFieldAccess("java.lang.Double", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
271 il.append(InstructionConst.AASTORE);
272 il.append(InstructionConst.DUP);
273 il.append(new PUSH(cp, 1));
274 il.append(factory.createFieldAccess("java.lang.Double", "TYPE", new ObjectType("java.lang.Class"), Const.GETSTATIC));
275 il.append(InstructionConst.AASTORE);
276 il.append(factory.createInvoke("java.lang.Class", "getMethod", new ObjectType("java.lang.reflect.Method"),
277 new Type[] { Type.STRING, new ArrayType(new ObjectType("java.lang.Class"), 1) }, Const.INVOKEVIRTUAL));
278 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
279 il.append(new PUSH(cp, 2));
280 il.append(factory.createNewArray(Type.OBJECT, (short) 1));
281 il.append(InstructionConst.DUP);
282 il.append(new PUSH(cp, 0));
283 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
284 il.append(new PUSH(cp, 1));
285 il.append(InstructionConst.AALOAD);
286 il.append(factory.createInvoke("java.lang.Double", "parseDouble", Type.DOUBLE, new Type[] { Type.STRING }, Const.INVOKESTATIC));
287 il.append(factory.createInvoke("java.lang.Double", "valueOf", new ObjectType("java.lang.Double"), new Type[] { Type.DOUBLE }, Const.INVOKESTATIC));
288 il.append(InstructionConst.AASTORE);
289 il.append(InstructionConst.DUP);
290 il.append(new PUSH(cp, 1));
291 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
292 il.append(new PUSH(cp, 2));
293 il.append(InstructionConst.AALOAD);
294 il.append(factory.createInvoke("java.lang.Double", "parseDouble", Type.DOUBLE, new Type[] { Type.STRING }, Const.INVOKESTATIC));
295 il.append(factory.createInvoke("java.lang.Double", "valueOf", new ObjectType("java.lang.Double"), new Type[] { Type.DOUBLE }, Const.INVOKESTATIC));
296 il.append(InstructionConst.AASTORE);
297 il.append(factory.createInvoke("java.lang.reflect.Method", "invoke", Type.OBJECT, new Type[] { Type.OBJECT, new ArrayType(Type.OBJECT, 1) },
298 Const.INVOKEVIRTUAL));
299 il.append(InstructionFactory.createReturn(Type.OBJECT));
300 final InstructionHandle ih4 = il.append(InstructionConst.ACONST_NULL);
301 il.append(InstructionFactory.createReturn(Type.OBJECT));
302 ifeq1.setTarget(ih1);
303 ifeq2.setTarget(ih2);
304 ifeq3.setTarget(ih3);
305 ifeq4.setTarget(ih4);
306 method.setMaxStack();
307 method.setMaxLocals();
308 cg.addMethod(method.getMethod());
309 il.dispose();
310 }
311
312 private void createMethodDADD() {
313 final InstructionList il = new InstructionList();
314 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE }, new String[] { "a", "b" }, "dadd",
315 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
316
317 il.append(InstructionFactory.createLoad(Type.DOUBLE, 1));
318 il.append(InstructionFactory.createLoad(Type.DOUBLE, 3));
319 il.append(InstructionFactory.createBinaryOperation("+", Type.DOUBLE));
320 il.append(InstructionFactory.createReturn(Type.DOUBLE));
321 method.setMaxStack();
322 method.setMaxLocals();
323 cg.addMethod(method.getMethod());
324 il.dispose();
325 }
326
327 private void createMethodDDIV() {
328 final InstructionList il = new InstructionList();
329 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE }, new String[] { "a", "b" }, "ddiv",
330 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
331
332 il.append(InstructionFactory.createLoad(Type.DOUBLE, 1));
333 il.append(InstructionFactory.createLoad(Type.DOUBLE, 3));
334 il.append(InstructionFactory.createBinaryOperation("/", Type.DOUBLE));
335 il.append(InstructionFactory.createReturn(Type.DOUBLE));
336 method.setMaxStack();
337 method.setMaxLocals();
338 cg.addMethod(method.getMethod());
339 il.dispose();
340 }
341
342 private void createMethodDMUL() {
343 final InstructionList il = new InstructionList();
344 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE }, new String[] { "a", "b" }, "dmul",
345 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
346
347 il.append(InstructionFactory.createLoad(Type.DOUBLE, 1));
348 il.append(InstructionFactory.createLoad(Type.DOUBLE, 3));
349 il.append(InstructionFactory.createBinaryOperation("*", Type.DOUBLE));
350 il.append(InstructionFactory.createReturn(Type.DOUBLE));
351 method.setMaxStack();
352 method.setMaxLocals();
353 cg.addMethod(method.getMethod());
354 il.dispose();
355 }
356
357 private void createMethodDREM() {
358 final InstructionList il = new InstructionList();
359 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE }, new String[] { "a", "b" }, "drem",
360 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
361
362 il.append(InstructionFactory.createLoad(Type.DOUBLE, 1));
363 il.append(InstructionFactory.createLoad(Type.DOUBLE, 3));
364 il.append(InstructionFactory.createBinaryOperation("%", Type.DOUBLE));
365 il.append(InstructionFactory.createReturn(Type.DOUBLE));
366 method.setMaxStack();
367 method.setMaxLocals();
368 cg.addMethod(method.getMethod());
369 il.dispose();
370 }
371
372 private void createMethodDSUB() {
373 final InstructionList il = new InstructionList();
374 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.DOUBLE, new Type[] { Type.DOUBLE, Type.DOUBLE }, new String[] { "a", "b" }, "dsub",
375 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
376
377 il.append(InstructionFactory.createLoad(Type.DOUBLE, 1));
378 il.append(InstructionFactory.createLoad(Type.DOUBLE, 3));
379 il.append(InstructionFactory.createBinaryOperation("-", Type.DOUBLE));
380 il.append(InstructionFactory.createReturn(Type.DOUBLE));
381 method.setMaxStack();
382 method.setMaxLocals();
383 cg.addMethod(method.getMethod());
384 il.dispose();
385 }
386
387 private void createMethodFADD() {
388 final InstructionList il = new InstructionList();
389 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.FLOAT, new Type[] { Type.FLOAT, Type.FLOAT }, new String[] { "a", "b" }, "fadd",
390 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
391
392 il.append(InstructionFactory.createLoad(Type.FLOAT, 1));
393 il.append(InstructionFactory.createLoad(Type.FLOAT, 2));
394 il.append(InstructionFactory.createBinaryOperation("+", Type.FLOAT));
395 il.append(InstructionFactory.createReturn(Type.FLOAT));
396 method.setMaxStack();
397 method.setMaxLocals();
398 cg.addMethod(method.getMethod());
399 il.dispose();
400 }
401
402 private void createMethodFDIV() {
403 final InstructionList il = new InstructionList();
404 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.FLOAT, new Type[] { Type.FLOAT, Type.FLOAT }, new String[] { "a", "b" }, "fdiv",
405 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
406
407 il.append(InstructionFactory.createLoad(Type.FLOAT, 1));
408 il.append(InstructionFactory.createLoad(Type.FLOAT, 2));
409 il.append(InstructionFactory.createBinaryOperation("/", Type.FLOAT));
410 il.append(InstructionFactory.createReturn(Type.FLOAT));
411 method.setMaxStack();
412 method.setMaxLocals();
413 cg.addMethod(method.getMethod());
414 il.dispose();
415 }
416
417 private void createMethodFMUL() {
418 final InstructionList il = new InstructionList();
419 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.FLOAT, new Type[] { Type.FLOAT, Type.FLOAT }, new String[] { "a", "b" }, "fmul",
420 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
421
422 il.append(InstructionFactory.createLoad(Type.FLOAT, 1));
423 il.append(InstructionFactory.createLoad(Type.FLOAT, 2));
424 il.append(InstructionFactory.createBinaryOperation("*", Type.FLOAT));
425 il.append(InstructionFactory.createReturn(Type.FLOAT));
426 method.setMaxStack();
427 method.setMaxLocals();
428 cg.addMethod(method.getMethod());
429 il.dispose();
430 }
431
432 private void createMethodFREM() {
433 final InstructionList il = new InstructionList();
434 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.FLOAT, new Type[] { Type.FLOAT, Type.FLOAT }, new String[] { "a", "b" }, "frem",
435 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
436
437 il.append(InstructionFactory.createLoad(Type.FLOAT, 1));
438 il.append(InstructionFactory.createLoad(Type.FLOAT, 2));
439 il.append(InstructionFactory.createBinaryOperation("%", Type.FLOAT));
440 il.append(InstructionFactory.createReturn(Type.FLOAT));
441 method.setMaxStack();
442 method.setMaxLocals();
443 cg.addMethod(method.getMethod());
444 il.dispose();
445 }
446
447 private void createMethodFSUB() {
448 final InstructionList il = new InstructionList();
449 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.FLOAT, new Type[] { Type.FLOAT, Type.FLOAT }, new String[] { "a", "b" }, "fsub",
450 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
451
452 il.append(InstructionFactory.createLoad(Type.FLOAT, 1));
453 il.append(InstructionFactory.createLoad(Type.FLOAT, 2));
454 il.append(InstructionFactory.createBinaryOperation("-", Type.FLOAT));
455 il.append(InstructionFactory.createReturn(Type.FLOAT));
456 method.setMaxStack();
457 method.setMaxLocals();
458 cg.addMethod(method.getMethod());
459 il.dispose();
460 }
461
462 private void createMethodIADD() {
463 final InstructionList il = new InstructionList();
464 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "iadd",
465 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
466
467 il.append(InstructionFactory.createLoad(Type.INT, 1));
468 il.append(InstructionFactory.createLoad(Type.INT, 2));
469 il.append(InstructionFactory.createBinaryOperation("+", Type.INT));
470 il.append(InstructionFactory.createReturn(Type.INT));
471 method.setMaxStack();
472 method.setMaxLocals();
473 cg.addMethod(method.getMethod());
474 il.dispose();
475 }
476
477 private void createMethodIAND() {
478 final InstructionList il = new InstructionList();
479 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "iand",
480 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
481
482 il.append(InstructionFactory.createLoad(Type.INT, 1));
483 il.append(InstructionFactory.createLoad(Type.INT, 2));
484 il.append(InstructionFactory.createBinaryOperation("&", Type.INT));
485 il.append(InstructionFactory.createReturn(Type.INT));
486 method.setMaxStack();
487 method.setMaxLocals();
488 cg.addMethod(method.getMethod());
489 il.dispose();
490 }
491
492 private void createMethodIDIV() {
493 final InstructionList il = new InstructionList();
494 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "idiv",
495 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
496
497 il.append(InstructionFactory.createLoad(Type.INT, 1));
498 il.append(InstructionFactory.createLoad(Type.INT, 2));
499 il.append(InstructionFactory.createBinaryOperation("/", Type.INT));
500 il.append(InstructionFactory.createReturn(Type.INT));
501 method.setMaxStack();
502 method.setMaxLocals();
503 cg.addMethod(method.getMethod());
504 il.dispose();
505 }
506
507 private void createMethodIMUL() {
508 final InstructionList il = new InstructionList();
509 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "imul",
510 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
511
512 il.append(InstructionFactory.createLoad(Type.INT, 1));
513 il.append(InstructionFactory.createLoad(Type.INT, 2));
514 il.append(InstructionFactory.createBinaryOperation("*", Type.INT));
515 il.append(InstructionFactory.createReturn(Type.INT));
516 method.setMaxStack();
517 method.setMaxLocals();
518 cg.addMethod(method.getMethod());
519 il.dispose();
520 }
521
522 private void createMethodIOR() {
523 final InstructionList il = new InstructionList();
524 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "ior",
525 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
526
527 il.append(InstructionFactory.createLoad(Type.INT, 1));
528 il.append(InstructionFactory.createLoad(Type.INT, 2));
529 il.append(InstructionFactory.createBinaryOperation("|", Type.INT));
530 il.append(InstructionFactory.createReturn(Type.INT));
531 method.setMaxStack();
532 method.setMaxLocals();
533 cg.addMethod(method.getMethod());
534 il.dispose();
535 }
536
537 private void createMethodIREM() {
538 final InstructionList il = new InstructionList();
539 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "irem",
540 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
541
542 il.append(InstructionFactory.createLoad(Type.INT, 1));
543 il.append(InstructionFactory.createLoad(Type.INT, 2));
544 il.append(InstructionFactory.createBinaryOperation("%", Type.INT));
545 il.append(InstructionFactory.createReturn(Type.INT));
546 method.setMaxStack();
547 method.setMaxLocals();
548 cg.addMethod(method.getMethod());
549 il.dispose();
550 }
551
552 private void createMethodISHL() {
553 final InstructionList il = new InstructionList();
554 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "ishl",
555 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
556
557 il.append(InstructionFactory.createLoad(Type.INT, 1));
558 il.append(InstructionFactory.createLoad(Type.INT, 2));
559 il.append(InstructionFactory.createBinaryOperation("<<", Type.INT));
560 il.append(InstructionFactory.createReturn(Type.INT));
561 method.setMaxStack();
562 method.setMaxLocals();
563 cg.addMethod(method.getMethod());
564 il.dispose();
565 }
566
567 private void createMethodISHR() {
568 final InstructionList il = new InstructionList();
569 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "ishr",
570 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
571
572 il.append(InstructionFactory.createLoad(Type.INT, 1));
573 il.append(InstructionFactory.createLoad(Type.INT, 2));
574 il.append(InstructionFactory.createBinaryOperation(">>", Type.INT));
575 il.append(InstructionFactory.createReturn(Type.INT));
576 method.setMaxStack();
577 method.setMaxLocals();
578 cg.addMethod(method.getMethod());
579 il.dispose();
580 }
581
582 private void createMethodISUB() {
583 final InstructionList il = new InstructionList();
584 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "isub",
585 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
586
587 il.append(InstructionFactory.createLoad(Type.INT, 1));
588 il.append(InstructionFactory.createLoad(Type.INT, 2));
589 il.append(InstructionFactory.createBinaryOperation("-", Type.INT));
590 il.append(InstructionFactory.createReturn(Type.INT));
591 method.setMaxStack();
592 method.setMaxLocals();
593 cg.addMethod(method.getMethod());
594 il.dispose();
595 }
596
597 private void createMethodIUSHR() {
598 final InstructionList il = new InstructionList();
599 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "iushr",
600 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
601
602 il.append(InstructionFactory.createLoad(Type.INT, 1));
603 il.append(InstructionFactory.createLoad(Type.INT, 2));
604 il.append(InstructionFactory.createBinaryOperation(">>>", Type.INT));
605 il.append(InstructionFactory.createReturn(Type.INT));
606 method.setMaxStack();
607 method.setMaxLocals();
608 cg.addMethod(method.getMethod());
609 il.dispose();
610 }
611
612 private void createMethodIXOR() {
613 final InstructionList il = new InstructionList();
614 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.INT, new Type[] { Type.INT, Type.INT }, new String[] { "a", "b" }, "ixor",
615 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
616
617 il.append(InstructionFactory.createLoad(Type.INT, 1));
618 il.append(InstructionFactory.createLoad(Type.INT, 2));
619 il.append(InstructionFactory.createBinaryOperation("^", Type.INT));
620 il.append(InstructionFactory.createReturn(Type.INT));
621 method.setMaxStack();
622 method.setMaxLocals();
623 cg.addMethod(method.getMethod());
624 il.dispose();
625 }
626
627 private void createMethodLADD() {
628 final InstructionList il = new InstructionList();
629 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "ladd",
630 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
631
632 il.append(InstructionFactory.createLoad(Type.LONG, 1));
633 il.append(InstructionFactory.createLoad(Type.LONG, 3));
634 il.append(InstructionFactory.createBinaryOperation("+", Type.LONG));
635 il.append(InstructionFactory.createReturn(Type.LONG));
636 method.setMaxStack();
637 method.setMaxLocals();
638 cg.addMethod(method.getMethod());
639 il.dispose();
640 }
641
642 private void createMethodLAND() {
643 final InstructionList il = new InstructionList();
644 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "land",
645 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
646
647 il.append(InstructionFactory.createLoad(Type.LONG, 1));
648 il.append(InstructionFactory.createLoad(Type.LONG, 3));
649 il.append(InstructionFactory.createBinaryOperation("&", Type.LONG));
650 il.append(InstructionFactory.createReturn(Type.LONG));
651 method.setMaxStack();
652 method.setMaxLocals();
653 cg.addMethod(method.getMethod());
654 il.dispose();
655 }
656
657 private void createMethodLDIV() {
658 final InstructionList il = new InstructionList();
659 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "ldiv",
660 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
661
662 il.append(InstructionFactory.createLoad(Type.LONG, 1));
663 il.append(InstructionFactory.createLoad(Type.LONG, 3));
664 il.append(InstructionFactory.createBinaryOperation("/", Type.LONG));
665 il.append(InstructionFactory.createReturn(Type.LONG));
666 method.setMaxStack();
667 method.setMaxLocals();
668 cg.addMethod(method.getMethod());
669 il.dispose();
670 }
671
672 private void createMethodLMUL() {
673 final InstructionList il = new InstructionList();
674 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lmul",
675 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
676
677 il.append(InstructionFactory.createLoad(Type.LONG, 1));
678 il.append(InstructionFactory.createLoad(Type.LONG, 3));
679 il.append(InstructionFactory.createBinaryOperation("*", Type.LONG));
680 il.append(InstructionFactory.createReturn(Type.LONG));
681 method.setMaxStack();
682 method.setMaxLocals();
683 cg.addMethod(method.getMethod());
684 il.dispose();
685 }
686
687 private void createMethodLOR() {
688 final InstructionList il = new InstructionList();
689 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lor",
690 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
691
692 il.append(InstructionFactory.createLoad(Type.LONG, 1));
693 il.append(InstructionFactory.createLoad(Type.LONG, 3));
694 il.append(InstructionFactory.createBinaryOperation("|", Type.LONG));
695 il.append(InstructionFactory.createReturn(Type.LONG));
696 method.setMaxStack();
697 method.setMaxLocals();
698 cg.addMethod(method.getMethod());
699 il.dispose();
700 }
701
702 private void createMethodLREM() {
703 final InstructionList il = new InstructionList();
704 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lrem",
705 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
706
707 il.append(InstructionFactory.createLoad(Type.LONG, 1));
708 il.append(InstructionFactory.createLoad(Type.LONG, 3));
709 il.append(InstructionFactory.createBinaryOperation("%", Type.LONG));
710 il.append(InstructionFactory.createReturn(Type.LONG));
711 method.setMaxStack();
712 method.setMaxLocals();
713 cg.addMethod(method.getMethod());
714 il.dispose();
715 }
716
717 private void createMethodLSHL() {
718 final InstructionList il = new InstructionList();
719 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lshl",
720 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
721
722 il.append(InstructionFactory.createLoad(Type.LONG, 1));
723 il.append(InstructionFactory.createLoad(Type.LONG, 3));
724 il.append(InstructionConst.L2I);
725 il.append(InstructionFactory.createBinaryOperation("<<", Type.LONG));
726 il.append(InstructionFactory.createReturn(Type.LONG));
727 method.setMaxStack();
728 method.setMaxLocals();
729 cg.addMethod(method.getMethod());
730 il.dispose();
731 }
732
733 private void createMethodLSHR() {
734 final InstructionList il = new InstructionList();
735 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lshr",
736 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
737
738 il.append(InstructionFactory.createLoad(Type.LONG, 1));
739 il.append(InstructionFactory.createLoad(Type.LONG, 3));
740 il.append(InstructionConst.L2I);
741 il.append(InstructionFactory.createBinaryOperation(">>", Type.LONG));
742 il.append(InstructionFactory.createReturn(Type.LONG));
743 method.setMaxStack();
744 method.setMaxLocals();
745 cg.addMethod(method.getMethod());
746 il.dispose();
747 }
748
749 private void createMethodLSUB() {
750 final InstructionList il = new InstructionList();
751 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lsub",
752 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
753
754 il.append(InstructionFactory.createLoad(Type.LONG, 1));
755 il.append(InstructionFactory.createLoad(Type.LONG, 3));
756 il.append(InstructionFactory.createBinaryOperation("-", Type.LONG));
757 il.append(InstructionFactory.createReturn(Type.LONG));
758 method.setMaxStack();
759 method.setMaxLocals();
760 cg.addMethod(method.getMethod());
761 il.dispose();
762 }
763
764 private void createMethodLUSHR() {
765 final InstructionList il = new InstructionList();
766 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lushr",
767 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
768
769 il.append(InstructionFactory.createLoad(Type.LONG, 1));
770 il.append(InstructionFactory.createLoad(Type.LONG, 3));
771 il.append(InstructionConst.L2I);
772 il.append(InstructionFactory.createBinaryOperation(">>>", Type.LONG));
773 il.append(InstructionFactory.createReturn(Type.LONG));
774 method.setMaxStack();
775 method.setMaxLocals();
776 cg.addMethod(method.getMethod());
777 il.dispose();
778 }
779
780 private void createMethodLXOR() {
781 final InstructionList il = new InstructionList();
782 final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.LONG, new Type[] { Type.LONG, Type.LONG }, new String[] { "a", "b" }, "lxor",
783 ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
784
785 il.append(InstructionFactory.createLoad(Type.LONG, 1));
786 il.append(InstructionFactory.createLoad(Type.LONG, 3));
787 il.append(InstructionFactory.createBinaryOperation("^", Type.LONG));
788 il.append(InstructionFactory.createReturn(Type.LONG));
789 method.setMaxStack();
790 method.setMaxLocals();
791 cg.addMethod(method.getMethod());
792 il.dispose();
793 }
794
795 private void createMethodMain() {
796 final InstructionList il = new InstructionList();
797 final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) },
798 new String[] { "args" }, "main", ORG_APACHE_BCEL_GENERIC_BINARY_OP, il, cp);
799 method.addException("java.lang.Exception");
800
801 il.append(factory.createNew(ORG_APACHE_BCEL_GENERIC_BINARY_OP));
802 il.append(InstructionConst.DUP);
803 il.append(factory.createInvoke(ORG_APACHE_BCEL_GENERIC_BINARY_OP, "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
804 il.append(InstructionFactory.createStore(Type.OBJECT, 1));
805 il.append(factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Const.GETSTATIC));
806 il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
807 il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
808 il.append(factory.createInvoke(ORG_APACHE_BCEL_GENERIC_BINARY_OP, "calculate", Type.OBJECT, new Type[] { new ArrayType(Type.STRING, 1) },
809 Const.INVOKEVIRTUAL));
810 il.append(factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.OBJECT }, Const.INVOKEVIRTUAL));
811 il.append(InstructionFactory.createReturn(Type.VOID));
812 method.setMaxStack();
813 method.setMaxLocals();
814 cg.addMethod(method.getMethod());
815 il.dispose();
816 }
817 }