[code-reflection] RFR: Fix issues with local class creation from reflectable constructs

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Dec 3 17:47:37 UTC 2025


Instantiation of local class from reflectable methods/lambdas is generally supported (although we do not support code reflection on local class members themselves).

However, when testing I found there are many gaps in the current story:

* existing support works or not depending on _where_ the local class is defined
* when the local class to create contains its own captures, and the local class is inside a lambda expression, we just fail with `UnsupportedOperationException` (!!)
* when a lambda tries to allocate a local class that was declared outside the lambda, the lambda code has no idea of which captures should come with the local class
* when a lambda tries to allocate a regular member inner class with an implicit enclosing `this`, the lambda itself is not marked as capturing `this`, so model generation fails

I addressed (I hope :-) ) all the above issues, and added several tests to check correctness.

The main fix is to share the code to compute the local captures associated with a local class. We should compute such captures whenever we see a class declaration, both in `ReflectMethods` and in all its companion visitors -- otherwise the risk is that we will run some code that will require knowledge of the local class capture, but such knowledge is not available yet.

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

Commit messages:
 - Only compute local var captures if not already computed
 - Initial push

Changes: https://git.openjdk.org/babylon/pull/723/files
  Webrev: https://webrevs.openjdk.org/?repo=babylon&pr=723&range=00
  Stats: 169 lines in 2 files changed: 147 ins; 17 del; 5 mod
  Patch: https://git.openjdk.org/babylon/pull/723.diff
  Fetch: git fetch https://git.openjdk.org/babylon.git pull/723/head:pull/723

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


More information about the babylon-dev mailing list