Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8355666

Allow transforms to run on elements generated by a builder

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • source
    • low
    • Hide
      A grep.app search for `ClassFileBuilder<` yield only 2 result in all of GitHub, both of which are in the JDK authored by me. All other usages have no source compatibility risks; there are always ways to migrate as noted in the release note on the main issue. This change in type parameter declaration does not affect binary linkage in any way.

      Otherwise, usages through the subinterfaces are completely fine; they are sealed, so method addition is safe.
      Show
      A grep.app search for `ClassFileBuilder<` yield only 2 result in all of GitHub, both of which are in the JDK authored by me. All other usages have no source compatibility risks; there are always ways to migrate as noted in the release note on the main issue. This change in type parameter declaration does not affect binary linkage in any way. Otherwise, usages through the subinterfaces are completely fine; they are sealed, so method addition is safe.
    • Java API
    • SE

      Summary

      Modify the generic signature of ClassFileBuilder, and add API to it to allow transforming elements produced by a builder handler.

      Problem

      In ASM there is a pattern where a user builds to a builder that immediately delegates all its output through a transform:

      // ASM
      ClassVisitor cv = new DelegateClassVisitor(new ClassWriter(...));
      cv.visitXxx(); // write elements through the delegate

      Such a pattern is not possible with current ClassFile API without advanced hacks.

      A workaround for CodeBuilder for such usages exist: it is CodeBuilder::transforming(CodeTransform, Consumer). We can base the new API off this one.

      We wish to introduce this to the base ClassFileBuilder class, yet the signature must represent a ClassFileTransform with a type variable to support this covariant overriding type such as CodeBuilder.

      Solution

      1. Expand the CodeBuilder::transforming API to the base ClassFileBuilder so all builders can enjoy this facility and cover this use pattern.

      2. Declare a new type parameter in ClassFileBuilder that represents the integrated transform type, so CodeBuilder::transforming is actually overridden.

      3. Provide migration helps in a release note on the original issue.

      With this solution, we can emulate the ASM pattern with:

      // ClassFile API
      cf.build(..., clb0 -> clb0.transforming((clb, cle) -> /*process */, clb -> {
          // write elements through delegate
      }));

      Specification

      See attachment.

            liach Chen Liang
            liach Chen Liang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: