Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 578244

Summary: [compiler] Unhandled event loop exception in AST while hovering over method (ClassCastException)
Product: [Eclipse Project] JDT Reporter: Juergen Baier <baier>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: jarthana, misc, xhamcio
Version: 4.23   
Target Milestone: 4.24 M3   
Hardware: PC   
OS: Mac OS X   
See Also: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/192503
Whiteboard:

Description Juergen Baier CLA 2022-01-17 10:33:16 EST
With the code below I get an "unhandled event loop exception" while hovering over getKey().

The error log shows:

java.lang.ClassCastException: class org.eclipse.jdt.internal.compiler.ast.LocalDeclaration cannot be cast to class org.eclipse.jdt.internal.compiler.ast.ForeachStatement (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration and org.eclipse.jdt.internal.compiler.ast.ForeachStatement are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @71b75f99)
	at org.eclipse.jdt.internal.compiler.parser.Parser.consumeEnhancedForStatement(Parser.java:3445)
	at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:8455)
	at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13038)
	at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2056)
	at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:1896)
	at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:349)
	at org.eclipse.jdt.internal.codeassist.impl.Engine.parseBlockStatements(Engine.java:312)
	at org.eclipse.jdt.internal.codeassist.SelectionEngine.select(SelectionEngine.java:1080)
	at org.eclipse.jdt.internal.core.Openable.codeSelect(Openable.java:167)
	at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:389)
	at org.eclipse.jdt.internal.core.CompilationUnit.codeSelect(CompilationUnit.java:382)
	at org.eclipse.jdt.internal.ui.javaeditor.JavaElementHyperlinkDetector.detectHyperlinks(JavaElementHyperlinkDetector.java:124)
	at org.eclipse.ui.texteditor.HyperlinkDetectorRegistry$HyperlinkDetectorDelegate.detectHyperlinks(HyperlinkDetectorRegistry.java:81)
	at org.eclipse.jface.text.hyperlink.HyperlinkManager.findHyperlinks(HyperlinkManager.java:289)
	at org.eclipse.jface.text.hyperlink.HyperlinkManager.findHyperlinks(HyperlinkManager.java:262)
	at org.eclipse.jface.text.hyperlink.HyperlinkManager.mouseMove(HyperlinkManager.java:457)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:216)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4565)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1522)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1545)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1530)
	at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1324)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4351)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3927)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1154)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1045)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
	at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:136)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:659)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:596)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1467)

This happens with the Java code

package eclipse;

import java.util.Map.Entry;
import java.util.Set;

public class StringToSetNavigate {

    public static void main(String[] args) {
	Set<Foo> foos = Set.of(new Foo(), new Bar());
	for (Foo foo : foos) {
	    String string;
	    if (foo instanceof Bar bar) {
		string = "__";
	    }
	}

	for (Entry<Foo, Set<Integer>> entry : map.entrySet()) {
	    Foo key = entry.getKey(); // <======== Hover over "getKey()"
	}
    }

    static class Foo {
    }

    static class Bar extends Foo {
    }
}

Note: If I change 

	    if (foo instanceof Bar bar) {
		string = "__";
	    }

to

	    if (foo instanceof Bar) {
		string = "__";
	    }

then the error no longer occurs.
Comment 1 Juergen Baier CLA 2022-03-22 04:03:48 EDT
This problem still persists in 4.23. Note that this problem occurs not only in rare and weird Java code. In fact there a multiple classes in our code base which trigger that ClassCastException.
Comment 2 Jay Arthanareeswaran CLA 2022-04-04 07:23:32 EDT
*** Bug 579552 has been marked as a duplicate of this bug. ***
Comment 3 h m CLA 2022-04-04 08:04:58 EDT
The error in this bug is different from the one from here.
Comment 4 Jay Arthanareeswaran CLA 2022-04-04 23:46:59 EDT
(In reply to h m from comment #3)
> The error in this bug is different from the one from here.

Well, what matters is the stack trace. What we have here and bug 579552 is very similar:

at org.eclipse.jdt.internal.compiler.parser.Parser.consumeEnhancedForStatement(Parser.java:3445)
 at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:8455)
 at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13038)
 at org.eclipse.jdt.internal.codeassist.impl.AssistParser.parseBlockStatements(AssistParser.java:2056)

vs

at org.eclipse.jdt.internal.compiler.parser.Parser.consumeEnhancedForStatement(Parser.java:3445)
 at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:8455)
 at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13038)
 at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:13293)
Comment 5 Eclipse Genie CLA 2022-04-05 05:24:02 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/192503
Comment 6 h m CLA 2022-04-05 08:01:25 EDT
After updating I have the same error.
Comment 7 Jay Arthanareeswaran CLA 2022-04-19 06:43:57 EDT
This have been pushed via

