在UI线程中注册Saveable时出现ClassCastException

时间:2014-07-17 17:48:37

标签: java eclipse multithreading eclipse-plugin

ISaveablesLifecycleListener的{​​{3}}表示在向Saveable添加ISaveablesSource时,必须在UI线程上调用handleLifecycleEvent

然而,当我这样做时:

PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
    @Override
    public void run() {
      externalSaveable = saveable;
      ISaveablesLifecycleListener lifecycleListener = (ISaveablesLifecycleListener)getSite().getService(ISaveablesLifecycleListener.class);
      lifecycleListener.handleLifecycleEvent(
          new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, new Saveable[] {saveableToRegister}, false));
    }
  });

我得到以下异常:

org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastException: abstractTextEditor.editor.AbstractTextEditor$2 cannot be cast to org.eclipse.ui.ISaveablesSource)
at org.eclipse.swt.SWT.error(SWT.java:4397)
at org.eclipse.swt.SWT.error(SWT.java:4312)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:138)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4145)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3762)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:140)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:611)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
Caused by: java.lang.ClassCastException: abstractTextEditor.editor.AbstractTextEditor$2 cannot be cast to org.eclipse.ui.ISaveablesSource
at org.eclipse.ui.internal.SaveablesList.handleLifecycleEvent(SaveablesList.java:215)
at abstractTextEditor.editor.AbstractTextEditor$2.run(AbstractTextEditor.java:865)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
... 24 more

(其中AbstractTextEditor不是eclipse AbstractTextEditor,而是我自己的编辑继承自TextEditor)。

从当前线程调用handleLifecycleEvent时,我不会收到此异常。我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果您仔细查看ClassCastException,请说AbstractTextEditor$2 - 请注意最后的$2

您尝试在此处使用匿名Runnable课程作为ISaveablesSource

new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, new Saveable[] {saveableToRegister}, false));

thisRunnable。您需要使用AbstractTextEditor.this传递编辑器:

new SaveablesLifecycleEvent(AbstractTextEditor.this, SaveablesLifecycleEvent.POST_OPEN, new Saveable[] {saveableToRegister}, false));

您确定需要使用asyncExec吗?文本编辑器的大多数方法都已在用户线程中运行。