Skip to content

Commit 6158bf2

Browse files
authored
resolve main class automatically (#71)
* resolve main class automatically Signed-off-by: xuzho <xuzho@microsoft.com> * resolve comments Signed-off-by: xuzho <xuzho@microsoft.com> * update: return all the main class in the workspace Signed-off-by: xuzho <xuzho@microsoft.com> * resolve comments Signed-off-by: xuzho <xuzho@microsoft.com> * remove unnecessary dependency Signed-off-by: xuzho <xuzho@microsoft.com>
1 parent a36eb4e commit 6158bf2

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

com.microsoft.java.debug.plugin/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<delegateCommandHandler class="com.microsoft.java.debug.plugin.internal.JavaDebugDelegateCommandHandler">
66
<command id="vscode.java.startDebugSession"/>
77
<command id="vscode.java.resolveClasspath"/>
8+
<command id="vscode.java.resolveMainClass"/>
89
<command id="vscode.java.buildWorkspace"/>
910
<command id="vscode.java.fetchUsageData"/>
1011
<command id="vscode.java.configLogLevel"/>

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
2626

2727
public static String RESOLVE_CLASSPATH = "vscode.java.resolveClasspath";
2828

29+
public static String RESOLVE_MAINCLASS = "vscode.java.resolveMainClass";
30+
2931
public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";
3032

3133
public static String CONFIG_LOG_LEVEL = "vscode.java.configLogLevel";
@@ -40,6 +42,9 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
4042
} else if (RESOLVE_CLASSPATH.equals(commandId)) {
4143
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
4244
return handler.resolveClasspaths(arguments);
45+
} else if (RESOLVE_MAINCLASS.equals(commandId)) {
46+
ResolveMainClassHandler handler = new ResolveMainClassHandler();
47+
return handler.resolveMainClass();
4348
} else if (BUILD_WORKSPACE.equals(commandId)) {
4449
// TODO
4550
} else if (FETCH_USER_DATA.equals(commandId)) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.plugin.internal;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.logging.Logger;
17+
import java.util.stream.Collectors;
18+
19+
import org.eclipse.core.resources.IProject;
20+
import org.eclipse.core.resources.IResource;
21+
import org.eclipse.core.runtime.CoreException;
22+
import org.eclipse.jdt.core.IMethod;
23+
import org.eclipse.jdt.core.JavaModelException;
24+
import org.eclipse.jdt.core.search.IJavaSearchConstants;
25+
import org.eclipse.jdt.core.search.IJavaSearchScope;
26+
import org.eclipse.jdt.core.search.SearchEngine;
27+
import org.eclipse.jdt.core.search.SearchMatch;
28+
import org.eclipse.jdt.core.search.SearchParticipant;
29+
import org.eclipse.jdt.core.search.SearchPattern;
30+
import org.eclipse.jdt.core.search.SearchRequestor;
31+
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
32+
33+
import com.microsoft.java.debug.core.Configuration;
34+
35+
public class ResolveMainClassHandler {
36+
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
37+
38+
/**
39+
* resolve main class and project name.
40+
* @return an array of main class and project name
41+
* @throws CoreException when there are errors when resolving main class.
42+
*/
43+
public Object resolveMainClass() throws CoreException {
44+
return resolveMainClassCore();
45+
}
46+
47+
private List<ResolutionItem> resolveMainClassCore() throws CoreException {
48+
IJavaSearchScope searchScope = SearchEngine.createWorkspaceScope();
49+
SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD,
50+
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH);
51+
ArrayList<ResolutionItem> res = new ArrayList<>();
52+
SearchRequestor requestor = new SearchRequestor() {
53+
@Override
54+
public void acceptSearchMatch(SearchMatch match) {
55+
Object element = match.getElement();
56+
if (element instanceof IMethod) {
57+
IMethod method = (IMethod) element;
58+
try {
59+
if (method.isMainMethod()) {
60+
IResource resource = method.getResource();
61+
if (resource != null) {
62+
IProject project = resource.getProject();
63+
if (project != null) {
64+
String mainClass = method.getDeclaringType().getFullyQualifiedName();
65+
String projectName = ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName()) ? null : project.getName();
66+
res.add(new ResolutionItem(mainClass, projectName));
67+
}
68+
}
69+
}
70+
} catch (JavaModelException e) {
71+
// ignore
72+
}
73+
}
74+
}
75+
};
76+
SearchEngine searchEngine = new SearchEngine();
77+
searchEngine.search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
78+
searchScope, requestor, null /* progress monitor */);
79+
return res.stream().distinct().collect(Collectors.toList());
80+
}
81+
82+
private class ResolutionItem {
83+
private String mainClass;
84+
private String projectName;
85+
86+
public ResolutionItem(String mainClass, String projectName) {
87+
this.mainClass = mainClass;
88+
this.projectName = projectName;
89+
}
90+
91+
@Override
92+
public boolean equals(Object o) {
93+
if (this == o) {
94+
return true;
95+
}
96+
if (o instanceof ResolutionItem) {
97+
ResolutionItem item = (ResolutionItem) o;
98+
if (mainClass != null ? !mainClass.equals(item.mainClass) : item.mainClass != null) {
99+
return false;
100+
}
101+
if (projectName != null ? !projectName.equals(item.projectName) : item.projectName != null) {
102+
return false;
103+
}
104+
return true;
105+
}
106+
return false;
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
return (mainClass == null ? 0 : mainClass.hashCode()) * 13 + (projectName == null ? 0 : projectName.hashCode());
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)