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
1 change: 1 addition & 0 deletions com.microsoft.java.debug.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<delegateCommandHandler class="com.microsoft.java.debug.plugin.internal.JavaDebugDelegateCommandHandler">
<command id="vscode.java.startDebugSession"/>
<command id="vscode.java.resolveClasspath"/>
<command id="vscode.java.resolveMainClass"/>
<command id="vscode.java.buildWorkspace"/>
<command id="vscode.java.fetchUsageData"/>
<command id="vscode.java.configLogLevel"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler

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

public static String RESOLVE_MAINCLASS = "vscode.java.resolveMainClass";

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

public static String CONFIG_LOG_LEVEL = "vscode.java.configLogLevel";
Expand All @@ -40,6 +42,9 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
} else if (RESOLVE_CLASSPATH.equals(commandId)) {
ResolveClasspathsHandler handler = new ResolveClasspathsHandler();
return handler.resolveClasspaths(arguments);
} else if (RESOLVE_MAINCLASS.equals(commandId)) {
ResolveMainClassHandler handler = new ResolveMainClassHandler();
return handler.resolveMainClass();
} else if (BUILD_WORKSPACE.equals(commandId)) {
// TODO
} else if (FETCH_USER_DATA.equals(commandId)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation 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 java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
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.ls.core.internal.managers.ProjectsManager;

import com.microsoft.java.debug.core.Configuration;

public class ResolveMainClassHandler {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);

/**
* resolve main class and project name.
* @return an array of main class and project name
* @throws CoreException when there are errors when resolving main class.
*/
public Object resolveMainClass() throws CoreException {
return resolveMainClassCore();
}

private List<ResolutionItem> resolveMainClassCore() throws CoreException {

@testforstephen testforstephen Oct 18, 2017

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.

new method resolveMainClassCore is unnecessary.[pending]

IJavaSearchScope searchScope = SearchEngine.createWorkspaceScope();
SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD,
IJavaSearchConstants.DECLARATIONS, SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH);
ArrayList<ResolutionItem> res = new ArrayList<>();
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) {
Object element = match.getElement();
if (element instanceof IMethod) {
IMethod method = (IMethod) element;
try {
if (method.isMainMethod()) {
IResource resource = method.getResource();
if (resource != null) {
IProject project = resource.getProject();
if (project != null) {
String mainClass = method.getDeclaringType().getFullyQualifiedName();
String projectName = ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName()) ? null : project.getName();
res.add(new ResolutionItem(mainClass, projectName));
}
}
}
} catch (JavaModelException e) {
// ignore
}
}
}
};
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
searchScope, requestor, null /* progress monitor */);
return res.stream().distinct().collect(Collectors.toList());
}

private class ResolutionItem {
private String mainClass;
private String projectName;

public ResolutionItem(String mainClass, String projectName) {
this.mainClass = mainClass;
this.projectName = projectName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof ResolutionItem) {
ResolutionItem item = (ResolutionItem) o;
if (mainClass != null ? !mainClass.equals(item.mainClass) : item.mainClass != null) {
return false;
}
if (projectName != null ? !projectName.equals(item.projectName) : item.projectName != null) {
return false;
}
return true;
}
return false;
}

@Override
public int hashCode() {
return (mainClass == null ? 0 : mainClass.hashCode()) * 13 + (projectName == null ? 0 : projectName.hashCode());
}
}
}