-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCELMainGenerator.java
62 lines (54 loc) · 2.46 KB
/
BCELMainGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import org.apache.bcel.Const;
import org.apache.bcel.generic.*;
import org.apache.bcel.classfile.*;
public class BCELMainGenerator {
public static void main(String[] args) {
try {
generateMainClass("Main", args);
System.out.println("Main class generated successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void generateMainClass(String className, String[] args) throws Exception {
ClassGen cg = new ClassGen(className, "java.lang.Object", "<generated>", Const.ACC_PUBLIC | Const.ACC_SUPER, null);
ConstantPoolGen cp = cg.getConstantPool();
// Create main method
InstructionList il = new InstructionList();
MethodGen mg = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] { "args" }, "main", className, il, cp);
for (String arg : args) {
String[] parts = new String[2];
if (arg != null) {
parts = arg.split("_");
}
switch (parts[0]) {
case "IConst":
il.append(new ICONST(Integer.parseInt(parts[1])));
break;
case "ILoad":
il.append(new ILOAD(Integer.parseInt(parts[1])));
break;
case "IStore":
il.append(new ISTORE(Integer.parseInt(parts[1])));
break;
case "LPrint":
il.append(new GETSTATIC(cp.addFieldref("java.lang.System", "out", "Ljava/io/PrintStream;")));
il.append(new PUSH(cp, Integer.parseInt(parts[1])));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream", "println", "(I)V")));
break;
case "IPrint":
il.append(new GETSTATIC(cp.addFieldref("java.lang.System", "out", "Ljava/io/PrintStream;")));
il.append(new ILOAD(Integer.parseInt(parts[1])));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream", "println", "(I)V")));
break;
}
}
il.append(new RETURN());
mg.setMaxStack();
mg.setMaxLocals();
cg.addMethod(mg.getMethod());
// Write class to file
JavaClass javaClass = cg.getJavaClass();
javaClass.dump(className + ".class");
}
}