From e90c9a62fcea781c775dc6cfe27387953a594682 Mon Sep 17 00:00:00 2001 From: Ed Merks Date: Fri, 17 Jan 2025 15:49:19 +0100 Subject: [PATCH] Avoid FindReplaceOverlay setTextEditorActionsActivated resulting in NPE - The FindReplaceOverlay's setTextEditorActionsActivated method reflectively invokes AbstractTextEditor.setActionActivation(boolean) and that method assumes that getEditorSite().getActionBarContributor() returns non-null but MultiPageEditorSite.getActionBarContributor() returns null so in PDE's target editor, this leads to an NPE. - AbstractTextEditor.setActionActivation(boolean) must guard against a null action contributor to avoid NPE and instead should get and use the action bar contributor of the containing multi-page editor. --- .../ui/texteditor/AbstractTextEditor.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java index a518ed85f3a..cd796b8595d 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java @@ -188,6 +188,7 @@ import org.eclipse.jface.text.source.VerticalRuler; import org.eclipse.ui.IActionBars; +import org.eclipse.ui.IEditorActionBarContributor; import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; @@ -5212,10 +5213,15 @@ public void setAction(String actionID, IAction action) { /** * Sets this editor's actions into activated (default) or deactived state. *

- * XXX: This is called by the Java editor for its breadcrumb feature. We - * don't want to make this risky method API because the Java editor - * breadcrumb might become a Platform UI feature during 3.5 and hence we can - * then delete this workaround. + * XXX: This is called by the Java editor for its breadcrumb feature. We don't + * want to make this risky method API because the Java editor breadcrumb might + * become a Platform UI feature during 3.5 and hence we can then delete this + * workaround. + *

+ *

+ * This is also called reflectively by + * org.eclipse.ui.internal.findandreplace.overlay.FindReplaceOverlay.targetActionActivationHandling.new + * FocusListener() {...}.setTextEditorActionsActivated(boolean). *

* * @param state true if activated @@ -5230,9 +5236,9 @@ private void setActionActivation(boolean state) { if (action != null) fActivationCodeTrigger.registerActionForKeyActivation(action); } - getEditorSite().getActionBarContributor().setActiveEditor(this); + setActiveEditor(this); } else { - getEditorSite().getActionBarContributor().setActiveEditor(null); + setActiveEditor(null); Iterator iter= fActions.values().iterator(); while (iter.hasNext()) { IAction action= iter.next(); @@ -5243,6 +5249,21 @@ private void setActionActivation(boolean state) { } } + private void setActiveEditor(IEditorPart targetEditor) { + IEditorSite editorSite = getEditorSite(); + IEditorActionBarContributor actionBarContributor = editorSite.getActionBarContributor(); + // Handle that MultiPageEditorSite.getActionBarContributor() returns null. + if (actionBarContributor == null) { + if (editorSite instanceof MultiPageEditorSite multiPageEditorSite) { + actionBarContributor = multiPageEditorSite.getMultiPageEditor().getEditorSite() + .getActionBarContributor(); + } + } + if (actionBarContributor != null) { + actionBarContributor.setActiveEditor(targetEditor); + } + } + private static final boolean HACK_TO_SUPPRESS_UNUSUED_WARNING= false; { if (HACK_TO_SUPPRESS_UNUSUED_WARNING)