[code-reflection] RFR: Simplify code generation for quotable lambdas not to require duplicate capture argument lists [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Oct 7 12:43:26 UTC 2024


> When translating quotable lambdas/method references, we generate two captured argument lists:
> 
> * the captured arguments to be used by the functional interface implementation;
> * the captured arguments to be used by the quotable implementation
> 
> In most cases these two captured lists are identical, except for cosmetic differences.
> This is even more true after the backend of javac underwent significant rearchitecture, to better support the Flexible Constructor Bodies feature.
> 
> This PR simplifies `ReflectMethods` so that a visitor now computes all the required captured variables upfront.
> The captured variables are then inserted in the quoted block, so that the rest of `BodyScanner` will be able to resolve references to such variables w/o the need for ad-hoc logic.
> 
> One complication is that, under this new regime, we can no longer afford to treat captured variables whose value is constant (e.g. `final int k = 100`) as if they were true captures.
> This is because neither `Lower` nor `LambdaToMethod` capture constant variables. Since we're reusing the same dynamic arguments as `LambdaToMethod` we need to make sure that `ReflectMethods` also does not capture constant arguments.
> Special logic to avoid capture of constant arguments is baked into the new visitor.
> 
> I have also added a check in the visitor, so that if, inside a quotable lambda, we see a creation of a local class that is declared outside the lambda, an unsupported exception is thrown.
> 
> I will followup with a separate fix to deal with these cases.

Maurizio Cimadamore has updated the pull request incrementally with three additional commits since the last revision:

 - Update src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ReflectMethods.java
   
   Co-authored-by: Paul Sandoz <paul.d.sandoz at googlemail.com>
 - Address review comments
 - Drop spurious reference to `LambdaToMethod`

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

Changes:
  - all: https://git.openjdk.org/babylon/pull/249/files
  - new: https://git.openjdk.org/babylon/pull/249/files/fdf2cc2c..1ae6a58c

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=babylon&pr=249&range=01
 - incr: https://webrevs.openjdk.org/?repo=babylon&pr=249&range=00-01

  Stats: 49 lines in 3 files changed: 23 ins; 18 del; 8 mod
  Patch: https://git.openjdk.org/babylon/pull/249.diff
  Fetch: git fetch https://git.openjdk.org/babylon.git pull/249/head:pull/249

PR: https://git.openjdk.org/babylon/pull/249


More information about the babylon-dev mailing list