-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenerateClass.java
37 lines (32 loc) · 1.83 KB
/
GenerateClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.jugistanbul;
import java.io.IOException;
import java.lang.classfile.*;
import java.lang.constant.MethodTypeDesc;
import java.nio.file.Path;
import static java.lang.classfile.ClassFile.*;
import static java.lang.constant.ClassDesc.of;
import static java.lang.constant.ConstantDescs.*;
/**
* @author hakdogan (huseyin.akdogan@patikaglobal.com)
* Created on 30.03.2024
***/
public class GenerateClass
{
public static void main(String[] args) throws IOException {
var className = Info.CLASS_NAME.getClassName();
ClassFile.of().buildTo(Path.of(STR."\{className}.class"), of(className),
classBuilder -> classBuilder
.withField("myField", of("java.lang.String"), ACC_PRIVATE)
.withMethodBody("hello", MethodTypeDesc.of(CD_void), ACC_STATIC,
codeBuilder -> codeBuilder
.getstatic(of("java.lang.System"), "out", of("java.io.PrintStream"))
.ldc("Hello, this class is generated by Class-File API which is a preview API to provide a standard API for parsing, generating, and transforming Java class files.")
.invokevirtual(of("java.io.PrintStream"), "println",
MethodTypeDesc.of(CD_void, of("java.lang.Object")))
.return_())
.withMethodBody("main", MethodTypeDesc.of(CD_void, of("java.lang.String").arrayType()), ACC_PUBLIC | ACC_STATIC,
codeBuilder -> codeBuilder
.invokeInstruction(Opcode.INVOKESTATIC, of(className), "hello", MethodTypeDesc.of(CD_void), false)
.return_()));
}
}