[code-reflection] RFR: Split support for reflectable lambdas from lambda metafactory
Paul Sandoz
psandoz at openjdk.org
Thu Nov 20 19:29:20 UTC 2025
On Thu, 20 Nov 2025 18:59:06 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> This PR introduces a new class, called `ReflectableLambdaMetafactory`, which lives in the incubating code reflection module.
>
> All capture requests for reflectable lambdas are now re-routed by javac to the new bootstrap methods defined in this class. This allows us to evolve the bootstrap protocol for reflectable lambda without negatively affecting other, non-reflectable lambdas (esp. at startup).
>
> Some magic was required to wire up the pieces: javac cannot refer to symbols in the incubating module, so I decided to let `ReflectMethods` leave a trail of crumbs for `LambdaToMethod` to go on (as `LambdaToMethod` needs to know the name of the class in which the bootstrap method lives).
>
> Some restructuring to `LambdaMetafactory` was also necessary, so that we could access the implementation internals from the new class. The new `xyzInternal` methods are exposed through `SharedSecrects` and are invoked by the reflectable metafactory, which also passes up extra information that the metafactory implementation needs to generate the additional code for reflectable lambdas.
>
> This also allowed me to get rid of the `CodeReflectionSupport` class in `InnerClassLambdaMetafactory` and the related module layer dance.
Nice update.
When the incubating module is not part of the module graph we should now get a direct linkage error, which should be clearer than than some indirect linkage error from within LMF.
src/jdk.incubator.code/share/classes/jdk/incubator/code/bytecode/BytecodeGenerator.java line 937:
> 935: .map(Value::type).map(BytecodeGenerator::toClassDesc).toArray(ClassDesc[]::new);
> 936: int lambdaIndex = lambdaSink.size();
> 937: DirectMethodHandleDesc lambdaMetafactory = DMHD_LAMBDA_METAFACTORY;
Memo for later consideration (separate to this PR). To support serializable lambdas we will need more state on `LambdaOp` like we already have for reflectable lambdas, so we can reliably use the alt factory with the `Serializable` interface.
-------------
Marked as reviewed by psandoz (Lead).
PR Review: https://git.openjdk.org/babylon/pull/688#pullrequestreview-3489522272
PR Review Comment: https://git.openjdk.org/babylon/pull/688#discussion_r2547361699
More information about the babylon-dev
mailing list