Skip to content

Commit

Permalink
Support customizing the classpaths and modulepaths (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored May 21, 2021
1 parent 71e9ac5 commit a024806
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public class ResolveClasspathsHandler {
*/
public String[][] resolveClasspaths(List<Object> arguments) throws Exception {
try {
if (arguments.size() == 3) {
return computeClassPath((String) arguments.get(0), (String) arguments.get(1), (String) arguments.get(2));
}
return computeClassPath((String) arguments.get(0), (String) arguments.get(1));
} catch (CoreException e) {
logger.log(Level.SEVERE, "Failed to resolve classpath: " + e.getMessage(), e);
Expand Down Expand Up @@ -159,6 +162,23 @@ public void acceptSearchMatch(SearchMatch match) {
* CoreException
*/
private static String[][] computeClassPath(String mainClass, String projectName) throws CoreException {
return computeClassPath(mainClass, projectName, null);
}

/**
* Accord to the project name and the main class, compute runtime classpath.
*
* @param mainClass
* fully qualified class name
* @param projectName
* project name
* @param scope
* scope of the classpath
* @return class path
* @throws CoreException
* CoreException
*/
private static String[][] computeClassPath(String mainClass, String projectName, String scope) throws CoreException {
IJavaProject project = null;
// if type exists in multiple projects, debug configuration need provide
// project name.
Expand All @@ -179,6 +199,12 @@ private static String[][] computeClassPath(String mainClass, String projectName)
project = projects.get(0);
}

if ("test".equals(scope)) {
return computeClassPath(project, mainClass, false /*excludeTestCode*/, Collections.EMPTY_LIST);
} else if ("runtime".equals(scope)) {
return computeClassPath(project, mainClass, true /*excludeTestCode*/, Collections.EMPTY_LIST);
}

IJavaElement testElement = findMainClassInTestFolders(project, mainClass);
List<IResource> mappedResources = (testElement != null && testElement.getResource() != null)
? Arrays.asList(testElement.getResource()) : Collections.EMPTY_LIST;
Expand Down Expand Up @@ -275,7 +301,7 @@ public void acceptSearchMatch(SearchMatch match) {

private static class JavaApplicationLaunchConfiguration extends LaunchConfiguration {
public static final String JAVA_APPLICATION_LAUNCH = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<launchConfiguration type=\"org.eclipse.jdt.launching.localJavaApplication\">\n"
+ "<launchConfiguration type=\"%s\">\n"
+ "<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS\">\n"
+ "</listAttribute>\n"
+ "<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES\">\n"
Expand All @@ -302,7 +328,18 @@ protected JavaApplicationLaunchConfiguration(IProject project, String mainType,
} else if (ProjectUtils.isGradleProject(project)) {
classpathProvider = "org.eclipse.buildship.core.classpathprovider";
}
this.launchInfo = new JavaLaunchConfigurationInfo(JAVA_APPLICATION_LAUNCH);

// Since MavenRuntimeClasspathProvider will only including test entries when:
// 1. Launch configuration is JUnit/TestNG type
// 2. Mapped resource is in test path.
// That's why we use JUnit launch configuration here to make sure the result is right when excludeTestCode is false.
String launchXml = null;
if (!excludeTestCode && mappedResources.isEmpty()) {
launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.junit.launchconfig");
} else {
launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.launching.localJavaApplication");
}
this.launchInfo = new JavaLaunchConfigurationInfo(launchXml);
}

@Override
Expand Down

0 comments on commit a024806

Please sign in to comment.