RFR: 8355658: Allow transforms to run on elements generated by a builder

Adam Sotona asotona at openjdk.org
Mon Apr 28 09:33:48 UTC 2025


On Mon, 28 Apr 2025 01:43:32 GMT, Chen Liang <liach at openjdk.org> wrote:

> Transforms can run on a stream of class file elements. Currently, that stream can only be from a CompoundElement. We can allow a ClassFileBuilder to provide such a stream too; a recent request https://mail.openjdk.org/pipermail/classfile-api-dev/2025-April/000698.html asks for this as well.
> 
> With this patch, we can now emulate this ASM pattern easily:
> 
> 
> // ASM
> ClassVisitor cv = new DelegateClassVisitor(new ClassWriter(...));
> cv.visitXxx(); // write elements through the delegate
> 
> // ClassFile API
> cf.build(..., clb0 -> clb0.transforming((clb, cle) -> /*process */, clb -> {
>     // write elements through delegate
> }));
> 
> 
> Notably, this patch introduces a source incompatibility (but not binary) in order to allow users to call `transform(model, (xb, xe) -> {})` or `transforming((xb1, xe) -> {}, xb1 -> {})`. This has little impact if users don't use `ClassFileBuilder` as a type directly (which according to grep.app, there are only two sites on whole GitHub, both updated in this PR). A release note has been created for this incompatibility at https://bugs.openjdk.org/browse/JDK-8355665; please review too.

Actual workaround for the above pattern is:

cf.transformClass(cf.parse(cf.build(...)), (clb, cle) -> /*process */, clb -> {
    // write elements through delegate
});

Instead of breaking the actual Class-File API compatibility and introduction of a new complex build&transform pattern
I would rather propose a new API method(s) ``ClassFile::buildToModel``.
However only if there is a strong request to optimize the process (to avoid redundant build/parse spin).

``CodeBuilder::transforming`` was never intended as a general pattern for all elements on all levels. Code building is the only exception providing intermediate transformation. Code building needs to handle situations where the actually built code must be immediately transformed in order to make the code valid to successfully finish the build process. This approach is not necessary on other building levels (class, method or field builder).

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

PR Comment: https://git.openjdk.org/jdk/pull/24908#issuecomment-2834600671


More information about the compiler-dev mailing list