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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| final String ClassName = "HelloJVM"; ClassGen classGen = new ClassGen(ClassName, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); ConstantPoolGen constantPoolGen = classGen.getConstantPool(); InstructionList instructionList = new InstructionList(); InstructionFactory instructionFactory = new InstructionFactory(classGen);
MethodGen test1MethodGen = new MethodGen(Constants.ACC_PUBLIC, Type.INT, Type.NO_ARGS, null, "test1", ClassName, instructionList, constantPoolGen); instructionList.append(new PUSH(constantPoolGen, 5)); instructionList.append(InstructionFactory.createReturn(Type.INT));
test1MethodGen.setMaxLocals(); test1MethodGen.setMaxStack(); classGen.addMethod(test1MethodGen.getMethod()); instructionList.dispose();
MethodGen test2MethodGen = new MethodGen(Constants.ACC_PUBLIC, Type.DOUBLE, Type.NO_ARGS, null, "test1", ClassName, instructionList, constantPoolGen); instructionList.append(new PUSH(constantPoolGen, 10.112)); instructionList.append(InstructionFactory.createReturn(Type.DOUBLE));
test2MethodGen.setMaxLocals(); test2MethodGen.setMaxStack(); classGen.addMethod(test2MethodGen.getMethod()); instructionList.dispose();
MethodGen mainMethodGen = new MethodGen(Constants.ACC_PUBLIC | Constants.ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] {"argv"}, "main", ClassName, instructionList, constantPoolGen); ObjectType p_stream = new ObjectType("java.io.PrintStream");
instructionList.append(instructionFactory.createNew(ClassName)); instructionList.append(InstructionFactory.createDup(1)); instructionList.append(instructionFactory.createInvoke(ClassName, "<init>", Type.VOID, new Type[] {}, (short) Constants.INVOKESPECIAL)); instructionList.append(InstructionFactory.createStore(Type.OBJECT, 1)); instructionList.append(instructionFactory.createFieldAccess("java.lang.System", "out", p_stream, (short) Constants.GETSTATIC)); instructionList.append(InstructionFactory.createLoad(Type.OBJECT, 1)); instructionList.append(instructionFactory.createInvoke(ClassName, "test1", Type.INT, Type.NO_ARGS, (short) Constants.INVOKEVIRTUAL)); instructionList.append(instructionFactory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] {Type.INT}, (short) Constants.INVOKEVIRTUAL)); instructionList.append(instructionFactory.createFieldAccess("java.lang.System", "out", p_stream, (short) Constants.GETSTATIC)); instructionList.append(InstructionFactory.createLoad(Type.OBJECT, 1)); instructionList.append(instructionFactory.createInvoke(ClassName, "test1", Type.DOUBLE, Type.NO_ARGS, (short) Constants.INVOKEVIRTUAL)); instructionList.append(instructionFactory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] {Type.DOUBLE}, (short) Constants.INVOKEVIRTUAL));
instructionList.append(InstructionFactory.createReturn(Type.VOID));
mainMethodGen.setMaxStack(); mainMethodGen.setMaxLocals(); classGen.addMethod(mainMethodGen.getMethod());
instructionList.dispose();
classGen.addEmptyConstructor(Constants.ACC_PUBLIC);
try { classGen.getJavaClass().dump("HelloJVM.class"); } catch(IOException e) { e.printStackTrace(); }
|