Skip to content

Commit d687cd9

Browse files
gzsomborSarikaSinha
authored andcommitted
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 <[email protected]> Reviewed-by: Sarika Sinha <[email protected]>
1 parent 71f6a09 commit d687cd9

File tree

7 files changed

+134
-101
lines changed

7 files changed

+134
-101
lines changed

org.eclipse.jdt.debug.ui/plugin.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ openConcreteVarType.tooltip=Open the Variable's Actual Implementation Type
126126
openConcreteVarTypeHierarchy.label=Open Actual Type Hierarch&y
127127
openConcreteVarTypeHierarchy.tooltip=Open the Variable's Actual Implementation Type Hierarchy
128128

129+
openFieldDeclaration.label=Open &Field Declaration
130+
openFieldDeclaration.tooltip=Open the Declaration of the Field
131+
129132
openRecType.label=Open Actual T&ype
130133
openRecType.tooltip=Open the Actual Type
131134

org.eclipse.jdt.debug.ui/plugin.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,22 @@
642642
id="org.eclipse.jdt.debug.ui.actions.OpenDeclaringType">
643643
</action>
644644
</objectContribution>
645+
<objectContribution
646+
objectClass="org.eclipse.jdt.debug.core.IJavaVariable"
647+
id="org.eclipse.jdt.debug.ui.FilteredJavaVariableActions">
648+
<filter
649+
name="JavaVariableFilter"
650+
value="isFieldVariable">
651+
</filter>
652+
<action
653+
label="%openFieldDeclaration.label"
654+
tooltip="%openFieldDeclaration.tooltip"
655+
class="org.eclipse.jdt.internal.debug.ui.actions.OpenVariableDeclarationAction"
656+
menubarPath="emptyNavigationGroup"
657+
enablesFor="1"
658+
id="org.eclipse.jdt.debug.ui.actions.OpenVariableDeclarationAction">
659+
</action>
660+
</objectContribution>
645661
<objectContribution
646662
objectClass="org.eclipse.jdt.debug.core.IJavaVariable"
647663
id="org.eclipse.jdt.debug.ui.FilteredJavaVariableActions">

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/JavaVarActionFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.jdt.debug.core.IJavaArrayType;
2222
import org.eclipse.jdt.debug.core.IJavaClassType;
2323
import org.eclipse.jdt.debug.core.IJavaDebugTarget;
24+
import org.eclipse.jdt.debug.core.IJavaFieldVariable;
2425
import org.eclipse.jdt.debug.core.IJavaObject;
2526
import org.eclipse.jdt.debug.core.IJavaType;
2627
import org.eclipse.jdt.debug.core.IJavaVariable;
@@ -195,6 +196,9 @@ else if (value.equals("isValuePrimitive")) { //$NON-NLS-1$
195196
if (value.equals("isObjectValue")) { //$NON-NLS-1$
196197
return varValue != null && JDIObjectValue.class.isAssignableFrom(varValue.getClass());
197198
}
199+
if (value.equals("isFieldVariable")) { //$NON-NLS-1$
200+
return var instanceof IJavaFieldVariable;
201+
}
198202
}
199203
else if (name.equals("ConcreteVariableActionFilter") && value.equals("isConcrete")) { //$NON-NLS-1$ //$NON-NLS-2$
200204
return isDeclaredSameAsConcrete(var);

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/OpenTypeAction.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2022 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -23,7 +23,6 @@
2323
import org.eclipse.core.runtime.Status;
2424
import org.eclipse.debug.core.DebugException;
2525
import org.eclipse.debug.core.model.IDebugElement;
26-
import org.eclipse.jdt.core.IJavaElement;
2726
import org.eclipse.jdt.core.IType;
2827
import org.eclipse.jdt.core.JavaModelException;
2928
import org.eclipse.jdt.core.search.IJavaSearchConstants;
@@ -55,19 +54,38 @@ public void run(IAction action) {
5554
try {
5655
while (itr.hasNext()) {
5756
Object element= itr.next();
58-
Object sourceElement = resolveSourceElement(element);
59-
if (sourceElement != null) {
60-
openInEditor(sourceElement);
61-
} else {
62-
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
63-
throw new CoreException(status);
57+
if (openElement(action, element)) {
58+
return;
6459
}
6560
}
6661
} catch(CoreException e) {
6762
JDIDebugUIPlugin.statusDialog(e.getStatus());
6863
}
6964
}
7065

66+
/**
67+
* Open the selected element, return true, if further selections should not be checked.
68+
*
69+
* @param action
70+
* the action proxy that handles the presentation portion of the action
71+
* @param element
72+
* the selected element.
73+
* @return true, if no other openElement calls should be made. Used, when multiple element is selected, and the action works as trying until one
74+
* succeeds.
75+
* @throws CoreException
76+
* if source element is not found.
77+
*/
78+
protected boolean openElement(IAction action, Object element) throws DebugException, CoreException {
79+
IType sourceElement = resolveSourceElement(element);
80+
if (sourceElement != null) {
81+
openInEditor(element, sourceElement);
82+
} else {
83+
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
84+
throw new CoreException(status);
85+
}
86+
return false;
87+
}
88+
7189
protected abstract IDebugElement getDebugElement(IAdaptable element);
7290

7391
/**
@@ -86,8 +104,8 @@ public void run(IAction action) {
86104
* @return the source element to open or <code>null</code> if none
87105
* @throws CoreException
88106
*/
89-
protected Object resolveSourceElement(Object e) throws CoreException {
90-
Object source = null;
107+
protected IType resolveSourceElement(Object e) throws CoreException {
108+
IType source = null;
91109
IAdaptable element= (IAdaptable) e;
92110
IDebugElement dbgElement= getDebugElement(element);
93111
if (dbgElement != null) {
@@ -107,20 +125,11 @@ protected Object resolveSourceElement(Object e) throws CoreException {
107125
return source;
108126
}
109127

110-
protected void openInEditor(Object sourceElement) throws CoreException {
128+
protected void openInEditor(Object element, IType sourceElement) throws CoreException {
111129
if (isHierarchy()) {
112-
if (sourceElement instanceof IJavaElement) {
113-
OpenTypeHierarchyUtil.open((IJavaElement)sourceElement, getWorkbenchWindow());
114-
} else {
115-
typeHierarchyError();
116-
}
130+
OpenTypeHierarchyUtil.open(sourceElement, getWorkbenchWindow());
117131
} else {
118-
if(sourceElement instanceof IJavaElement) {
119-
JavaUI.openInEditor((IJavaElement) sourceElement);
120-
}
121-
else {
122-
showErrorMessage(ActionMessages.OpenTypeAction_2);
123-
}
132+
JavaUI.openInEditor(sourceElement);
124133
}
125134
}
126135

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2014 IBM Corporation and others.
2+
* Copyright (c) 2000, 2022 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,22 +14,20 @@
1414
package org.eclipse.jdt.internal.debug.ui.actions;
1515

1616

17-
import java.util.Iterator;
18-
1917
import org.eclipse.core.runtime.CoreException;
2018
import org.eclipse.core.runtime.IStatus;
2119
import org.eclipse.core.runtime.Status;
20+
import org.eclipse.debug.core.DebugException;
2221
import org.eclipse.debug.core.model.IDebugElement;
22+
import org.eclipse.jdt.core.IType;
2323
import org.eclipse.jdt.debug.core.IJavaType;
2424
import org.eclipse.jdt.debug.core.IJavaValue;
2525
import org.eclipse.jdt.debug.core.IJavaVariable;
2626
import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
2727
import org.eclipse.jdt.internal.debug.core.model.JDIInterfaceType;
2828
import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
2929
import org.eclipse.jdt.internal.debug.core.model.JDIVariable;
30-
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
3130
import org.eclipse.jface.action.IAction;
32-
import org.eclipse.jface.viewers.IStructuredSelection;
3331

3432
/**
3533
* Opens the concrete type of variable - i.e. it's value's actual type.
@@ -48,38 +46,34 @@ protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException {
4846
return null;
4947
}
5048

51-
5249
@Override
53-
public void run(IAction action) {
54-
IStructuredSelection selection = getCurrentSelection();
55-
if (selection == null) {
56-
return;
57-
}
58-
Iterator<?> itr = selection.iterator();
59-
try {
60-
while (itr.hasNext()) {
61-
Object element = itr.next();
62-
if (element instanceof JDIVariable && ((JDIVariable) element).getJavaType() instanceof JDIInterfaceType) {
63-
JDIObjectValue val = (JDIObjectValue) ((JDIVariable) element).getValue();
64-
if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
65-
OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeAction();
66-
declaredAction.setActivePart(action, getPart());
67-
declaredAction.run(action);
68-
return;
69-
}
70-
}
71-
Object sourceElement = resolveSourceElement(element);
72-
if (sourceElement != null) {
73-
openInEditor(sourceElement);
74-
} else {
75-
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
76-
throw new CoreException(status);
50+
protected boolean openElement(IAction action, Object element) throws DebugException, CoreException {
51+
if (element instanceof JDIVariable) {
52+
final var jdiVariable = (JDIVariable) element;
53+
if (isInterfaceType(jdiVariable)) {
54+
final var val = (JDIObjectValue) jdiVariable.getValue();
55+
if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
56+
OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeAction();
57+
declaredAction.setActivePart(action, getPart());
58+
declaredAction.run(action);
59+
return true;
7760
}
7861
}
7962
}
80-
catch (CoreException e) {
81-
JDIDebugUIPlugin.statusDialog(e.getStatus());
63+
IType sourceElement = resolveSourceElement(element);
64+
if (sourceElement != null) {
65+
openInEditor(element, sourceElement);
66+
return false;
8267
}
68+
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
69+
throw new CoreException(status);
8370
}
8471

72+
private boolean isInterfaceType(JDIVariable jdiVariable) {
73+
try {
74+
return jdiVariable.getJavaType() instanceof JDIInterfaceType;
75+
} catch (DebugException e) {
76+
return false;
77+
}
78+
}
8579
}
Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2014 IBM Corporation and others.
2+
* Copyright (c) 2000, 2022 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,21 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.debug.ui.actions;
1515

16-
import java.util.Iterator;
17-
18-
import org.eclipse.core.runtime.CoreException;
19-
import org.eclipse.core.runtime.IStatus;
20-
import org.eclipse.core.runtime.Status;
21-
import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
22-
import org.eclipse.jdt.internal.debug.core.model.JDIInterfaceType;
23-
import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
24-
import org.eclipse.jdt.internal.debug.core.model.JDIVariable;
25-
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26-
import org.eclipse.jface.action.IAction;
27-
import org.eclipse.jface.viewers.IStructuredSelection;
28-
2916
/**
30-
* Opens the concrete type hierarhcy of variable - i.e. it's value's actual type.
17+
* Opens the concrete type hierarchy of variable - i.e. it's value's actual type.
3118
*/
3219
public class OpenVariableConcreteTypeHierarchyAction extends OpenVariableConcreteTypeAction {
3320

@@ -39,36 +26,4 @@ protected boolean isHierarchy() {
3926
return true;
4027
}
4128

42-
@Override
43-
public void run(IAction action) {
44-
IStructuredSelection selection = getCurrentSelection();
45-
if (selection == null) {
46-
return;
47-
}
48-
Iterator<?> itr = selection.iterator();
49-
try {
50-
while (itr.hasNext()) {
51-
Object element = itr.next();
52-
if (element instanceof JDIVariable && ((JDIVariable) element).getJavaType() instanceof JDIInterfaceType) {
53-
JDIObjectValue val = (JDIObjectValue) ((JDIVariable) element).getValue();
54-
if (val.getJavaType().toString().contains("$$Lambda$")) { //$NON-NLS-1$
55-
OpenVariableDeclaredTypeAction declaredAction = new OpenVariableDeclaredTypeHierarchyAction();
56-
declaredAction.setActivePart(action, getPart());
57-
declaredAction.run(action);
58-
return;
59-
}
60-
}
61-
Object sourceElement = resolveSourceElement(element);
62-
if (sourceElement != null) {
63-
openInEditor(sourceElement);
64-
} else {
65-
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$
66-
throw new CoreException(status);
67-
}
68-
}
69-
}
70-
catch (CoreException e) {
71-
JDIDebugUIPlugin.statusDialog(e.getStatus());
72-
}
73-
}
7429
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Zsombor Gegesy - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.jdt.internal.debug.ui.actions;
16+
17+
import org.eclipse.core.runtime.CoreException;
18+
import org.eclipse.debug.core.model.IDebugElement;
19+
import org.eclipse.jdt.core.IType;
20+
import org.eclipse.jdt.debug.core.IJavaFieldVariable;
21+
import org.eclipse.jdt.debug.core.IJavaType;
22+
import org.eclipse.jdt.internal.debug.core.model.JDIFieldVariable;
23+
import org.eclipse.jdt.ui.JavaUI;
24+
25+
/**
26+
* Open the source code, where the variable is declared.
27+
*
28+
*/
29+
public class OpenVariableDeclarationAction extends OpenVariableConcreteTypeAction {
30+
31+
@Override
32+
protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException {
33+
if (element instanceof IJavaFieldVariable) {
34+
var variable = (IJavaFieldVariable) element;
35+
return variable.getDeclaringType();
36+
}
37+
return null;
38+
}
39+
40+
@Override
41+
protected void openInEditor(Object element, IType sourceElement) throws CoreException {
42+
if (element instanceof JDIFieldVariable) {
43+
var field = (JDIFieldVariable) element;
44+
var fieldElement = sourceElement.getField(field.getName());
45+
var editor = JavaUI.openInEditor(fieldElement);
46+
if (editor != null) {
47+
return;
48+
}
49+
}
50+
super.openInEditor(element, sourceElement);
51+
}
52+
}

0 commit comments

Comments
 (0)