eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
161 stars 130 forks source link

CCE while typing try-with-resources #1825

Closed stephan-herrmann closed 5 months ago

stephan-herrmann commented 9 months ago

The following exception occurred:

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding cannot be cast to class org.eclipse.jdt.internal.compiler.lookup.VariableBinding (org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding and org.eclipse.jdt.internal.compiler.lookup.VariableBinding are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @6030eff1)
    at org.eclipse.jdt.internal.compiler.ast.TryStatement.generateCode(TryStatement.java:582)
    at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.generateCode(AbstractMethodDeclaration.java:393)
    at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.generateCode(AbstractMethodDeclaration.java:327)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.generateCode(TypeDeclaration.java:759)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.generateCode(TypeDeclaration.java:829)
    at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.generateCode(CompilationUnitDeclaration.java:414)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1327)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:790)
    at org.eclipse.jdt.core.dom.ASTParser.internalCreateASTCached(ASTParser.java:1242)
    at org.eclipse.jdt.core.dom.ASTParser.lambda$0(ASTParser.java:1120)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5821)
    at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1120)
    at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:868)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider$1.run(CoreASTProvider.java:294)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider.createAST(CoreASTProvider.java:286)
    at org.eclipse.jdt.core.manipulation.CoreASTProvider.getAST(CoreASTProvider.java:199)
    at org.eclipse.jdt.core.manipulation.SharedASTProviderCore.getAST(SharedASTProviderCore.java:138)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:167)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$1.lambda$0(SelectionListenerWithASTManager.java:149)
    at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5821)
    at org.eclipse.jdt.internal.core.JavaModelManager.callReadOnlyUnchecked(JavaModelManager.java:5809)
    at org.eclipse.jdt.internal.core.JavaModelManager.callReadOnly(JavaModelManager.java:5797)
    at org.eclipse.jdt.core.JavaCore.callReadOnly(JavaCore.java:6124)
    at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$1.run(SelectionListenerWithASTManager.java:149)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

while I was typing something like

protected Document readXMLFile_safe(URL url) {
        InputStream is = null;
        InputSource source = null;

        try (InputStream ){
...

Obviously, the incomplete resource declaration caused unexpected resolving to a type. But why does the compiler proceed to generating code when the source is so buggy? Or is only the CompilationUnitResolver doing this. Yes, I probably was hovering over the incomplete code.

stephan-herrmann commented 5 months ago

But why does the compiler proceed to generating code when the source is so buggy?

Because nobody implemented validation whether a resource variable (since 9) really is a variable, d'uh.