Add AccesTypeToOpcode mapper

This commit is contained in:
i22007 2024-05-08 15:23:04 +02:00
parent 44452d204b
commit 26653ac719
4 changed files with 23 additions and 3 deletions

View File

@ -13,7 +13,8 @@ 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,
Mapper mapper = new Mapper();
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
"java/lang/Object", null);
for (MemberNode memberNode : classNode.members) {

View File

@ -8,6 +8,7 @@ import org.objectweb.asm.Opcodes;
public class FieldCodeGen {
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
FieldVisitor fieldVisitor = classWriter.visitField(Opcodes.ACC_PUBLIC, fieldNode.name, "", null, null);
Mapper mapper = new Mapper();
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.name, "", null, null);
}
}

View File

@ -0,0 +1,17 @@
package bytecode;
import ast.AccessTypeNode;
import ast.EnumAccessTypeNode;
import org.objectweb.asm.Opcodes;
public class Mapper {
public int mapAccesTypeToOpcode(AccessTypeNode type) {
switch (type.enumAccessTypeNode) {
case EnumAccessTypeNode.PUBLIC:
return Opcodes.ACC_PUBLIC;
case EnumAccessTypeNode.PRIVATE:
return Opcodes.ACC_PRIVATE;
}
return 0;
}
}

View File

@ -7,8 +7,9 @@ import org.objectweb.asm.Opcodes;
public class MethodCodeGen {
public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) {
Mapper mapper = new Mapper();
MethodVisitor constructor =
classWriter.visitMethod(Opcodes.ACC_PUBLIC,
classWriter.visitMethod(mapper.mapAccesTypeToOpcode(methodNode.visibility),
"<init>",
"()V",
null,