Skip to content

Commit af0ddda

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

1 file changed

Lines changed: 71 additions & 11 deletions

File tree

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

Lines changed: 71 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,24 @@ 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+
if (type == null) {
65+
for (IJavaProject proj : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects()) {
66+
IType type2 = proj.findType(javaStackFrame.getDeclaringTypeName());
67+
if (type2 != null && type2.exists()) {
68+
iJavaProject = proj;
69+
}
70+
}
71+
}
72+
if (iJavaProject == null && type != null) {
73+
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
74+
iJavaProject = JavaCore.create(project);
75+
}
6276
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
63-
IJavaProject iJavaProject = JavaCore.create(project);
77+
iJavaProject = JavaCore.create(project);
6478
IType iType = iJavaProject.findType(javaStackFrame.getReceivingTypeName());
79+
int currentLine = javaStackFrame.getLineNumber();
6580
String currentMethod = javaStackFrame.getMethodName();
6681
List<String> frameParams = javaStackFrame.getArgumentTypeNames();
6782
List<String> ref = frameParams.stream().map(e -> {
@@ -80,39 +95,84 @@ public void run(IAction action) {
8095
ast.accept(new ASTVisitor() {
8196
boolean meth = false;
8297
boolean found = false;
98+
boolean inTargetContext = false;
8399
@Override
84100
public boolean visit(MethodDeclaration node) {
85101
if (node.getName().getIdentifier().equals(currentMethod)) {
86102
List<Object> parameters = node.parameters();
87103
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;
104+
int start = node.getStartPosition();
105+
int end = start + node.getLength();
106+
int startLine = ast.getLineNumber(start);
107+
int endLine = ast.getLineNumber(end);
108+
if (currentLine >= startLine && currentLine <= endLine) {
109+
inTargetContext = true;
110+
if (methodParams.equals(ref)) {
111+
meth = true;
112+
for (Object op : node.parameters()) {
113+
SingleVariableDeclaration parm = (SingleVariableDeclaration) op;
114+
if (parm.getName().getIdentifier().equals(name)) {
115+
highlightLine(ast, cu, node.getStartPosition());
116+
found = true;
117+
return false;
118+
}
96119
}
120+
return true;
97121
}
98-
return true;
99122
}
100123
}
101124
return true;
102125
}
103126

127+
@Override
128+
public void endVisit(MethodDeclaration node) {
129+
inTargetContext = false;
130+
}
131+
104132
@Override
105133
public boolean visit(VariableDeclarationFragment node) {
106134
if (found) {
107135
return false;
108136
}
109-
if (meth && node.getName().getIdentifier().equals(name)) {
137+
if ((meth || inTargetContext) && node.getName().getIdentifier().equals(name)) {
110138
found = true;
111139
highlightLine(ast, cu, node.getStartPosition());
112140
return false;
113141
}
114142
return true;
115143
}
144+
145+
@Override
146+
public boolean visit(LambdaExpression node) {
147+
if (found) {
148+
return false;
149+
}
150+
List<Object> parameters = node.parameters();
151+
int start = node.getStartPosition();
152+
int end = start + node.getLength();
153+
int startLine = ast.getLineNumber(start);
154+
int endLine = ast.getLineNumber(end);
155+
if (currentLine >= startLine && currentLine <= endLine) {
156+
inTargetContext = true;
157+
for (Object param : parameters) {
158+
if (param instanceof SingleVariableDeclaration) {
159+
SingleVariableDeclaration svd = (SingleVariableDeclaration) param;
160+
if (svd.getName().getIdentifier().equals(name)) {
161+
highlightLine(ast, cu, svd.getStartPosition());
162+
found = true;
163+
return false;
164+
}
165+
}
166+
}
167+
}
168+
return true;
169+
170+
}
171+
172+
@Override
173+
public void endVisit(LambdaExpression node) {
174+
inTargetContext = false;
175+
}
116176
});
117177
}
118178
}

0 commit comments

Comments
 (0)