Repository navigation
Java 24: Class-File API (JEP 484) - #122
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What you'll learn
.classfile into aClassModeland inspect its methods and fields withClassFile.of().parse(...)ClassFile.of().build(...), load it as a hidden class viaMethodHandles.Lookup.defineHiddenClass, and invoke it with aMethodHandleClassTransform(here: dropping one method) and get back new, valid bytecodeWhy it matters
The Class-File API is the JDK's own supported replacement for ASM/BCEL-style bytecode libraries: it tracks the JVM spec release-by-release, so tools built on it (debuggers, bytecode processors, dynamic-proxy generators) no longer need to vendor a separate bytecode library that risks falling behind a new class file version.
Read more
JEP 484: Class-File API
Quiz
After a
ClassTransform.dropping(...)removes a method from a class's bytecode and the result is re-parsed into aClassModel, does the original class's declared constructor (<init>) still show up inclassModel.methods()?Answer
Yes. Every constructor compiles to a regular method named
<init>in the class file, andClassTransform.droppingonly removes the elements matched by the predicate — it was not asked to drop<init>, so it passes through untouched along with every other method.Note: this sandbox's network policy blocked every JDK distribution host (including the Gradle toolchain resolver), so
./gradlew :JavaReleases:testcould not run locally with only JDK 21 installed. CI has since run on JDK 25 and is green (306 tests, +3, 0 failures), confirming all three new tests pass.Closes #110