diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtUtils.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtUtils.java index 77476e0ea..c1ae03826 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtUtils.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtUtils.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Set; +import org.apache.commons.lang3.StringUtils; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspaceRoot; @@ -103,7 +104,7 @@ public static IJavaProject getJavaProject(IProject project) { * If the project doesn't exist or not a java project, return null. */ public static IJavaProject getJavaProject(String projectName) { - if (projectName == null) { + if (StringUtils.isBlank(projectName)) { return null; } IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); @@ -111,12 +112,25 @@ public static IJavaProject getJavaProject(String projectName) { return getJavaProject(project); } + /** + * List all available Java projects of the specified workspace. + */ + public static List listJavaProjects(IWorkspaceRoot workspace) { + List results = new ArrayList<>(); + for (IProject project : workspace.getProjects()) { + if (isJavaProject(project)) { + results.add(JavaCore.create(project)); + } + } + return results; + } + /** * Given the project name, return the corresponding project object. * If the project doesn't exist, return null. */ public static IProject getProject(String projectName) { - if (projectName == null) { + if (StringUtils.isBlank(projectName)) { return null; } IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java index d2daf15b3..0e11dd39a 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveClasspathsHandler.java @@ -19,6 +19,8 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -83,6 +85,12 @@ private static IJavaProject getJavaProjectFromName(String projectName) throws Co * CoreException */ public static List getJavaProjectFromType(String fullyQualifiedTypeName) throws CoreException { + // If only one Java project exists in the whole workspace, return the project directly. + List javaProjects = JdtUtils.listJavaProjects(ResourcesPlugin.getWorkspace().getRoot()); + if (javaProjects.size() <= 1) { + return javaProjects; + } + String[] splitItems = fullyQualifiedTypeName.split("/"); // If the main class name contains the module name, should trim the module info. if (splitItems.length == 2) { @@ -132,7 +140,7 @@ private static String[][] computeClassPath(String mainClass, String projectName) IJavaProject project = null; // if type exists in multiple projects, debug configuration need provide // project name. - if (projectName != null) { + if (StringUtils.isNotBlank(projectName)) { project = getJavaProjectFromName(projectName); } else { List projects = getJavaProjectFromType(mainClass);