[code-reflection] RFR: Add support for local classes [v2]
Maurizio Cimadamore
mcimadamore at openjdk.org
Tue Jun 4 17:47:32 UTC 2024
> This PR adds support for local and anonymous instance class creation in the code model.
>
> This is a bit trickier than inner classes in that local classes can also capture local variables from enclosing scopes. And, to make things even trickier, some local classes can be used in a *pre-construction* environment, that is in a location where `this` is not available yet (e.g. because we're in the middle of a `super` constructor call).
>
> In this PR, we only add support for local and anonymous class that do **not** appear in a pre-construction context. The logic to compute the set of captured local is shared with `Lower` (as part of Java 8 that logic has been somewhat generalized to accommodate different clients, as lambda generation also needs to know the set of variables captured by a local class). The resulting captures are then added as arguments of the `new` operation, and the constructor signature is tweaked accordingly.
>
> For instance, given this:
>
>
> String testLocalCaptureParam(String s) {
> class Foo {
> String m() { return s; }
> }
> return new Foo().m();
> }
>
>
> The following model is now generated:
>
>
> func @"testLocalCaptureParam" (%0 : LocalClassTest, %1 : java.lang.String)java.lang.String -> {
> %2 : Var<java.lang.String> = var %1 @"s";
> %3 : java.lang.String = var.load %2;
> %4 : .<LocalClassTest, LocalClassTest$2Foo> = new %0 %3 @"func<.<LocalClassTest, LocalClassTest$2Foo>, LocalClassTest, java.lang.String>";
> %5 : java.lang.String = invoke %4 @".<LocalClassTest, LocalClassTest$2Foo>::m()java.lang.String";
> return %5;
> };
>
>
> I've added a bunch of tests to check the shape of the resulting code model, plus a dynamic test to make sure that we can indeed build local classes by interpreting said model.
Maurizio Cimadamore has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains one commit:
Initial push
-------------
Changes: https://git.openjdk.org/babylon/pull/111/files
Webrev: https://webrevs.openjdk.org/?repo=babylon&pr=111&range=01
Stats: 367 lines in 7 files changed: 335 ins; 11 del; 21 mod
Patch: https://git.openjdk.org/babylon/pull/111.diff
Fetch: git fetch https://git.openjdk.org/babylon.git pull/111/head:pull/111
PR: https://git.openjdk.org/babylon/pull/111
More information about the babylon-dev
mailing list