Skip to content

Commit

Permalink
a field instruction added
Browse files Browse the repository at this point in the history
  • Loading branch information
hakdogan committed Mar 31, 2024
1 parent ad6e309 commit 62e2120
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/jugistanbul/GenerateClass.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.jugistanbul;

import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.lang.classfile.Opcode;
import java.lang.classfile.*;
import java.lang.constant.MethodTypeDesc;
import java.nio.file.Path;

import static java.lang.classfile.ClassFile.ACC_PUBLIC;
import static java.lang.classfile.ClassFile.ACC_STATIC;
import static java.lang.classfile.ClassFile.*;
import static java.lang.constant.ClassDesc.of;
import static java.lang.constant.ConstantDescs.CD_void;
import static java.lang.constant.ConstantDescs.*;

/**
* @author hakdogan (huseyin.akdogan@patikaglobal.com)
Expand All @@ -19,19 +17,21 @@ public class GenerateClass
{
public static void main(String[] args) throws IOException {

ClassFile.of().buildTo(Path.of("HelloJEP457.class"), of("HelloJEP457"),
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,
.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("HelloJEP457"),
"hello", MethodTypeDesc.of(CD_void), false)
.invokeInstruction(Opcode.INVOKESTATIC, of(className), "hello", MethodTypeDesc.of(CD_void), false)
.return_()));
}

}

0 comments on commit 62e2120

Please sign in to comment.