diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java new file mode 100644 index 0000000..619068a --- /dev/null +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -0,0 +1,39 @@ +package bytecode; + +import ast.ClassNode; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Opcodes; + +import java.io.FileOutputStream; +import java.io.IOException; + +public class ClassCodeGen { + public void generateClassCode(ClassNode classNode) { + ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, + "java/lang/Object", null); + + FieldCodeGen fieldCodeGen = new FieldCodeGen(); + fieldCodeGen.generateFieldCode(classWriter); + + MethodCodeGen methodCodeGen = new MethodCodeGen(); + methodCodeGen.generateMethodCode(classWriter); + + classWriter.visitEnd(); + printIntoClassFile(classWriter.toByteArray(), classNode.name); + + classWriter.visitEnd(); + } + + private void printIntoClassFile(byte[] byteCode, String name) { + String filePath = "./classFileOutput/" + name + ".class"; + + try { + FileOutputStream fileOutputStream = new FileOutputStream(filePath); + fileOutputStream.write(byteCode); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}