Re: javac to emit static flag for local and anonymous classes in static contexts

2023-05-22 Thread Archie Cobbs
Hi Liang,

On Sun, May 21, 2023 at 11:29 PM -  wrote:

> Thus, I suggest emitting static flags for the InnerClasses attribute
> of anonymous/local classes in static/pre-initialization contexts,
>

Related to your question on PR#13656 ... local/anonymous classes in a
pre-initialization context (which we're now calling a "pre-construction"
context) are not static, they're just not instantiable until after
superclass construction.

-Archie

-- 
Archie L. Cobbs


javac to emit static flag for local and anonymous classes in static contexts

2023-05-21 Thread -
Hello,
While looking at https://bugs.openjdk.org/browse/JDK-8162500 and
https://bugs.openjdk.org/browse/JDK-8162501 I realized that the static
flag in InnerClasses attribute alone is not sufficient to check if an
inner class is static; this flag is simply not emitted on local and
anonymous classes.

A simple workaround would be to check the EnclosingMethod attribute
and check if the enclosing method is static. However, this does not
always work:
Local/anonymous classes can be declared in an initializer block
(instance or static) and anonymous classes can be declared in variable
initialization statements.
In this case, the EnclosingMethod attribute can only report the
enclosing class, since the class appears in multiple methods (for
) or non-reflectable method (), and simple attempts to
guess will fail.
Moreover, with the upcoming JEP 447 that adds a pre-initialization
context, local/anonymous classes in the constructor may potentially be
static before the super constructor call, and be instance after the
super constructor call, which complicates the guessing problem.

Thus, I suggest emitting static flags for the InnerClasses attribute
of anonymous/local classes in static/pre-initialization contexts, in
order to allow core reflection to reliably detect the receiver class
of a local or anonymous class. It may also have a cascading effect on
the nested classes within these local/anonymous classes, as shown in
JDK-8162500.

Chen Liang