Skip to content

Commit 7ad4d31

Browse files
Variable Navigation throwing NPE
Variable Navigation throwing NPE for non-java projects Fix: #712
1 parent 093195d commit 7ad4d31

1 file changed

Lines changed: 69 additions & 11 deletions

File tree

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

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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.LambdaExpression;
3334
import org.eclipse.jdt.core.dom.MethodDeclaration;
3435
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
3536
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
@@ -58,10 +59,22 @@ public void run(IAction action) {
5859
Object frame = DebugUITools.getDebugContext();
5960
if (frame instanceof IStackFrame jFrame) {
6061
if (jFrame instanceof IJavaStackFrame javaStackFrame) {
62+
IJavaProject iJavaProject = null;
6163
String type = javaStackFrame.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
64+
for (IJavaProject proj : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects()) {
65+
IType type2 = proj.findType(javaStackFrame.getDeclaringTypeName());
66+
if (type2 != null && type2.exists()) {
67+
iJavaProject = proj;
68+
}
69+
}
70+
if (iJavaProject == null && type != null) {
71+
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
72+
iJavaProject = JavaCore.create(project);
73+
}
6274
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
63-
IJavaProject iJavaProject = JavaCore.create(project);
75+
iJavaProject = JavaCore.create(project);
6476
IType iType = iJavaProject.findType(javaStackFrame.getReceivingTypeName());
77+
int currentLine = javaStackFrame.getLineNumber();
6578
String currentMethod = javaStackFrame.getMethodName();
6679
List<String> frameParams = javaStackFrame.getArgumentTypeNames();
6780
List<String> ref = frameParams.stream().map(e -> {
@@ -80,39 +93,84 @@ public void run(IAction action) {
8093
ast.accept(new ASTVisitor() {
8194
boolean meth = false;
8295
boolean found = false;
96+
boolean inTargetContext = false;
8397
@Override
8498
public boolean visit(MethodDeclaration node) {
8599
if (node.getName().getIdentifier().equals(currentMethod)) {
86100
List<Object> parameters = node.parameters();
87101
List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList();
88-
if (methodParams.equals(ref)) {
89-
meth = true;
90-
for (Object op : node.parameters()) {
91-
SingleVariableDeclaration parm = (SingleVariableDeclaration) op;
92-
if (parm.getName().getIdentifier().equals(name)) {
93-
highlightLine(ast, cu, node.getStartPosition());
94-
found = true;
95-
return false;
102+
int start = node.getStartPosition();
103+
int end = start + node.getLength();
104+
int startLine = ast.getLineNumber(start);
105+
int endLine = ast.getLineNumber(end);
106+
if (currentLine >= startLine && currentLine <= endLine) {
107+
inTargetContext = true;
108+
if (methodParams.equals(ref)) {
109+
meth = true;
110+
for (Object op : node.parameters()) {
111+
SingleVariableDeclaration parm = (SingleVariableDeclaration) op;
112+
if (parm.getName().getIdentifier().equals(name)) {
113+
highlightLine(ast, cu, node.getStartPosition());
114+
found = true;
115+
return false;
116+
}
96117
}
118+
return true;
97119
}
98-
return true;
99120
}
100121
}
101122
return true;
102123
}
103124

125+
@Override
126+
public void endVisit(MethodDeclaration node) {
127+
inTargetContext = false;
128+
}
129+
104130
@Override
105131
public boolean visit(VariableDeclarationFragment node) {
106132
if (found) {
107133
return false;
108134
}
109-
if (meth && node.getName().getIdentifier().equals(name)) {
135+
if ((meth || inTargetContext) && node.getName().getIdentifier().equals(name)) {
110136
found = true;
111137
highlightLine(ast, cu, node.getStartPosition());
112138
return false;
113139
}
114140
return true;
115141
}
142+
143+
@Override
144+
public boolean visit(LambdaExpression node) {
145+
if (found) {
146+
return false;
147+
}
148+
List<Object> parameters = node.parameters();
149+
int start = node.getStartPosition();
150+
int end = start + node.getLength();
151+
int startLine = ast.getLineNumber(start);
152+
int endLine = ast.getLineNumber(end);
153+
if (currentLine >= startLine && currentLine <= endLine) {
154+
inTargetContext = true;
155+
for (Object param : parameters) {
156+
if (param instanceof SingleVariableDeclaration) {
157+
SingleVariableDeclaration svd = (SingleVariableDeclaration) param;
158+
if (svd.getName().getIdentifier().equals(name)) {
159+
highlightLine(ast, cu, svd.getStartPosition());
160+
found = true;
161+
return false;
162+
}
163+
}
164+
}
165+
}
166+
return true;
167+
168+
}
169+
170+
@Override
171+
public void endVisit(LambdaExpression node) {
172+
inTargetContext = false;
173+
}
116174
});
117175
}
118176
}

0 commit comments

Comments
 (0)