Skip to content

Commit 243ba94

Browse files
yaohaizhandxu
authored andcommitted
Add debug execute command handler. (scalacenter#9)
1 parent 4282fad commit 243ba94

File tree

4 files changed

+192
-3
lines changed

4 files changed

+192
-3
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<?eclipse version="3.4"?>
33
<plugin>
4-
<extension
5-
point="org.eclipse.m2e.core.lifecycleMappingMetadataSource">
4+
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
5+
<delegateCommandHandler class="org.eclipse.jdt.ls.core.internal.DebugDelegateCommandHandlerFactory">
6+
<command id="vscode.java.startDebugSession"/>
7+
<command id="vscode.java.resolveClasspath"/>
8+
<command id="vscode.java.buildWorkspace"/>
9+
</delegateCommandHandler>
610
</extension>
711
</plugin>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DebugServerFactory implements IExecutableExtensionFactory {
1818

1919
@Override
2020
public Object create() throws CoreException {
21-
return JavaDebugServer.getInstance();
21+
return JavaDebugDelegateCommandHandler.getInstance();
2222
}
2323

2424
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.microsoft.java.debug.plugin.internal;
2+
3+
import java.util.List;
4+
5+
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
6+
7+
8+
public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler {
9+
10+
public static String DEBUG_STARTSESSION = "vscode.java.startDebugSession";
11+
12+
public static String RESOLVE_CLASSPATH = "vscode.java.resolveClasspath";
13+
14+
public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";
15+
16+
@Override
17+
public Object executeCommand(String commandId, List<Object> arguments) {
18+
if (DEBUG_STARTSESSION.equals(commandId)) {
19+
20+
} else if (RESOLVE_CLASSPATH.equals(commandId)) {
21+
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
22+
return handler.resolveClasspaths(arguments);
23+
} else if (BUILD_WORKSPACE.equals(commandId)) {
24+
25+
}
26+
return null;
27+
}
28+
29+
public static JavaDebugDelegateCommandHandler getInstance() {
30+
return new JavaDebugDelegateCommandHandler();
31+
}
32+
}
33+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Red Hat Inc. 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+
package com.microsoft.java.debug.plugin.internal;
12+
13+
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.concurrent.CompletableFuture;
18+
19+
import org.eclipse.core.resources.IProject;
20+
import org.eclipse.core.resources.IWorkspaceRoot;
21+
import org.eclipse.core.resources.ResourcesPlugin;
22+
import org.eclipse.core.runtime.CoreException;
23+
import org.eclipse.core.runtime.IStatus;
24+
import org.eclipse.core.runtime.Status;
25+
import org.eclipse.jdt.core.IJavaElement;
26+
import org.eclipse.jdt.core.IJavaProject;
27+
import org.eclipse.jdt.core.JavaCore;
28+
import org.eclipse.jdt.core.search.IJavaSearchConstants;
29+
import org.eclipse.jdt.core.search.IJavaSearchScope;
30+
import org.eclipse.jdt.core.search.SearchEngine;
31+
import org.eclipse.jdt.core.search.SearchMatch;
32+
import org.eclipse.jdt.core.search.SearchParticipant;
33+
import org.eclipse.jdt.core.search.SearchPattern;
34+
import org.eclipse.jdt.core.search.SearchRequestor;
35+
import org.eclipse.jdt.launching.JavaRuntime;
36+
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
37+
38+
public class ResolveClasspathsHandler {
39+
40+
public String[] resolveClasspaths(List<Object> arguments) {
41+
try {
42+
return computeClassPath((String)arguments.get(0), (String)arguments.get(1));
43+
} catch (CoreException e) {
44+
logException("Failed to resolve classpath.", e);
45+
}
46+
return null;
47+
}
48+
49+
/**
50+
* Get java project from name.
51+
*
52+
* @param projectName
53+
* project name
54+
* @return java project
55+
* @throws CoreException
56+
* CoreException
57+
*/
58+
private static IJavaProject getJavaProjectFromName(String projectName) throws CoreException {
59+
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
60+
IProject project = root.getProject(projectName);
61+
if (!project.exists()) {
62+
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
63+
String.format("Cannot find the project with name '%s'.", projectName)));
64+
}
65+
if (!project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
66+
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
67+
String.format("The project '%s' does not have java nature enabled.", projectName)));
68+
}
69+
IJavaProject javaProject = JavaCore.create(project);
70+
return javaProject;
71+
}
72+
73+
/**
74+
* Get java project from type.
75+
*
76+
* @param typeFullyQualifiedName
77+
* fully qualified name of type
78+
* @return java project
79+
* @throws CoreException
80+
* CoreException
81+
*/
82+
private static List<IJavaProject> getJavaProjectFromType(String typeFullyQualifiedName) throws CoreException {
83+
SearchPattern pattern = SearchPattern.createPattern(typeFullyQualifiedName, IJavaSearchConstants.TYPE,
84+
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
85+
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
86+
ArrayList<IJavaProject> projects = new ArrayList<>();
87+
SearchRequestor requestor = new SearchRequestor() {
88+
@Override
89+
public void acceptSearchMatch(SearchMatch match) {
90+
Object element = match.getElement();
91+
if (element instanceof IJavaElement) {
92+
projects.add(((IJavaElement) element).getJavaProject());
93+
}
94+
}
95+
};
96+
SearchEngine searchEngine = new SearchEngine();
97+
searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
98+
requestor, null /* progress monitor */);
99+
100+
return projects;
101+
}
102+
103+
/**
104+
* Accord to the project name and the main class, compute runtime classpath.
105+
*
106+
* @param projectName
107+
* project name
108+
* @param mainClass
109+
* full qualified class name
110+
* @return class path
111+
* @throws CoreException
112+
* CoreException
113+
*/
114+
private static String[] computeClassPath(String projectName, String mainClass) throws CoreException {
115+
IJavaProject project = null;
116+
// if type exists in multiple projects, debug configuration need provide
117+
// project name.
118+
if (projectName != null) {
119+
project = getJavaProjectFromName(projectName);
120+
} else {
121+
List<IJavaProject> projects = getJavaProjectFromType(mainClass);
122+
if (projects.size() == 0) {
123+
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
124+
String.format("Main class '%s' doesn't exist in the workspace.", mainClass)));
125+
}
126+
if (projects.size() > 1) {
127+
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
128+
String.format(
129+
"Main class '%s' isn't unique in the workspace, please pass in specified projectname.",
130+
mainClass)));
131+
}
132+
project = projects.get(0);
133+
}
134+
return computeClassPath(project);
135+
}
136+
137+
/**
138+
* Compute runtime classpath of a java project.
139+
*
140+
* @param javaProject
141+
* java project
142+
* @return class path
143+
* @throws CoreException
144+
* CoreException
145+
*/
146+
private static String[] computeClassPath(IJavaProject javaProject) throws CoreException {
147+
if (javaProject == null) {
148+
throw new IllegalArgumentException("javaProject is null");
149+
}
150+
return JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
151+
}
152+
}

0 commit comments

Comments
 (0)