RFR: 8359707: Add classfile modification code to RedefineClassHelper [v6]

David Holmes dholmes at openjdk.org
Thu Jun 26 07:39:32 UTC 2025


On Wed, 25 Jun 2025 21:00:15 GMT, Coleen Phillimore <coleenp at openjdk.org> wrote:

>> I copied this code for another test in the Valhalla repo and thought it would be a good utility function.  It might be better written using the Classfile API.
>> Tested with test.
>
> Coleen Phillimore has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Made it even shorter.

This looks really neat. A couple of nits and a query.

test/lib/RedefineClassHelper.java line 29:

> 27: import java.lang.classfile.ClassFile;
> 28: import java.lang.classfile.ClassModel;
> 29: import java.util.Map;

Nit: import out of order

test/lib/RedefineClassHelper.java line 74:

> 72: 
> 73:     private static byte[] getBytecodes(ClassLoader loader, String name) throws Exception {
> 74:         try(InputStream is = loader.getResourceAsStream(name + ".class")) {

Suggestion:

        try (InputStream is = loader.getResourceAsStream(name + ".class")) {

test/lib/RedefineClassHelper.java line 92:

> 90:         byte[] buf = getBytecodes(loader, oldClassName);
> 91:         ClassModel classModel = ClassFile.of().parse(buf);
> 92:         return ClassFile.of().build(ClassDesc.of(newClassName), classModel::forEach);

That's cool! I have no idea how to read it to understand what it does, but it is cool it can be done in one line. :)

Seriously though, which part of that is doing the actual replace??

-------------

Marked as reviewed by dholmes (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/25857#pullrequestreview-2961031930
PR Review Comment: https://git.openjdk.org/jdk/pull/25857#discussion_r2168385540
PR Review Comment: https://git.openjdk.org/jdk/pull/25857#discussion_r2168392685
PR Review Comment: https://git.openjdk.org/jdk/pull/25857#discussion_r2168399258


More information about the serviceability-dev mailing list