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