From 44452d204b7cdcff5a2de508ffbd30973ccd1fc4 Mon Sep 17 00:00:00 2001 From: i22007 Date: Wed, 8 May 2024 15:22:39 +0200 Subject: [PATCH] Add accesTypeToOpcode mapper --- src/main/java/Example.java | 4 ++-- src/main/java/Main.java | 2 +- src/main/java/bytecode/ClassCodeGen.java | 17 ++++++++++++----- src/main/java/bytecode/FieldCodeGen.java | 7 +++++-- src/main/java/bytecode/MethodCodeGen.java | 3 ++- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/Example.java b/src/main/java/Example.java index 6f34d6e..75d8cd9 100644 --- a/src/main/java/Example.java +++ b/src/main/java/Example.java @@ -1,3 +1,3 @@ -public class Example { -} +public class Example() { +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 65c9921..1315931 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,7 +14,7 @@ public class Main { public static void main(String[] args) throws Exception { CharStream codeCharStream = null; try { - codeCharStream = CharStreams.fromPath(Paths.get("src\\main\\java\\Example.java")); + codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java")); parsefile(codeCharStream); } catch (IOException e) { System.err.println("Error reading the file: " + e.getMessage()); diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java index 619068a..af003bc 100644 --- a/src/main/java/bytecode/ClassCodeGen.java +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -1,6 +1,9 @@ package bytecode; import ast.ClassNode; +import ast.FieldNode; +import ast.MemberNode; +import ast.MethodNode; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -13,11 +16,15 @@ public class ClassCodeGen { 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); + for (MemberNode memberNode : classNode.members) { + if (memberNode instanceof FieldNode) { + FieldCodeGen fieldCodeGen = new FieldCodeGen(); + fieldCodeGen.generateFieldCode(classWriter, (FieldNode) memberNode); + } else if (memberNode instanceof MethodNode) { + MethodCodeGen methodCodeGen = new MethodCodeGen(); + methodCodeGen.generateMethodCode(classWriter, (MethodNode) memberNode); + } + } classWriter.visitEnd(); printIntoClassFile(classWriter.toByteArray(), classNode.name); diff --git a/src/main/java/bytecode/FieldCodeGen.java b/src/main/java/bytecode/FieldCodeGen.java index 4cca429..abe7ebb 100644 --- a/src/main/java/bytecode/FieldCodeGen.java +++ b/src/main/java/bytecode/FieldCodeGen.java @@ -1,10 +1,13 @@ package bytecode; +import ast.FieldNode; import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Opcodes; public class FieldCodeGen { - public void generateFieldCode(ClassWriter classWriter) { - + public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) { + FieldVisitor fieldVisitor = classWriter.visitField(Opcodes.ACC_PUBLIC, fieldNode.name, "", null, null); } } diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index a54fda8..c7c1bd3 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -1,11 +1,12 @@ package bytecode; +import ast.MethodNode; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class MethodCodeGen { - public void generateMethodCode(ClassWriter classWriter) { + public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) { MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "",