Eclipse RCP:调用perspectiveChanged会导致stackoverflow

时间:2016-01-12 14:26:22

标签: java eclipse-plugin eclipse-rcp

我有一个Eclipse RCP应用程序,我从Eclipse 3.0迁移到4.4,到目前为止工作正常。由于迁移,添加了一些菜单条目(例如通用打开文件),我想删除它们。这项工作到目前为止,因为我在hideActionset(...)方法中调用了perspectiveActivated()

菜单条目消失。但是当我重置透视图然后更改透视图时,菜单条目会再次出现。我尝试通过在hideActionSet(...)方法中调用perspectiveChanged(...)来解决此问题,但是我们经常调用它们以获得堆栈溢出。

    /**
     * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
     */
    public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) 
    {
        if( perspective.getId().equals(m_PerspektivenID) || m_PerspektivenID.equals(STANDARDAKTION))
        {
            setEnabled( true );
        }
        else
        {
            setEnabled( false );
        }

        if(page != null)
        {
            page.hideActionSet("org.eclipse.ui.actionSet.keyBindings");
            page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
        }
    }

    /**
     * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
     */
    public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) 
    {
        if(page != null)
        {
            page.hideActionSet("org.eclipse.ui.actionSet.keyBindings");
            page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
        }
    }

这是剪辑正在生成的两个日志文件之一:

!ENTRY org.eclipse.equinox.event 4 0 2016-01-12 14:37:00.768
!MESSAGE Exception while dispatching event org.osgi.service.event.Event [topic=org/eclipse/e4/ui/model/ui/UIElement/widget/SET] to handler org.eclipse.e4.ui.services.internal.events.UIEventHandler@faec277
!STACK 0
java.lang.StackOverflowError
    at java.util.HashMap.hash(HashMap.java:338)
    at java.util.HashMap.containsKey(HashMap.java:595)
    at java.util.Collections$SynchronizedMap.containsKey(Collections.java:2578)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.set(EclipseContext.java:347)
    at org.eclipse.e4.ui.internal.services.ContextContextService.setEventCaching(ContextContextService.java:129)
    at org.eclipse.e4.ui.internal.services.ContextContextService.deferUpdates(ContextContextService.java:86)
    at org.eclipse.ui.internal.contexts.ContextService.deferUpdates(ContextService.java:92)
    at org.eclipse.ui.internal.Perspective.removeActionSet(Perspective.java:362)
    at org.eclipse.ui.internal.WorkbenchPage.hideActionSet(WorkbenchPage.java:2593)
    at packagenane.classname.perspectiveChanged(BaseAction.java:146)
    at org.eclipse.ui.internal.PerspectiveListenerList$4.run(PerspectiveListenerList.java:134)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:867)
    at org.eclipse.ui.internal.PerspectiveListenerList.fireEvent(PerspectiveListenerList.java:58)
    at org.eclipse.ui.internal.PerspectiveListenerList.firePerspectiveChanged(PerspectiveListenerList.java:131)
    at org.eclipse.ui.internal.WorkbenchWindow.firePerspectiveChanged(WorkbenchWindow.java:1721)
    at org.eclipse.ui.internal.WorkbenchPage.hideActionSet(WorkbenchPage.java:2596)
    at packagenane.classname.perspectiveChanged(BaseAction.java:146)
    at org.eclipse.ui.internal.PerspectiveListenerList$4.run(PerspectiveListenerList.java:134)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:867)
    at org.eclipse.ui.internal.PerspectiveListenerList.fireEvent(PerspectiveListenerList.java:58)
    at org.eclipse.ui.internal.PerspectiveListenerList.firePerspectiveChanged(PerspectiveListenerList.java:131)
    at org.eclipse.ui.internal.WorkbenchWindow.firePerspectiveChanged(WorkbenchWindow.java:1721)
    at org.eclipse.ui.internal.WorkbenchPage.hideActionSet(WorkbenchPage.java:2596)
    at packagenane.classname.perspectiveChanged(BaseAction.java:146)
    at org.eclipse.ui.internal.PerspectiveListenerList$4.run(PerspectiveListenerList.java:134)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:867)
    at org.eclipse.ui.internal.PerspectiveListenerList.fireEvent(PerspectiveListenerList.java:58)
    at org.eclipse.ui.internal.PerspectiveListenerList.firePerspectiveChanged(PerspectiveListenerList.java:131)
    at org.eclipse.ui.internal.WorkbenchWindow.firePerspectiveChanged(WorkbenchWindow.java:1721)
    at org.eclipse.ui.internal.WorkbenchPage.hideActionSet(WorkbenchPage.java:2596)
    at packagenane.classname.perspectiveChanged(BaseAction.java:146)
    at org.eclipse.ui.internal.PerspectiveListenerList$4.run(PerspectiveListenerList.java:134)

1 个答案:

答案 0 :(得分:1)

如果您查看堆栈跟踪,您可以清楚地看到,当您致电WorkbenchPage.hideActionSet时,正在触发新的透视更改事件,并且当您仍在第一个perspectiveChanged时再次调用perspectiveChanged 1}}来电。

停止此操作的一种方法是在perspectiveChanged方法中设置一个标记,以检测您是否已在处理更改。

类似的东西:

private boolean changeActive;

public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) 
{
  if (changeActive) {   // Don't do anything if already handling change
    return;
  }

  changeActive = true;

  if (page != null)
    {
        page.hideActionSet("org.eclipse.ui.actionSet.keyBindings");
        page.hideActionSet("org.eclipse.ui.actionSet.openFiles");
    }

  changeActive = false;
}