Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions com.microsoft.java.debug.plugin/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.m2e.core.lifecycleMappingMetadataSource">
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
<delegateCommandHandler class="org.eclipse.jdt.ls.core.internal.DebugDelegateCommandHandlerFactory">
<command id="vscode.java.startDebugSession"/>
<command id="vscode.java.resolveClasspath"/>
<command id="vscode.java.buildWorkspace"/>
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DebugServerFactory implements IExecutableExtensionFactory {

@Override
public Object create() throws CoreException {
return JavaDebugServer.getInstance();
return JavaDebugDelegateCommandHandler.getInstance();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.microsoft.java.debug.plugin.internal;

import java.util.List;

import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;


public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler {

public static String DEBUG_STARTSESSION = "vscode.java.startDebugSession";

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

public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";

@Override
public Object executeCommand(String commandId, List<Object> arguments) {
if (DEBUG_STARTSESSION.equals(commandId)) {

} else if (RESOLVE_CLASSPATH.equals(commandId)) {
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
return handler.resolveClasspaths(arguments);
} else if (BUILD_WORKSPACE.equals(commandId)) {

}
return null;
}

public static JavaDebugDelegateCommandHandler getInstance() {
return new JavaDebugDelegateCommandHandler();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.microsoft.java.debug.plugin.internal;

import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not ref this class.


public class ResolveClasspathsHandler {

public String[] resolveClasspaths(List<Object> arguments) {
try {
return computeClassPath((String)arguments.get(0), (String)arguments.get(1));
} catch (CoreException e) {
logException("Failed to resolve classpath.", e);
}
return null;
}

/**
* Get java project from name.
*
* @param projectName
* project name
* @return java project
* @throws CoreException
* CoreException
*/
private static IJavaProject getJavaProjectFromName(String projectName) throws CoreException {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(projectName);
if (!project.exists()) {
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
String.format("Cannot find the project with name '%s'.", projectName)));
}
if (!project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
String.format("The project '%s' does not have java nature enabled.", projectName)));
}
IJavaProject javaProject = JavaCore.create(project);
return javaProject;
}

/**
* Get java project from type.
*
* @param typeFullyQualifiedName
* fully qualified name of type
* @return java project
* @throws CoreException
* CoreException
*/
private static List<IJavaProject> getJavaProjectFromType(String typeFullyQualifiedName) throws CoreException {
SearchPattern pattern = SearchPattern.createPattern(typeFullyQualifiedName, IJavaSearchConstants.TYPE,
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
ArrayList<IJavaProject> projects = new ArrayList<>();
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) {
Object element = match.getElement();
if (element instanceof IJavaElement) {
projects.add(((IJavaElement) element).getJavaProject());
}
}
};
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
requestor, null /* progress monitor */);

return projects;
}

/**
* Accord to the project name and the main class, compute runtime classpath.
*
* @param projectName
* project name
* @param mainClass
* full qualified class name
* @return class path
* @throws CoreException
* CoreException
*/
private static String[] computeClassPath(String projectName, String mainClass) throws CoreException {
IJavaProject project = null;
// if type exists in multiple projects, debug configuration need provide
// project name.
if (projectName != null) {
project = getJavaProjectFromName(projectName);
} else {
List<IJavaProject> projects = getJavaProjectFromType(mainClass);
if (projects.size() == 0) {
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
String.format("Main class '%s' doesn't exist in the workspace.", mainClass)));
}
if (projects.size() > 1) {
throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID,
String.format(
"Main class '%s' isn't unique in the workspace, please pass in specified projectname.",
mainClass)));
}
project = projects.get(0);
}
return computeClassPath(project);
}

/**
* Compute runtime classpath of a java project.
*
* @param javaProject
* java project
* @return class path
* @throws CoreException
* CoreException
*/
private static String[] computeClassPath(IJavaProject javaProject) throws CoreException {
if (javaProject == null) {
throw new IllegalArgumentException("javaProject is null");
}
return JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
}
}