Bug 577622 - Create a new OpenVariableDeclarationAction

This action navigates from a JDIFieldVariable to the actual source code, where that field is declared.

Change-Id: I802a388a31b1f5e192da78526dc977c1b5fa479e
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.debug/+/188550
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Sarika Sinha <sarika.sinha@in.ibm.com>
diff --git a/org.eclipse.jdt.debug.ui/plugin.properties b/org.eclipse.jdt.debug.ui/plugin.properties
index 7a3f817..613c24c 100644
--- a/org.eclipse.jdt.debug.ui/plugin.properties
+++ b/org.eclipse.jdt.debug.ui/plugin.properties
@@ -126,6 +126,9 @@
 openConcreteVarTypeHierarchy.label=Open Actual Type Hierarch&y
 openConcreteVarTypeHierarchy.tooltip=Open the Variable's Actual Implementation Type Hierarchy
 
+openFieldDeclaration.label=Open &Field Declaration
+openFieldDeclaration.tooltip=Open the Declaration of the Field
+
 openRecType.label=Open Actual T&ype
 openRecType.tooltip=Open the Actual Type
 
diff --git a/org.eclipse.jdt.debug.ui/plugin.xml b/org.eclipse.jdt.debug.ui/plugin.xml
index 6e88711..d6ae998 100644
--- a/org.eclipse.jdt.debug.ui/plugin.xml
+++ b/org.eclipse.jdt.debug.ui/plugin.xml
@@ -646,6 +646,22 @@
             objectClass="org.eclipse.jdt.debug.core.IJavaVariable"
             id="org.eclipse.jdt.debug.ui.FilteredJavaVariableActions">
          <filter
+               name="JavaVariableFilter"
+               value="isFieldVariable">
+         </filter>
+         <action
+               label="%openFieldDeclaration.label"
+               tooltip="%openFieldDeclaration.tooltip"
+               class="org.eclipse.jdt.internal.debug.ui.actions.OpenVariableDeclarationAction"
+               menubarPath="emptyNavigationGroup"
+               enablesFor="1"
+               id="org.eclipse.jdt.debug.ui.actions.OpenVariableDeclarationAction">
+         </action>
+      </objectContribution>
+      <objectContribution
+            objectClass="org.eclipse.jdt.debug.core.IJavaVariable"
+            id="org.eclipse.jdt.debug.ui.FilteredJavaVariableActions">
+         <filter
                name="PrimitiveVariableActionFilter"
                value="isPrimitive">
          </filter>
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java
index a871df8..fbf7705 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java
@@ -21,6 +21,7 @@
 import org.eclipse.jdt.debug.core.IJavaArrayType;
 import org.eclipse.jdt.debug.core.IJavaClassType;
 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
+import org.eclipse.jdt.debug.core.IJavaFieldVariable;
 import org.eclipse.jdt.debug.core.IJavaObject;
 import org.eclipse.jdt.debug.core.IJavaType;
 import org.eclipse.jdt.debug.core.IJavaVariable;
@@ -195,6 +196,9 @@
 					if (value.equals("isObjectValue")) { //$NON-NLS-1$
 						return varValue != null && JDIObjectValue.class.isAssignableFrom(varValue.getClass());
 					}
