JEP 484 Class-File API (previously JEP 457 and JEP 466) is an API introduced in Java 24 to provide a standard API for parsing, generating, and transforming Java class files.
The example here shows how easy it is to use to read and modify an existing class file.
The example modifies a single ldc
instruction to change the constant printed from "Hello World" to
another string provided as an argument.
For examples of generating a class from scratch see Java Class-File API Hello World and Java Class-File API Brainf*ck Compiler.
You'll need JDK 24. The easiest way to install this on Linux is with SDK man:
sdk install java 24.ea.36-open
If you have Java 24, you can then execute the run.sh
script which will
compile and execute HelloWorld.java
and HelloTranslator.java
.
HelloTranslator
will transform HelloWorld.class
, changing
the constant "Hello World" into the script argument,
and then executing it.
$ ./run.sh "Hallo Wereld"
There are many similar projects for parsing, generating, and transforming Java class files, including: