Skip to content

Commit e74176c

Browse files
Code optimisation
1 parent dd17573 commit e74176c

1 file changed

Lines changed: 17 additions & 153 deletions

File tree

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

Lines changed: 17 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@
1717
import java.util.List;
1818
import java.util.stream.Collectors;
1919

20-
import org.eclipse.core.resources.IProject;
21-
import org.eclipse.core.resources.ResourcesPlugin;
22-
import org.eclipse.core.runtime.NullProgressMonitor;
2320
import org.eclipse.debug.core.model.IStackFrame;
2421
import org.eclipse.debug.internal.ui.DebugUIPlugin;
2522
import org.eclipse.debug.ui.DebugUITools;
2623
import org.eclipse.jdt.core.IClassFile;
2724
import org.eclipse.jdt.core.ICompilationUnit;
2825
import org.eclipse.jdt.core.IJavaElement;
29-
import org.eclipse.jdt.core.IJavaProject;
30-
import org.eclipse.jdt.core.IOrdinaryClassFile;
31-
import org.eclipse.jdt.core.IPackageFragment;
3226
import org.eclipse.jdt.core.IType;
33-
import org.eclipse.jdt.core.JavaCore;
34-
import org.eclipse.jdt.core.JavaModelException;
27+
import org.eclipse.jdt.core.WorkingCopyOwner;
3528
import org.eclipse.jdt.core.dom.AST;
3629
import org.eclipse.jdt.core.dom.ASTParser;
3730
import org.eclipse.jdt.core.dom.ASTVisitor;
@@ -42,7 +35,6 @@
4235
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
4336
import org.eclipse.jdt.debug.core.IJavaStackFrame;
4437
import org.eclipse.jdt.debug.core.IJavaVariable;
45-
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
4638
import org.eclipse.jdt.ui.JavaUI;
4739
import org.eclipse.jface.action.IAction;
4840
import org.eclipse.jface.text.IDocument;
@@ -70,21 +62,6 @@ public void run(IAction action) {
7062
Object frame = DebugUITools.getDebugContext();
7163
if (frame instanceof IStackFrame jFrame) {
7264
if (jFrame instanceof IJavaStackFrame javaStackFrame) {
73-
IJavaProject iJavaProject = null;
74-
String type = javaStackFrame.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
75-
if (type == null) {
76-
for (IJavaProject proj : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects()) {
77-
IType type2 = proj.findType(javaStackFrame.getDeclaringTypeName());
78-
if (type2 != null && type2.exists()) {
79-
iJavaProject = proj;
80-
}
81-
}
82-
}
83-
if (iJavaProject == null && type != null) {
84-
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type);
85-
iJavaProject = JavaCore.create(project);
86-
}
87-
IType iType = iJavaProject.findType(javaStackFrame.getReceivingTypeName());
8865
int currentLine = javaStackFrame.getLineNumber();
8966
String currentMethod = javaStackFrame.getMethodName();
9067
List<String> frameParams = javaStackFrame.getArgumentTypeNames();
@@ -96,46 +73,24 @@ public void run(IAction action) {
9673
return e;
9774
}).collect(Collectors.toList());
9875
final ICompilationUnit[] cu = { null };
99-
if (iType == null) {
100-
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
101-
IEditorPart editor = window.getActivePage().getActiveEditor();
102-
IJavaElement element = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
103-
cu[0] = (element instanceof ICompilationUnit cuElement) ? cuElement : null;
76+
77+
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
78+
IEditorPart editor = window.getActivePage().getActiveEditor();
79+
IJavaElement element = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
80+
81+
if (element instanceof ICompilationUnit icu) {
82+
cu[0] = icu;
83+
} else if (element instanceof IClassFile icf) {
84+
cu[0] = icf.getWorkingCopy(new WorkingCopyOwner() {
85+
}, null);
10486
} else {
105-
cu[0] = iType.getCompilationUnit();
87+
cu[0] = null;
10688
}
89+
10790
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
108-
if (cu[0] == null && iType != null) {
109-
IOrdinaryClassFile classFile = iType.getClassFile();
110-
if (classFile != null && classFile.getSource() != null) {
111-
String source = classFile.getSource();
112-
parser.setSource(source.toCharArray());
113-
parser.setKind(ASTParser.K_COMPILATION_UNIT);
114-
iTypeGlobal = iType;
115-
}
116-
} else if (cu[0] == null && iType == null) {
117-
final IJavaElement javaElement = getJavaElement(javaStackFrame);
118-
cu[0] = getCompilationUnit(javaElement);
119-
if (javaElement != null) {
120-
if (javaElement instanceof ICompilationUnit iCompilationUnit) {
121-
parser.setSource(iCompilationUnit);
122-
} else if (javaElement instanceof IClassFile iClassFile) {
123-
parser.setSource(iClassFile);
124-
} else if (javaElement instanceof IType typeNew) {
125-
char[] source = typeNew.getSource().toCharArray();
126-
if (source != null) {
127-
parser.setSource(source);
128-
} else {
129-
return; // No source
130-
}
131-
}
132-
parser.setResolveBindings(true);
133-
}
134-
} else {
135-
parser.setSource(cu[0]);
136-
parser.setKind(ASTParser.K_COMPILATION_UNIT);
137-
parser.setResolveBindings(true);
138-
}
91+
parser.setSource(cu[0]);
92+
parser.setKind(ASTParser.K_COMPILATION_UNIT);
93+
parser.setResolveBindings(true);
13994

14095
if (parser.createAST(null) instanceof CompilationUnit ast) {
14196
ast.accept(new ASTVisitor() {
@@ -261,6 +216,7 @@ private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPo
261216
} catch (PartInitException e) { // We can ignore the PartInitException
262217
DebugUIPlugin.log(e);
263218
}
219+
264220
}
265221
if (editor == null) {
266222
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
@@ -279,96 +235,4 @@ private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPo
279235
DebugUIPlugin.log(e);
280236
}
281237
}
282-
283-
/**
284-
* Gets the Java element for the current stack frame
285-
*
286-
* @param frame
287-
* IJavaStackFrame of the element
288-
*/
289-
private IJavaElement getJavaElement(IJavaStackFrame frame) {
290-
try {
291-
String projectName = frame.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
292-
if (projectName != null) {
293-
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
294-
if (project.exists()) {
295-
IJavaProject javaProject = JavaCore.create(project);
296-
IType type = javaProject.findType(frame.getReceivingTypeName());
297-
if (type != null) {
298-
return type.getCompilationUnit();
299-
}
300-
}
301-
}
302-
IType globalType = findTypeInWorkspace(frame.getDeclaringTypeName());
303-
if (globalType != null) {
304-
return globalType;
305-
}
306-
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
307-
if (window != null && window.getActivePage() != null) {
308-
IEditorPart editor = window.getActivePage().getActiveEditor();
309-
if (editor != null) {
310-
return JavaUI.getEditorInputJavaElement(editor.getEditorInput());
311-
}
312-
}
313-
} catch (Exception e) {
314-
DebugUIPlugin.log(e);
315-
}
316-
return null;
317-
}
318-
319-
/**
320-
* Finds a type in the entire workspace
321-
*
322-
* @param fullyQualifiedName
323-
*/
324-
private IType findTypeInWorkspace(String fullyQualifiedName) {
325-
try {
326-
for (IJavaProject project : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects()) {
327-
IType type = project.findType(fullyQualifiedName);
328-
if (type != null) {
329-
return type;
330-
}
331-
}
332-
} catch (JavaModelException e) {
333-
DebugUIPlugin.log(e);
334-
}
335-
return null;
336-
}
337-
338-
/**
339-
* Gets the Compilation unit for the JavaElement
340-
*
341-
* @param element
342-
* IJavaElement of the java element
343-
*/
344-
public ICompilationUnit getCompilationUnit(IJavaElement element) {
345-
if (element == null) {
346-
return null;
347-
}
348-
349-
if (element instanceof IClassFile classFile) {
350-
try {
351-
return classFile.getWorkingCopy(null, new NullProgressMonitor());
352-
} catch (JavaModelException e) {
353-
DebugUIPlugin.log(e);
354-
return null;
355-
}
356-
}
357-
IJavaElement ancestor = element.getAncestor(IJavaElement.COMPILATION_UNIT);
358-
if (ancestor instanceof ICompilationUnit iCompilationUnit) {
359-
return iCompilationUnit;
360-
}
361-
if (element instanceof IPackageFragment) {
362-
try {
363-
ICompilationUnit[] units = null;
364-
if (element instanceof IPackageFragment fragment) {
365-
units = fragment.getCompilationUnits();
366-
}
367-
return units.length > 0 ? units[0] : null;
368-
} catch (JavaModelException e) {
369-
DebugUIPlugin.log(e);
370-
}
371-
}
372-
return null;
373-
}
374238
}

0 commit comments

Comments
 (0)