eclipse-jdt / eclipse.jdt.ui

Eclipse Public License 2.0
35 stars 86 forks source link

Pasting snippet to Package Explorer does not work anymore #1414

Closed iloveeclipse closed 3 months ago

iloveeclipse commented 3 months ago

Just stumbled over this bug: copy text below into clipboard

    public static void main(String[] args) throws Exception {
        while(true) {
            Thread.sleep(500);
        }
    }

and try to paste it (Ctrl+V) into Package Explorer.

org.eclipse.e4.ui.workbench Error Tue May 21 17:46:48 CEST 2024 Execution exception for: ParameterizedCommand(Command(org.eclipse.ui.edit.paste,Paste, Paste from the clipboard, Category(org.eclipse.ui.category.edit,Edit,null,true), WorkbenchHandlerServiceHandler("org.eclipse.ui.edit.paste"), ,,true),null) in context chain: WorkbenchContext -> TrimmedWindowImpl (IDEWindow) Context -> PerspectiveImpl (org.eclipse.jdt.ui.JavaPerspective) Context -> PartImpl (org.eclipse.jdt.ui.PackageExplorer) Context

org.eclipse.core.commands.ExecutionException: While executing the action, an exception occurred at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:129) at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:98) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:58) at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:299) at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:233) at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:174) at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:165) at org.eclipse.core.commands.Command.executeWithChecks(Command.java:488) at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:485) at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:204) at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:308) at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:569) at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:644) at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:439) at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:96) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91) at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1954) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1616) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1643) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1626) at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1665) at org.eclipse.swt.widgets.Widget.gtk_key_press_event(Widget.java:994) at org.eclipse.swt.widgets.Control.gtk_key_press_event(Control.java:3999) at org.eclipse.swt.widgets.Composite.gtk_key_press_event(Composite.java:931) at org.eclipse.swt.widgets.Tree.gtk_key_press_event(Tree.java:2359) at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:2595) at org.eclipse.swt.widgets.Control.windowProc(Control.java:6833) at org.eclipse.swt.widgets.Tree.windowProc(Tree.java:4272) at org.eclipse.swt.widgets.Display.windowProc(Display.java:6162) at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_do_event(Native Method) at org.eclipse.swt.widgets.Display.eventProc(Display.java:1598) at org.eclipse.swt.internal.gtk3.GTK3.gtk_main_iteration_do(Native Method) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4514) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1151) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1042) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:152) at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:639) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:546) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:208) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:143) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:109) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:439) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:271) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:668) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:605) at org.eclipse.equinox.launcher.Main.run(Main.java:1481) at org.eclipse.equinox.launcher.Main.main(Main.java:1454) Caused by: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.ImplicitTypeDeclaration cannot be cast to class org.eclipse.jdt.core.dom.AbstractTypeDeclaration (org.eclipse.jdt.core.dom.ImplicitTypeDeclaration and org.eclipse.jdt.core.dom.AbstractTypeDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1745d77d) at org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction$TextPaster$ParsedCu.parseAsTypes(PasteAction.java:438) at org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction$TextPaster$ParsedCu.parseCus(PasteAction.java:381) at org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction$TextPaster.parseCUs(PasteAction.java:1077) at org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction$TextPaster.canPasteOn(PasteAction.java:588) at org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction.run(PasteAction.java:269) at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchRun(SelectionDispatchAction.java:274) at org.eclipse.jdt.ui.actions.SelectionDispatchAction.run(SelectionDispatchAction.java:252) at org.eclipse.jface.action.Action.runWithEvent(Action.java:474) at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:124) ... 54 more



I assume this is regression from https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2066 and either `org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction` or JDT core needs a fix.
iloveeclipse commented 3 months ago

I see that the code in PasteAction casts org.eclipse.jdt.core.dom.CompilationUnit.types() to List<AbstractTypeDeclaration> because CompilationUnit.types() javadoc says its element type is AbstractTypeDeclaration.

This seem to be not the case anymore.

So at least javadoc in org.eclipse.jdt.core.dom.CompilationUnit must be fixed, and most likely the code in PasteAction that uses CompilationUnit.types() result.

I also wonder if there are more clients assumed wrong type on the CompilationUnit.types() content?

iloveeclipse commented 3 months ago

Note, this issue must be fixed on UI side, as the logic changed not only around type of the element but also handling of "snippet without type" parsed as K_COMPILATION_UNIT and not more as K_CLASS_BODY_DECLARATIONS => https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/1416

The main underlined issue is separated to https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2481