+					if (value.equals("isFieldVariable")) { //$NON-NLS-1$
+						return var instanceof IJavaFieldVariable;
+					}
 				}
 				else if (name.equals("ConcreteVariableActionFilter") && value.equals("isConcrete")) { //$NON-NLS-1$ //$NON-NLS-2$
 					return isDeclaredSameAsConcrete(var);
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenTypeAction.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenTypeAction.java
index eb82441..2537a52 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenTypeAction.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenTypeAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -23,7 +23,6 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.model.IDebugElement;
-import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.search.IJavaSearchConstants;
@@ -55,12 +54,8 @@
 		try {
 			while (itr.hasNext()) {
 				Object element= itr.next();
-				Object sourceElement = resolveSourceElement(element);
-				if (sourceElement != null) {
-					openInEditor(sourceElement);
-				} else {
-					IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
-					throw new CoreException(status);
+				if (openElement(action, element)) {
+					return;
 				}
 			}
 		} catch(CoreException e) {
@@ -68,6 +63,29 @@
 		}
 	}
 
+	/**
+	 * Open the selected element, return true, if further selections should not be checked.
+	 *
+	 * @param action
+	 *            the action proxy that handles the presentation portion of the action
+	 * @param element
+	 *            the selected element.
+	 * @return true, if no other openElement calls should be made. Used, when multiple element is selected, and the action works as trying until one
+	 *         succeeds.
+	 * @throws CoreException
+	 *             if source element is not found.
+	 */
+	protected boolean openElement(IAction action, Object element) throws DebugException, CoreException {
+		IType sourceElement = resolveSourceElement(element);
+		if (sourceElement != null) {
+			openInEditor(element, sourceElement);
+		} else {
+			IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
+			throw new CoreException(status);
+		}
+		return false;
+	}
+
 	protected abstract IDebugElement getDebugElement(IAdaptable element);
 
 	/**
@@ -86,8 +104,8 @@
 	 * @return the source element to open or <code>null</code> if none
 	 * @throws CoreException
 	 */
-	protected Object resolveSourceElement(Object e) throws CoreException {
-		Object source = null;
+	protected IType resolveSourceElement(Object e) throws CoreException {
+		IType source = null;
 		IAdaptable element= (IAdaptable) e;
 		IDebugElement dbgElement= getDebugElement(element);
 		if (dbgElement != null) {
@@ -107,20 +125,11 @@
 		return source;
 	}
 
-	protected void openInEditor(Object sourceElement) throws CoreException {
+	protected void openInEditor(Object element, IType sourceElement) throws CoreException {
 		if (isHierarchy()) {
-			if (sourceElement instanceof IJavaElement) {
-				OpenTypeHierarchyUtil.open((IJavaElement)sourceElement, getWorkbenchWindow());
-			} else {
-				typeHierarchyError();
-			}
+			OpenTypeHierarchyUtil.open(sourceElement, getWorkbenchWindow());
 		} else {
-			if(sourceElement instanceof IJavaElement) {
-				JavaUI.openInEditor((IJavaElement) sourceElement);
-			}
-			else {
-				showErrorMessage(ActionMessages.OpenTypeAction_2);
-			}
+			JavaUI.openInEditor(sourceElement);
 		}
 	}
 
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeAction.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeAction.java
index edf4d29..72e1048 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeAction.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2014 IBM Corporation and others.
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -14,12 +14,12 @@
 package org.eclipse.jdt.internal.debug.ui.actions;
 
 
-import java.util.Iterator;
-
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.model.IDebugElement;
+import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.debug.core.IJavaType;
 import org.eclipse.jdt.debug.core.IJavaValue;
 import org.eclipse.jdt.debug.core.IJavaVariable;
@@ -27,9 +27,7 @@
 import org.eclipse.jdt.internal.debug.core.model.JDIInterfaceType;
 import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
 import org.eclipse.jdt.internal.debug.core.model.JDIVariable;
-import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
 import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.IStructuredSelection;
 
 /**
  * Opens the concrete type of variable - i.e. it's value's actual type.
@@ -48,38 +46,34 @@
 		return null;
 	}
 
-
 	@Override
-	public void run(IAction action) {
-		IStructuredSelection selection = getCurrentSelection();
-		if (selection == null) {
-			return;
-		}
-		Iterator<?> itr = selection.iterator();
-		try {
-			while (itr.hasNext()) {
-				Object element = itr.next();
-				if (element instanceof JDIVariable && ((JDIVariable) element).getJavaType() instanceof JDIInterfaceType) {
-					JDIObjectValue val = (JDIObjectValue) ((JDIVariable) element).getValue();
-					if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
-						OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeAction();
-						declaredAction.setActivePart(action, getPart());
-						declaredAction.run(action);
-						return;
-					}
-				}
-				Object sourceElement = resolveSourceElement(element);
-				if (sourceElement != null) {
-						openInEditor(sourceElement);
-				} else {
-						IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
-						throw new CoreException(status);
+	protected boolean openElement(IAction action, Object element) throws DebugException, CoreException {
+		if (element instanceof JDIVariable) {
+			final var jdiVariable = (JDIVariable) element;
+			if (isInterfaceType(jdiVariable)) {
+				final var val = (JDIObjectValue) jdiVariable.getValue();
+				if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
+					OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeAction();
+					declaredAction.setActivePart(action, getPart());
+					declaredAction.run(action);
+					return true;
 				}
 			}
 		}
-		catch (CoreException e) {
-			JDIDebugUIPlugin.statusDialog(e.getStatus());
+		IType sourceElement = resolveSourceElement(element);
+		if (sourceElement != null) {
+			openInEditor(element, sourceElement);
+			return false;
 		}
+		IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
+		throw new CoreException(status);
 	}
 
+	private boolean isInterfaceType(JDIVariable jdiVariable) {
+		try {
+			return jdiVariable.getJavaType() instanceof JDIInterfaceType;
+		} catch (DebugException e) {
+			return false;
+		}
+	}
 }
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeHierarchyAction.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeHierarchyAction.java
index 115ec19..6123723 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeHierarchyAction.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableConcreteTypeHierarchyAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2014 IBM Corporation and others.
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -13,21 +13,8 @@
  *******************************************************************************/
 package org.eclipse.jdt.internal.debug.ui.actions;
 
-import java.util.Iterator;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
-import org.eclipse.jdt.internal.debug.core.model.JDIInterfaceType;
-import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
-import org.eclipse.jdt.internal.debug.core.model.JDIVariable;
-import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.IStructuredSelection;
-
 /**
- * Opens the concrete type hierarhcy of variable - i.e. it's value's actual type.
+ * Opens the concrete type hierarchy of variable - i.e. it's value's actual type.
  */
 public class OpenVariableConcreteTypeHierarchyAction extends OpenVariableConcreteTypeAction {
 
@@ -39,36 +26,4 @@
 		return true;
 	}
 
-	@Override
-	public void run(IAction action) {
-		IStructuredSelection selection = getCurrentSelection();
-		if (selection == null) {
-			return;
-		}
-		Iterator<?> itr = selection.iterator();
-		try {
-			while (itr.hasNext()) {
-				Object element = itr.next();
-				if (element instanceof JDIVariable && ((JDIVariable) element).getJavaType() instanceof JDIInterfaceType) {
-					JDIObjectValue val = (JDIObjectValue) ((JDIVariable) element).getValue();
-					if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
-						OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeHierarchyAction();
-						declaredAction.setActivePart(action, getPart());
-						declaredAction.run(action);
-						return;
-					}
-				}
-				Object sourceElement = resolveSourceElement(element);
-				if (sourceElement != null) {
-					openInEditor(sourceElement);
-				} else {
-					IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
-					throw new CoreException(status);
-				}
-			}
-		}
-		catch (CoreException e) {
-			JDIDebugUIPlugin.statusDialog(e.getStatus());
-		}
-	}
 }
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableDeclarationAction.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableDeclarationAction.java
new file mode 100644
index 0000000..626fbff
--- /dev/null
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenVariableDeclarationAction.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2022 IBM Corporation and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *     Zsombor Gegesy - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.jdt.internal.debug.ui.actions;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.debug.core.model.IDebugElement;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.debug.core.IJavaFieldVariable;
+import org.eclipse.jdt.debug.core.IJavaType;
+import org.eclipse.jdt.internal.debug.core.model.JDIFieldVariable;
+import org.eclipse.jdt.ui.JavaUI;
+
+/**
+ * Open the source code, where the variable is declared.
+ *
+ */
+public class OpenVariableDeclarationAction extends OpenVariableConcreteTypeAction {
+
+	@Override
+	protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException {
+		if (element instanceof IJavaFieldVariable) {
+			var variable = (IJavaFieldVariable) element;
+			return variable.getDeclaringType();
+		}
+		return null;
+	}
+
+	@Override
+	protected void openInEditor(Object element, IType sourceElement) throws CoreException {
+		if (element instanceof JDIFieldVariable) {
+			var field = (JDIFieldVariable) element;
+			var fieldElement = sourceElement.getField(field.getName());
+			var editor = JavaUI.openInEditor(fieldElement);
+			if (editor != null) {
+				return;
+			}
+		}
+		super.openInEditor(element, sourceElement);
+	}
+}