Merge remote-tracking branch 'origin/main'
# Conflicts: # src/main/java/Main.java # src/main/java/bytecode/ByteCodeGenerator.java
This commit is contained in:
commit
d7a8500109
39
src/main/java/bytecode/ClassCodeGen.java
Normal file
39
src/main/java/bytecode/ClassCodeGen.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user