RFR: 8322882: Null pointer error when compiling Static initializer in a local class
Archie Cobbs
acobbs at openjdk.org
Mon Jul 8 18:05:42 UTC 2024
On Mon, 17 Jun 2024 17:23:03 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:
> A local class can capture a free variable from its enclosing scope. The variable's value is passed to the local class constructor via a synthetic constructor parameter, and then stored in a synthetic proxy field in the local class. Therefore, at any point at which such a local class is instantiated, it must also be possible to access the captured free variable's value so it can be passed to the constructor.
>
> The compiler was incorrectly assuming that within the local class itself, this would always be the case, because of the existence of the synthetic proxy field. Prior to JDK 16, local classes were not allowed to have static methods, so this was a safe assumption. However, that assumption is no longer safe, and violating results in a `NullPointerException`, for example with this class:
>
>
> class LocalFreeVarStaticInstantiate {
> static void foo(Object there) {
> class Local {
> {
> there.hashCode();
> }
> static {
> new Local(); // can't get there from here
> }
> }
> }
> }
>
>
> This patch adds the missing check for the situation where access to a proxy variable instance field is required for a synthetic constructor parameter, but the instantiation is occurring within a static method of the class containing the field and so the field can't be accessed.
>
> **Note:** With this change, the following test fails to compile until Maurizio's refactoring relating to lambda's and outer instances (see "javac-pre-capture" Jira bug label) has been applied:
>
> * `test/langtools/tools/javac/lambda/T8209407/VerifierErrorInnerPlusLambda.java`
Closing this PR as it is superceded by PR #19904.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/19754#issuecomment-2214845972
More information about the compiler-dev
mailing list