Skip to content

Commit bdb116a

Browse files
Avoid return null for rpc command (microsoft#14)
* Avoid return null for rpc command Signed-off-by: Jinbo Wang <jinbwan@microsoft.com> * Add todo comments to revisit null issue of json rpc in future * Update delegate commander to use new interface Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
1 parent 8313c8e commit bdb116a

6 files changed

Lines changed: 173 additions & 162 deletions

File tree

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
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+
112
package com.microsoft.java.debug.plugin.internal;
213

314
import java.util.List;
415

16+
import org.eclipse.core.runtime.IProgressMonitor;
517
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
618

19+
public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler {
20+
21+
public static String DEBUG_STARTSESSION = "vscode.java.startDebugSession";
22+
23+
public static String RESOLVE_CLASSPATH = "vscode.java.resolveClasspath";
24+
25+
public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";
26+
27+
@Override
28+
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
29+
if (DEBUG_STARTSESSION.equals(commandId)) {
30+
IDebugServer debugServer = JavaDebugServer.getInstance();
31+
debugServer.start();
32+
return debugServer.getPort();
33+
} else if (RESOLVE_CLASSPATH.equals(commandId)) {
34+
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
35+
return handler.resolveClasspaths(arguments);
36+
} else if (BUILD_WORKSPACE.equals(commandId)) {
37+
// TODO
38+
}
39+
throw new UnsupportedOperationException(String.format("Java debug plugin doesn't support the command '%s'.", commandId));
40+
}
741

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-
IDebugServer debugServer = JavaDebugServer.getInstance();
20-
debugServer.start();
21-
return debugServer.getPort();
22-
} else if (RESOLVE_CLASSPATH.equals(commandId)) {
23-
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
24-
return handler.resolveClasspaths(arguments);
25-
} else if (BUILD_WORKSPACE.equals(commandId)) {
26-
27-
}
28-
return null;
29-
}
30-
31-
public static JavaDebugDelegateCommandHandler getInstance() {
32-
return new JavaDebugDelegateCommandHandler();
33-
}
3442
}
35-

‎com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebuggerServerPlugin.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,27 @@
1111

1212
package com.microsoft.java.debug.plugin.internal;
1313

14+
import java.util.logging.Logger;
15+
1416
import org.osgi.framework.BundleActivator;
1517
import org.osgi.framework.BundleContext;
1618

19+
import com.microsoft.java.debug.core.Configuration;
20+
1721
public class JavaDebuggerServerPlugin implements BundleActivator {
22+
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
1823

1924
public static final String PLUGIN_ID = "com.microsoft.java.debug";
2025

2126

2227
@Override
2328
public void start(BundleContext context) throws Exception {
24-
System.out.println("Starting " + PLUGIN_ID);
29+
logger.info("Starting " + PLUGIN_ID);
2530
}
2631

2732
@Override
2833
public void stop(BundleContext context) throws Exception {
29-
34+
logger.info("Stopping " + PLUGIN_ID);
3035
}
3136

3237
}

‎com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
112
package com.microsoft.java.debug.plugin.internal;
213

314
import java.net.URI;

‎com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
112
package com.microsoft.java.debug.plugin.internal;
213

314
import java.util.Map;

‎com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java‎

Lines changed: 110 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
*******************************************************************************/
1111
package com.microsoft.java.debug.plugin.internal;
1212

13-
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException;
14-
1513
import java.util.ArrayList;
1614
import java.util.List;
15+
import java.util.logging.Logger;
1716

1817
import org.eclipse.core.resources.IProject;
1918
import org.eclipse.core.resources.IWorkspaceRoot;
@@ -32,120 +31,122 @@
3231
import org.eclipse.jdt.core.search.SearchPattern;
3332
import org.eclipse.jdt.core.search.SearchRequestor;
3433
import org.eclipse.jdt.launching.JavaRuntime;
35-
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
34+
35+
import com.microsoft.java.debug.core.Configuration;
3636

