From 9137bedd1f51a13d82114661fdb6623b169fff4e Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 21 Sep 2022 15:10:26 +0800 Subject: [PATCH] Fix gradle project classpath calculation use 'org.eclipse.jdt.launching.localJavaApplication' configuration type and use 'excludeTest' attribute to specify whether test classpath should be included. Signed-off-by: Sheng Chen --- .../managers/JavaLaunchConfigurationInfo.java | 15 ++------------- .../internal/commands/ProjectCommandTest.java | 10 +++++++--- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/JavaLaunchConfigurationInfo.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/JavaLaunchConfigurationInfo.java index 6d46a36104..e530aadca5 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/JavaLaunchConfigurationInfo.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/JavaLaunchConfigurationInfo.java @@ -33,7 +33,7 @@ public class JavaLaunchConfigurationInfo extends LaunchConfigurationInfo { private static final String JAVA_APPLICATION_LAUNCH = "\n" - + "\n" + + "\n" + "\n" + "\n" + "\n" @@ -43,21 +43,10 @@ public class JavaLaunchConfigurationInfo extends LaunchConfigurationInfo { public JavaLaunchConfigurationInfo(String scope) { super(); - // Since MavenRuntimeClasspathProvider will only encluding 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. - // See: {@link org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider#getArtifactScope(ILaunchConfiguration)} - String launchXml = null; - if ("test".equals(scope)) { - launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.junit.launchconfig"); - } else { - launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.launching.localJavaApplication"); - } try { DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); parser.setErrorHandler(new DefaultHandler()); - StringReader reader = new StringReader(launchXml); + StringReader reader = new StringReader(JAVA_APPLICATION_LAUNCH); InputSource source = new InputSource(reader); Element root = parser.parse(source).getDocumentElement(); initializeFromXML(root); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java index c429582915..9ab1816f6e 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java @@ -210,10 +210,14 @@ public void testGetClasspathsForGradle() throws Exception { IProject project = WorkspaceHelper.getProject("simple-gradle"); String uriString = project.getFile("src/main/java/Library.java").getLocationURI().toString(); ClasspathOptions options = new ClasspathOptions(); - // Gradle project will always return classpath containing test dependencies. - // So we only test `scope = "test"` scenario. - options.scope = "test"; + options.scope = "runtime"; ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(3, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + assertTrue(result.classpaths[0].indexOf("junit") == -1); + + options.scope = "test"; + result = ProjectCommand.getClasspaths(uriString, options); assertEquals(5, result.classpaths.length); assertEquals(0, result.modulepaths.length); boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> {