Skip to content

Commit

Permalink
Use MethodTypeDesc builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjameshamilton committed Jan 21, 2024
1 parent bad77b3 commit ea854cd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
import java.io.IOException;
import java.nio.file.Path;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;

import static java.lang.constant.ConstantDescs.CD_void;
import static java.lang.constant.ClassDesc.*;
import static java.lang.constant.MethodTypeDesc.ofDescriptor;
import static java.lang.classfile.ClassFile.*;

public class Main {
public static void main(String[] args) throws IOException {
ClassFile.of().buildTo(Path.of("HelloWorld.class"), ClassDesc.of("HelloWorld"), classBuilder -> classBuilder
.withMethodBody("main", ofDescriptor("([Ljava/lang/String;)V"), ACC_PUBLIC | ACC_STATIC, codeBuilder -> codeBuilder
ClassFile.of().buildTo(Path.of("HelloWorld.class"), of("HelloWorld"), classBuilder -> classBuilder
.withMethodBody("main", MethodTypeDesc.of(CD_void, of("java.lang.String").arrayType()), ACC_PUBLIC | ACC_STATIC, codeBuilder -> codeBuilder
.getstatic(of("java.lang.System"), "out", of("java.io.PrintStream"))
.ldc("Hello World")
.invokevirtual(of("java.io.PrintStream"), "println", ofDescriptor("(Ljava/lang/Object;)V"))
.invokevirtual(of("java.io.PrintStream"), "println", MethodTypeDesc.of(CD_void, of("java.lang.Object")))
.return_()));
}
}

0 comments on commit ea854cd

Please sign in to comment.