Add accesTypeToOpcode mapper

This commit is contained in:
i22007 2024-05-08 15:22:39 +02:00
parent 3cbef38c5e
commit 44452d204b
5 changed files with 22 additions and 11 deletions

View File

@ -1,3 +1,3 @@
public class Example { public class Example() {
}
}

View File

@ -14,7 +14,7 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CharStream codeCharStream = null; CharStream codeCharStream = null;
try { try {
codeCharStream = CharStreams.fromPath(Paths.get("src\\main\\java\\Example.java")); codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java"));
parsefile(codeCharStream); parsefile(codeCharStream);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage()); System.err.println("Error reading the file: " + e.getMessage());

View File

@ -1,6 +1,9 @@
package bytecode; package bytecode;
import ast.ClassNode; import ast.ClassNode;
import ast.FieldNode;
import ast.MemberNode;
import ast.MethodNode;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -13,11 +16,15 @@ public class ClassCodeGen {
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null, classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null,
"java/lang/Object", null); "java/lang/Object", null);
FieldCodeGen fieldCodeGen = new FieldCodeGen(); for (MemberNode memberNode : classNode.members) {
fieldCodeGen.generateFieldCode(classWriter); if (memberNode instanceof FieldNode) {
FieldCodeGen fieldCodeGen = new FieldCodeGen();
MethodCodeGen methodCodeGen = new MethodCodeGen(); fieldCodeGen.generateFieldCode(classWriter, (FieldNode) memberNode);
methodCodeGen.generateMethodCode(classWriter); } else if (memberNode instanceof MethodNode) {
MethodCodeGen methodCodeGen = new MethodCodeGen();
methodCodeGen.generateMethodCode(classWriter, (MethodNode) memberNode);
}
}
classWriter.visitEnd(); classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.name); printIntoClassFile(classWriter.toByteArray(), classNode.name);

View File

@ -1,10 +1,13 @@
package bytecode; package bytecode;
import ast.FieldNode;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
public class FieldCodeGen { 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);
} }
} }

View File

@ -1,11 +1,12 @@
package bytecode; package bytecode;
import ast.MethodNode;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
public class MethodCodeGen { public class MethodCodeGen {
public void generateMethodCode(ClassWriter classWriter) { public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) {
MethodVisitor constructor = MethodVisitor constructor =
classWriter.visitMethod(Opcodes.ACC_PUBLIC, classWriter.visitMethod(Opcodes.ACC_PUBLIC,
"<init>", "<init>",