https://github.com/eclipse-jdt/eclipse.jdt.core/commit/22b4e4754d4f916394181cb7f1e6aa1c26d7af60
Comment 8 Jay Arthanareeswaran CLA 2022-04-19 06:45:37 EDT
*** Bug 577508 has been marked as a duplicate of this bug. ***
Comment 9 h m CLA 2022-04-19 12:55:10 EDT
I see only ooph update in my Eclipse.
Comment 10 Jay Arthanareeswaran CLA 2022-04-20 02:14:13 EDT
(In reply to h m from comment #9)
> I see only ooph update in my Eclipse.

You can download the latest build from here

https://download.eclipse.org/eclipse/downloads/drops4/I20220419-1800/

or install the JDT update from here:

https://download.eclipse.org/eclipse/updates/4.24-I-builds/I20220419-1800/

to try out the fix.
Comment 11 h m CLA 2022-04-20 15:55:24 EDT
I have the same error after updating.
Comment 12 Jay Arthanareeswaran CLA 2022-04-21 04:45:24 EDT
(In reply to h m from comment #11)
> I have the same error after updating.

That probably means what you reported in 579552 is not same as this bug report. Can you try the testcase given in comment #0 and let me know if that works for you? We need to confirm that your build has the fix. May I ask how did you update? Can you confirm that the plug-in version for jdt.core is indeed is the latest?
Comment 13 h m CLA 2022-04-21 06:59:51 EDT
    Eclipse JDT Plug-in Developer Resources	3.18.1100.v20220308-0310	org.eclipse.jdt.source.feature.group	Eclipse.org
	Eclipse.org	Eclipse Compiler for Java(TM)	3.30.0.v20220419-1041	org.eclipse.jdt.core.compiler.batch

I installed the update from here:
https://download.eclipse.org/eclipse/updates/4.24-I-builds/I20220419-1800/

And I have the error with the code from comment #0.
Comment 14 h m CLA 2022-04-21 07:15:41 EDT
I also have this error when updating:
Cannot complete the install because of a conflicting dependency.
  Software being installed: Eclipse Platform 4.24.0.v20220419-2208 (org.eclipse.platform.feature.group 4.24.0.v20220419-2208)
  Software currently installed: Eclipse IDE for Java Developers 4.23.0.20220310-1200 (epp.package.java 4.23.0.20220310-1200)
  Only one of the following can be installed at once: 
    Proxy for Windows 1.1.100.v20180515-0858 (org.eclipse.core.net.win32.x86_64 1.1.100.v20180515-0858)
    Proxy for Windows 1.1.500.v20190925-1337 (org.eclipse.core.net.win32.x86_64 1.1.500.v20190925-1337)
    Proxy for Windows 1.1.600.v20220312-1450 (org.eclipse.core.net.win32.x86_64 1.1.600.v20220312-1450)
  Cannot satisfy dependency:
    From: Eclipse IDE for Java Developers 4.23.0.20220310-1200 (epp.package.java 4.23.0.20220310-1200)
    To: org.eclipse.equinox.p2.iu; org.eclipse.platform.feature.group [4.23.0.v20220308-0722,4.23.0.v20220308-0722]
  Cannot satisfy dependency:
    From: Eclipse Platform 4.23.0.v20220308-0722 (org.eclipse.platform.feature.group 4.23.0.v20220308-0722)
    To: org.eclipse.equinox.p2.iu; org.eclipse.core.net.win32.x86_64 [1.1.500.v20190925-1337,1.1.500.v20190925-1337]
  Cannot satisfy dependency:
    From: Eclipse Platform 4.24.0.v20220419-2208 (org.eclipse.platform.feature.group 4.24.0.v20220419-2208)
    To: org.eclipse.equinox.p2.iu; org.eclipse.core.net.win32.x86_64 [1.1.600.v20220312-1450,1.1.600.v20220312-1450]
Comment 15 Jay Arthanareeswaran CLA 2022-04-22 00:16:43 EDT
(In reply to h m from comment #13)
>     Eclipse JDT Plug-in Developer Resources	3.18.1100.v20220308-0310
> org.eclipse.jdt.source.feature.group	Eclipse.org
> 	Eclipse.org	Eclipse Compiler for Java(TM)	3.30.0.v20220419-1041
> org.eclipse.jdt.core.compiler.batch
> 
> I installed the update from here:
> https://download.eclipse.org/eclipse/updates/4.24-I-builds/I20220419-1800/
> 
> And I have the error with the code from comment #0.

That doesn't look good. It probably means you still don't have the fix in your setup. What's your current eclipse version?

For verification sake, can you take the whole build mentioned in comment #10 and try again?
Comment 16 h m CLA 2022-04-22 05:03:25 EDT
Ok, but how do I update? Updating from the link and software plugins didn't help.