eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
149 stars 117 forks source link

NPE in context of statements before constructor call #2466

Open stephan-herrmann opened 1 month ago

stephan-herrmann commented 1 month ago

I played with code like this ...

public class TestFlow {
    TestFlow() {
        throw new RuntimeException();
        super();
    }
    TestFlow(boolean f) {
        if (f)
            super();
        else // remove this line to see the crash
            this();
    }
}

... initially to challenge where we still report "Constructor call must be the first statement in a constructor", despite the preview being enabled (just a matter of phrasing).

But then the compiler crashed with :

java.lang.NullPointerException: Cannot read field "thrownExceptions" because "this.binding" is null
    at org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall.analyseCode(ExplicitConstructorCall.java:120)
    at org.eclipse.jdt.internal.compiler.ast.IfStatement.analyseCode(IfStatement.java:115)
    at org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration.analyseCode(ConstructorDeclaration.java:183)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.internalAnalyseCode(TypeDeclaration.java:956)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.analyseCode(TypeDeclaration.java:308)
    at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.analyseCode(CompilationUnitDeclaration.java:137)
    at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:910)
    at org.eclipse.jdt.internal.compiler.ProcessTaskManager.run(ProcessTaskManager.java:145)
    at java.base/java.lang.Thread.run(Thread.java:1570)
stephan-herrmann commented 1 month ago

... initially to challenge where we still report "Constructor call must be the first statement in a constructor", despite the preview being enabled (just a matter of phrasing).

outsourced to #2468