-
Type:
Bug
-
Resolution: Duplicate
-
Priority:
P3
-
None
-
Affects Version/s: 16, 23
-
Component/s: tools
ADDITIONAL SYSTEM INFORMATION :
Ubuntu 24.04.3 LTS
Kernel version 6.14.0-35-generic
javac 21.0.9
A DESCRIPTION OF THE PROBLEM :
Compiler is not accusing illegal use of local class constructor in a static context when using method reference. The compiler fails with a NPE crash if this happens within a closure.
This is related to https://bugs.openjdk.org/browse/JDK-8333313, and fix was confirmed in JDK 24
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Javac reports a compile error when trying to reference a constructor of a Local class in a static context, such as:
```
error: local class Local cannot be instantiated from a static context
```
ACTUAL -
Javac crashes with the exception:
```
An exception has occurred in the compiler (21.0.9). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.NullPointerException: Cannot read field "sym" because "this.lvar[0]" is null
at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:568)
at jdk.compiler/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2368)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2715)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:910)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:2053)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1871)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1909)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:635)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:686)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.doVisitBlock(Gen.java:1138)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1117)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:975)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:938)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2506)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:769)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1710)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1678)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:976)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
```
---------- BEGIN SOURCE ----------
import java.util.function.Supplier;
class TestCaseJavac21NpeCrash {
public static void main(final String[] args) {
class Local {
static Supplier<Local> staticFunction() {
return Local::new;
}
public String[] closure() {
return args;
}
}
}
}
---------- END SOURCE ----------
Ubuntu 24.04.3 LTS
Kernel version 6.14.0-35-generic
javac 21.0.9
A DESCRIPTION OF THE PROBLEM :
Compiler is not accusing illegal use of local class constructor in a static context when using method reference. The compiler fails with a NPE crash if this happens within a closure.
This is related to https://bugs.openjdk.org/browse/JDK-8333313, and fix was confirmed in JDK 24
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Javac reports a compile error when trying to reference a constructor of a Local class in a static context, such as:
```
error: local class Local cannot be instantiated from a static context
```
ACTUAL -
Javac crashes with the exception:
```
An exception has occurred in the compiler (21.0.9). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.NullPointerException: Cannot read field "sym" because "this.lvar[0]" is null
at jdk.compiler/com.sun.tools.javac.jvm.Code.emitop0(Code.java:568)
at jdk.compiler/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2368)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2715)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genArgs(Gen.java:910)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitNewClass(Gen.java:2053)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCNewClass.accept(JCTree.java:1871)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:885)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitReturn(Gen.java:1909)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:635)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:686)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.doVisitBlock(Gen.java:1138)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1117)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:649)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:975)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:938)
at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:614)
at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2506)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:769)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1710)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1678)
at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:976)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:319)
at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:178)
at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
```
---------- BEGIN SOURCE ----------
import java.util.function.Supplier;
class TestCaseJavac21NpeCrash {
public static void main(final String[] args) {
class Local {
static Supplier<Local> staticFunction() {
return Local::new;
}
public String[] closure() {
return args;
}
}
}
}
---------- END SOURCE ----------
- caused by
-
JDK-8254321 Allow static members to be declared in inner classes
-
- Closed
-
- duplicates
-
JDK-8322882 Null pointer error when compiling Static initializer in a local class
-
- Resolved
-