Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

liach
Copy link
Member

@liach liach commented Apr 28, 2025

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.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8355666 to be approved

Issues

  • JDK-8355658: Allow transforms to run on elements generated by a builder (Enhancement - P4)
  • JDK-8355666: Allow transforms to run on elements generated by a builder (CSR)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24908/head:pull/24908
$ git checkout pull/24908

Update a local copy of the PR:
$ git checkout pull/24908
$ git pull https://git.openjdk.org/jdk.git pull/24908/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24908

View PR using the GUI difftool:
$ git pr show -t 24908

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24908.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 28, 2025

👋 Welcome back liach! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 28, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 28, 2025
@openjdk
Copy link

openjdk bot commented Apr 28, 2025

@liach The following labels will be automatically applied to this pull request:

  • build
  • compiler
  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Apr 28, 2025

Webrevs

@liach
Copy link
Member Author

liach commented Apr 28, 2025

Testing: tier 1-3 passed

@asotona
Copy link
Member

asotona commented Apr 28, 2025

Actual workaround for the above pattern is:

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

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).

@liach
Copy link
Member Author

liach commented Apr 28, 2025

Indeed, even if we want to construct a transformed handler, an instance method on ClassFileTransform is better.

@liach liach closed this Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build [email protected] compiler [email protected] core-libs [email protected] csr Pull request needs approved CSR before integration rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants