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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,20 +104,33 @@ 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();
IProject project = root.getProject(projectName);
return getJavaProject(project);
}

/**
* List all available Java projects of the specified workspace.
*/
public static List<IJavaProject> listJavaProjects(IWorkspaceRoot workspace) {
List<IJavaProject> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +85,12 @@ private static IJavaProject getJavaProjectFromName(String projectName) throws Co
* CoreException
*/
public static List<IJavaProject> getJavaProjectFromType(String fullyQualifiedTypeName) throws CoreException {
// If only one Java project exists in the whole workspace, return the project directly.
List<IJavaProject> 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) {
Expand Down Expand Up @@ -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<IJavaProject> projects = getJavaProjectFromType(mainClass);
Expand Down