Generate jar with first class as main
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
57c2023215
commit
837e751094
@ -1,7 +1,7 @@
|
|||||||
package ast.type;
|
package ast.type;
|
||||||
|
|
||||||
public class AccessModifierNode {
|
public class AccessModifierNode {
|
||||||
EnumAccessModifierNode accessType;
|
public EnumAccessModifierNode accessType;
|
||||||
|
|
||||||
public AccessModifierNode(String accessModifier) {
|
public AccessModifierNode(String accessModifier) {
|
||||||
setModifier(accessModifier);
|
setModifier(accessModifier);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package ast.type;
|
package ast.type;
|
||||||
|
|
||||||
public class TypeNode {
|
public class TypeNode {
|
||||||
EnumTypeNode type;
|
public EnumTypeNode type;
|
||||||
|
|
||||||
public TypeNode(String type) {
|
public TypeNode(String type) {
|
||||||
setType(type);
|
setType(type);
|
||||||
|
@ -4,17 +4,57 @@ import ast.ProgramNode;
|
|||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import bytecode.visitor.ProgramVisitor;
|
import bytecode.visitor.ProgramVisitor;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
|
|
||||||
public class ByteCodeGenerator implements ProgramVisitor {
|
public class ByteCodeGenerator implements ProgramVisitor {
|
||||||
|
|
||||||
@Override
|
private JarOutputStream jarOutputStream;
|
||||||
public void visit(ProgramNode programNode) {
|
private ByteArrayOutputStream byteArrayOutputStream;
|
||||||
//ASMCodeGenerator asmCodeGenerator = new ASMCodeGenerator();
|
|
||||||
//asmCodeGenerator.test();
|
|
||||||
|
|
||||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
|
||||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
@Override
|
||||||
classDeclarationNode.accept(classCodeGen);
|
public void visit(ProgramNode programNode) {
|
||||||
|
byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
|
Manifest manifest = new Manifest();
|
||||||
|
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||||
|
manifest.getMainAttributes().putValue("Main-Class", programNode.classes.get(0).identifier);
|
||||||
|
|
||||||
|
jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||||
|
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream);
|
||||||
|
classDeclarationNode.accept(classCodeGen);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
jarOutputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
saveJarFile(byteArrayOutputStream.toByteArray(), "output.jar");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private void saveJarFile(byte[] jarBytes, String jarFileName) {
|
||||||
|
File directory = new File("src/main/resources/classFileOutput");
|
||||||
|
if (!directory.exists()) {
|
||||||
|
directory.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
File jarFile = new File(directory, jarFileName);
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(jarFile)) {
|
||||||
|
fos.write(jarBytes);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,22 +4,27 @@ import ast.ClassNode;
|
|||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MemberNode;
|
import ast.member.MemberNode;
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
import ast.type.BaseTypeNode;
|
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
|
||||||
|
|
||||||
public class ClassCodeGen implements ClassVisitor {
|
public class ClassCodeGen implements ClassVisitor {
|
||||||
private Mapper mapper;
|
private Mapper mapper;
|
||||||
private ClassWriter classWriter;
|
private ClassWriter classWriter;
|
||||||
|
private JarOutputStream jarOutputStream;
|
||||||
|
|
||||||
public ClassCodeGen() {
|
public ClassCodeGen(JarOutputStream jarOutputStream) {
|
||||||
mapper = new Mapper();
|
mapper = new Mapper();
|
||||||
|
this.jarOutputStream = jarOutputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -38,6 +43,7 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
|
writeToJar(classWriter.toByteArray(), classNode.identifier);
|
||||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
@ -45,9 +51,7 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(FieldNode fieldNode) {
|
public void visit(FieldNode fieldNode) {
|
||||||
if(fieldNode.type instanceof BaseTypeNode baseTypeNode){
|
classWriter.visitField(mapper.mapAccessTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(fieldNode.type.type), null, null);
|
||||||
classWriter.visitField(mapper.mapAccessTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(baseTypeNode.enumType), null, null );
|
|
||||||
}
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,4 +71,15 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void writeToJar(byte[] byteCode, String className) {
|
||||||
|
try {
|
||||||
|
JarEntry jarEntry = new JarEntry(className + ".class");
|
||||||
|
jarOutputStream.putNextEntry(jarEntry);
|
||||||
|
jarOutputStream.write(byteCode);
|
||||||
|
jarOutputStream.closeEntry();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,23 @@ package bytecode;
|
|||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import ast.type.BaseTypeNode;
|
|
||||||
|
|
||||||
public class Mapper {
|
public class Mapper {
|
||||||
public int mapAccessTypeToOpcode(AccessTypeNode type) {
|
public int mapAccessTypeToOpcode(AccessModifierNode accessModifierNode) {
|
||||||
switch (type.enumAccessTypeNode) {
|
switch (accessModifierNode.accessType) {
|
||||||
case EnumAccessTypeNode.PUBLIC:
|
case EnumAccessModifierNode.PUBLIC:
|
||||||
return Opcodes.ACC_PUBLIC;
|
return Opcodes.ACC_PUBLIC;
|
||||||
case EnumAccessTypeNode.PRIVATE:
|
case EnumAccessModifierNode.PRIVATE:
|
||||||
return Opcodes.ACC_PRIVATE;
|
return Opcodes.ACC_PRIVATE;
|
||||||
|
case EnumAccessModifierNode.PUBLIC_STATIC:
|
||||||
|
return Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
|
||||||
|
case EnumAccessModifierNode.PRIVATE_STATIC:
|
||||||
|
return Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateMethodDescriptor(BaseTypeNode baseTypeNode, ParameterListNode parameterListNode) {
|
public String generateMethodDescriptor(TypeNode typeNode, ParameterListNode parameterListNode) {
|
||||||
String descriptor = "(";
|
String descriptor = "(";
|
||||||
for(ParameterNode parameterNode : parameterListNode.parameters) {
|
for(ParameterNode parameterNode : parameterListNode.parameters) {
|
||||||
descriptor += getTypeChar(EnumTypeNode.INT);
|
descriptor += getTypeChar(EnumTypeNode.INT);
|
||||||
|
@ -35,7 +35,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visit(ConstructorNode constructorNode) {
|
public void visit(ConstructorNode constructorNode) {
|
||||||
methodVisitor =
|
methodVisitor =
|
||||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.visibility),
|
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType),
|
||||||
"<init>",
|
"<init>",
|
||||||
"()V",
|
"()V",
|
||||||
null,
|
null,
|
||||||
|
Loading…
Reference in New Issue
Block a user