3737
public class ResolveClasspathsHandler {
38+
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
3839

39-
public String[] resolveClasspaths(List<Object> arguments) {
40-
try {
41-
return computeClassPath((String)arguments.get(0), (String)arguments.get(1));
42-
} catch (CoreException e) {
43-
logException("Failed to resolve classpath.", e);
44-
}
45-
return null;
46-
}
40+
public String[] resolveClasspaths(List<Object> arguments) throws Exception {
41+
try {
42+
return computeClassPath((String) arguments.get(0), (String) arguments.get(1));
43+
} catch (CoreException e) {
44+
logger.severe("Failed to resolve classpath: " + e.getMessage());
45+
throw new Exception("Failed to resolve classpath: " + e.getMessage(), e);
46+
}
47+
}
4748

48-
/**
49-
* Get java project from name.
50-
*
51-
* @param projectName
52-
* project name
53-
* @return java project
54-
* @throws CoreException
55-
* CoreException
56-
*/
57-
private static IJavaProject getJavaProjectFromName(String projectName) throws CoreException {
58-
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
59-
IProject project = root.getProject(projectName);
60-
if (!project.exists()) {
61-
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
62-
String.format("Cannot find the project with name '%s'.", projectName)));
63-
}
64-
if (!project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
65-
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
66-
String.format("The project '%s' does not have java nature enabled.", projectName)));
67-
}
68-
IJavaProject javaProject = JavaCore.create(project);
69-
return javaProject;
70-
}
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, JavaDebuggerServerPlugin.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, JavaDebuggerServerPlugin.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+
}
7172

72-
/**
73-
* Get java project from type.
74-
*
75-
* @param typeFullyQualifiedName
76-
* fully qualified name of type
77-
* @return java project
78-
* @throws CoreException
79-
* CoreException
80-
*/
81-
private static List<IJavaProject> getJavaProjectFromType(String typeFullyQualifiedName) throws CoreException {
82-
SearchPattern pattern = SearchPattern.createPattern(typeFullyQualifiedName, IJavaSearchConstants.TYPE,
83-
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
84-
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
85-
ArrayList<IJavaProject> projects = new ArrayList<>();
86-
SearchRequestor requestor = new SearchRequestor() {
87-
@Override
88-
public void acceptSearchMatch(SearchMatch match) {
89-
Object element = match.getElement();
90-
if (element instanceof IJavaElement) {
91-
projects.add(((IJavaElement) element).getJavaProject());
92-
}
93-
}
94-
};
95-
SearchEngine searchEngine = new SearchEngine();
96-
searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
97-
requestor, null /* progress monitor */);
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 */);
9899

99-
return projects;
100-
}
100+
return projects;
101+
}
101102

102-
/**
103-
* Accord to the project name and the main class, compute runtime classpath.
104-
*
105-
* @param mainClass
103+
/**
104+
* Accord to the project name and the main class, compute runtime classpath.
105+
*
106+
* @param mainClass
106107
* fully qualified class name
107-
* @param projectName
108-
* project name
109-
* @return class path
110-
* @throws CoreException
111-
* CoreException
112-
*/
113-
private static String[] computeClassPath(String mainClass, String projectName) throws CoreException {
114-
IJavaProject project = null;
115-
// if type exists in multiple projects, debug configuration need provide
116-
// project name.
117-
if (projectName != null) {
118-
project = getJavaProjectFromName(projectName);
119-
} else {
120-
List<IJavaProject> projects = getJavaProjectFromType(mainClass);
121-
if (projects.size() == 0) {
122-
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
123-
String.format("Main class '%s' doesn't exist in the workspace.", mainClass)));
124-
}
125-
if (projects.size() > 1) {
126-
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
127-
String.format(
128-
"Main class '%s' isn't unique in the workspace, please pass in specified projectname.",
129-
mainClass)));
130-
}
131-
project = projects.get(0);
132-
}
133-
return computeClassPath(project);
134-
}
108+
* @param projectName
109+
* project name
110+
* @return class path
111+
* @throws CoreException
112+
* CoreException
113+
*/
114+
private static String[] computeClassPath(String mainClass, String projectName) 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, JavaDebuggerServerPlugin.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, JavaDebuggerServerPlugin.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+
}
135136

136-
/**
137-
* Compute runtime classpath of a java project.
138-
*
139-
* @param javaProject
140-
* java project
141-
* @return class path
142-
* @throws CoreException
143-
* CoreException
144-
*/
145-
private static String[] computeClassPath(IJavaProject javaProject) throws CoreException {
146-
if (javaProject == null) {
147-
throw new IllegalArgumentException("javaProject is null");
148-
}
149-
return JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
150-
}
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+
}
151152
}

0 commit comments

Comments
 (0)