Skip to content

Commit 7752d38

Browse files
Handled static variables and code optimization
1 parent d3155bc commit 7752d38

File tree

4 files changed

+117
-93
lines changed

4 files changed

+117
-93
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -748,22 +748,22 @@
748748
id="org.eclipse.jdt.debug.ui.actions.OpenVariableDeclaredType">
749749
</action>
750750
<action
751-
label="%openDeclLocalVarNavigation.label"
752-
helpContextId="open_variable_declared_type_hierarchy_action_context"
753-
tooltip="%openDeclLocalVarNavigation.tooltip"
754-
class="org.eclipse.jdt.internal.debug.ui.actions.OpenDeclLocalVarNavigationAction"
755-
menubarPath="emptyNavigationGroup"
756-
enablesFor="1"
757-
id="org.eclipse.jdt.debug.ui.actions.OpenDeclLocalVarNavigation">
758-
</action>
759-
<visibility>
760-
<not>
761-
<objectState
762-
name="JavaVariableFilter"
763-
value="isLocalVariableValue">
764-
</objectState>
765-
</not>
766-
</visibility>
751+
label="%openDeclLocalVarNavigation.label"
752+
helpContextId="open_variable_declared_type_hierarchy_action_context"
753+
tooltip="%openDeclLocalVarNavigation.tooltip"
754+
class="org.eclipse.jdt.internal.debug.ui.actions.OpenDeclLocalVarNavigationAction"
755+
menubarPath="emptyNavigationGroup"
756+
enablesFor="1"
757+
id="org.eclipse.jdt.debug.ui.actions.OpenDeclLocalVarNavigation">
758+
</action>
759+
<visibility>
760+
<not>
761+
<objectState
762+
name="JavaVariableFilter"
763+
value="isLocalVariableValue">
764+
</objectState>
765+
</not>
766+
</visibility>
767767
</objectContribution>
768768
<objectContribution
769769
objectClass="org.eclipse.jdt.debug.core.IJavaVariable"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.eclipse.jdt.debug.core.IJavaPrimitiveValue;
2828
import org.eclipse.jdt.debug.core.IJavaType;
2929
import org.eclipse.jdt.debug.core.IJavaVariable;
30+
import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIReturnValueVariable;
31+
import org.eclipse.jdt.internal.debug.core.model.JDIFieldVariable;
3032
import org.eclipse.jdt.internal.debug.core.model.JDILocalVariable;
3133
import org.eclipse.jdt.internal.debug.core.model.JDINullValue;
3234
import org.eclipse.jdt.internal.debug.core.model.JDIObjectValue;
@@ -209,7 +211,7 @@ else if (value.equals("isValuePrimitive")) { //$NON-NLS-1$
209211
return var instanceof IJavaFieldVariable;
210212
}
211213
if (value.equals("isLocalVariableValue")) { //$NON-NLS-1$
212-
return !(var instanceof JDILocalVariable);
214+
return (!(var instanceof JDILocalVariable) && (var instanceof JDIReturnValueVariable) && !(var instanceof JDIFieldVariable));
213215
}
214216
}
215217
else if (name.equals("ConcreteVariableActionFilter") && value.equals("isConcrete")) { //$NON-NLS-1$ //$NON-NLS-2$

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

Lines changed: 97 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.eclipse.core.resources.IProject;
2121
import org.eclipse.core.resources.ResourcesPlugin;
2222
import org.eclipse.core.runtime.CoreException;
23-
import org.eclipse.debug.core.model.IDebugElement;
2423
import org.eclipse.debug.core.model.IStackFrame;
24+
import org.eclipse.debug.ui.DebugUITools;
2525
import org.eclipse.jdt.core.ICompilationUnit;
2626
import org.eclipse.jdt.core.IJavaProject;
2727
import org.eclipse.jdt.core.IType;
@@ -30,108 +30,131 @@
3030
import org.eclipse.jdt.core.dom.ASTParser;
3131
import org.eclipse.jdt.core.dom.ASTVisitor;
3232
import org.eclipse.jdt.core.dom.CompilationUnit;
33+
import org.eclipse.jdt.core.dom.FieldDeclaration;
3334
import org.eclipse.jdt.core.dom.MethodDeclaration;
3435
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
3536
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
3637
import org.eclipse.jdt.debug.core.IJavaStackFrame;
37-
import org.eclipse.jdt.debug.core.IJavaType;
3838
import org.eclipse.jdt.debug.core.IJavaVariable;
3939
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
4040
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
4141
import org.eclipse.jdt.ui.JavaUI;
42+
import org.eclipse.jface.action.IAction;
4243
import org.eclipse.jface.text.IDocument;
4344
import org.eclipse.jface.text.IRegion;
45+
import org.eclipse.jface.viewers.IStructuredSelection;
4446
import org.eclipse.ui.IEditorPart;
4547
import org.eclipse.ui.texteditor.IDocumentProvider;
4648
import org.eclipse.ui.texteditor.ITextEditor;
4749

48-
public class OpenDeclLocalVarNavigationAction extends OpenVariableTypeAction {
50+
public class OpenDeclLocalVarNavigationAction extends ObjectActionDelegate {
4951
@Override
50-
protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException {
52+
public void run(IAction action) {
53+
IStructuredSelection selection = getCurrentSelection();
54+
if (selection == null) {
55+
return;
56+
}
5157
try {
52-
if (element instanceof IJavaVariable varE) {
53-
IJavaType javaType = varE.getJavaType();
54-
String name = varE.getName();
55-
IStackFrame jFrame = javaType.getDebugTarget().getThreads()[0].getTopStackFrame();
56-
if (jFrame instanceof IJavaStackFrame jf) {
57-
jf.getReferenceType().getAllFieldNames();
58-
String type = jf.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
59-
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
60-
IJavaProject iJavaProject = JavaCore.create(project);
61-
IType iType = iJavaProject.findType(jf.getReceivingTypeName());
62-
String nameMethod = jf.getMethodName();
63-
List<String> frameParams = jf.getArgumentTypeNames();
64-
List<String> ref = frameParams.stream().map(e -> {
65-
int dot = e.lastIndexOf('.');
66-
if(dot>=0) {
67-
return e.substring(dot+1);
68-
}
69-
return e;
70-
}).collect(Collectors.toList());
71-
ICompilationUnit cu = iType.getCompilationUnit();
72-
ASTParser parse = ASTParser.newParser(AST.getJLSLatest());
73-
parse.setSource(cu);
74-
parse.setKind(ASTParser.K_COMPILATION_UNIT);
58+
if (selection.getFirstElement() != null) {
59+
Object element = selection.getFirstElement();
60+
if (element instanceof IJavaVariable varE) {
61+
String name = varE.getName();
62+
Object frame = DebugUITools.getDebugContext();
63+
if (frame instanceof IStackFrame jFrame) {
64+
if (jFrame instanceof IJavaStackFrame jf) {
65+
jf.getReferenceType().getAllFieldNames();
66+
String type = jf.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
67+
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
68+
IJavaProject iJavaProject = JavaCore.create(project);
69+
IType iType = iJavaProject.findType(jf.getReceivingTypeName());
70+
String nameMethod = jf.getMethodName();
71+
List<String> frameParams = jf.getArgumentTypeNames();
72+
List<String> ref = frameParams.stream().map(e -> {
73+
int dot = e.lastIndexOf('.');
74+
if (dot >= 0) {
75+
return e.substring(dot + 1);
76+
}
77+
return e;
78+
}).collect(Collectors.toList());
79+
ICompilationUnit cu = iType.getCompilationUnit();
80+
ASTParser parse = ASTParser.newParser(AST.getJLSLatest());
81+
parse.setSource(cu);
82+
parse.setKind(ASTParser.K_COMPILATION_UNIT);
83+
parse.setResolveBindings(true);
84+
CompilationUnit ast = (CompilationUnit) parse.createAST(null);
85+
ast.accept(new ASTVisitor() {
86+
boolean meth = false;
87+
boolean found = false;
88+
@Override
89+
public boolean visit(MethodDeclaration node) {
90+
if (node.getName().getIdentifier().equals(nameMethod)) {
91+
List<Object> parameters = node.parameters();
92+
List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList();
93+
if (methodParams.equals(ref)) {
94+
meth = true;
95+
for (Object op : node.parameters()) {
96+
SingleVariableDeclaration parm = (SingleVariableDeclaration) op;
97+
if (parm.getName().getIdentifier().equals(name)) {
98+
highlightLine(ast, cu, node.getStartPosition());
99+
found = true;
100+
return true;
101+
}
102+
}
103+
return true;
104+
}
105+
}
106+
return true;
107+
}
108+
@Override
109+
public boolean visit(VariableDeclarationFragment node) {
110+
if (found) {
111+
return false;
112+
}
113+
if (meth && node.getName().getIdentifier().equals(name)) {
114+
found = true;
115+
highlightLine(ast, cu, node.getStartPosition());
116+
return true;
117+
}
118+
return true;
119+
}
120+
@Override
121+
public boolean visit(FieldDeclaration node) {
122+
if (found) {
123+
return false;
124+
}
125+
for (Object op : node.fragments()) {
126+
VariableDeclarationFragment vdf = (VariableDeclarationFragment) op;
75127

76-
parse.setResolveBindings(true);
77-
CompilationUnit ast = (CompilationUnit) parse.createAST(null);
78-
ast.accept(new ASTVisitor() {
79-
boolean meth = false;
80-
boolean found = false;
81-
@Override
82-
public boolean visit(MethodDeclaration node) {
83-
if (node.getName().getIdentifier().equals(nameMethod)) {
84-
List<Object> parameters = node.parameters();
85-
List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList();
86-
if (methodParams.equals(ref)) {
87-
meth = true;
88-
for (Object op : node.parameters()) {
89-
SingleVariableDeclaration parm = (SingleVariableDeclaration) op;
90-
if (parm.getName().getIdentifier().equals(name)) {
91-
highlightLine(ast, cu, node.getStartPosition());
128+
if (vdf.getName().getIdentifier().equals(name)) {
92129
found = true;
130+
highlightLine(ast, cu, node.getStartPosition());
93131
return true;
94132
}
95133
}
96134
return true;
97135
}
98-
}
99-
return true;
100-
}
101-
@Override
102-
public boolean visit(VariableDeclarationFragment node) {
103-
if (found) {
104-
return false;
105-
}
106-
if (meth && node.getName().getIdentifier().equals(name)) {
107-
found = true;
108-
highlightLine(ast, cu, node.getStartPosition());
109-
return true;
110-
}
111-
return true;
112-
}
113-
private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) {
114-
int line = ast.getLineNumber(startPos);
115-
try {
116-
IEditorPart editor = JavaUI.openInEditor(cu);
117-
if (editor instanceof ITextEditor txtEd) {
118-
IDocumentProvider prov = txtEd.getDocumentProvider();
119-
IDocument doc = prov.getDocument(txtEd.getEditorInput());
120-
IRegion lineReg = doc.getLineInformation(line - 1);
121-
txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength());
136+
private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) {
137+
int line = ast.getLineNumber(startPos);
138+
try {
139+
IEditorPart editor = JavaUI.openInEditor(cu);
140+
141+
if (editor instanceof ITextEditor txtEd) {
142+
IDocumentProvider prov = txtEd.getDocumentProvider();
143+
IDocument doc = prov.getDocument(txtEd.getEditorInput());
144+
IRegion lineReg = doc.getLineInformation(line - 1);
145+
txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength());
146+
}
147+
} catch (Exception e) {
148+
JDIDebugUIPlugin.log(e);
149+
}
122150
}
123-
} catch (Exception e) {
124-
System.out.println("Exception: " + e.getMessage()); //$NON-NLS-1$
125-
}
151+
});
126152
}
127-
});
128-
ResourcesPlugin.getWorkspace();
153+
}
129154
}
130155
}
131156
} catch (CoreException e) {
132157
JDIDebugUIPlugin.statusDialog(e.getStatus());
133158
}
134-
135-
return null;
136159
}
137160
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.eclipse.jdt.debug.core.IJavaType;
3434
import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
3535
import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
36-
import org.eclipse.jdt.internal.debug.core.model.JDILocalVariable;
3736
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
3837
import org.eclipse.jdt.internal.ui.util.OpenTypeHierarchyUtil;
3938
import org.eclipse.jdt.ui.JavaUI;
@@ -78,7 +77,7 @@ public void run(IAction action) {
7877
*/
7978
protected boolean openElement(IAction action, Object element) throws DebugException, CoreException {
8079
IType sourceElement = resolveSourceElement(element);
81-
if (sourceElement != null || (sourceElement == null && element instanceof JDILocalVariable)) {
80+
if (sourceElement != null) {
8281
openInEditor(element, sourceElement);
8382
} else {
8483
IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, "Source not found", null); //$NON-NLS-1$

0 commit comments

Comments
 (0)