diff --git a/org.eclipse.jdt.ls.core/plugin.xml b/org.eclipse.jdt.ls.core/plugin.xml index f0840d6d21..fba9139db8 100644 --- a/org.eclipse.jdt.ls.core/plugin.xml +++ b/org.eclipse.jdt.ls.core/plugin.xml @@ -109,6 +109,9 @@ + + @@ -127,6 +130,9 @@ + + arguments, IProgress params.setPosition(textParams.getPosition()); TypeHierarchyItem typeHierarchyItem = typeHierarchyCommand.typeHierarchy(params, monitor); return typeHierarchyItem; - case "java.project.upgradeGradle": + case "java.project.upgradeGradle": { String projectUri = (String) arguments.get(0); String gradleVersion = arguments.size() > 1 ? (String) arguments.get(1) : null; if (gradleVersion == null) { gradleVersion = GradleVersion.current().getVersion(); } return GradleProjectImporter.upgradeGradleVersion(projectUri, gradleVersion, monitor); + } case "java.project.resolveWorkspaceSymbol": SymbolInformation si = JSONUtility.toModel(arguments.get(0), SymbolInformation.class); return ProjectCommand.resolveWorkspaceSymbol(si); + case "java.project.updateJdk": { + String projectUri = (String) arguments.get(0); + String jdkPath = (String) arguments.get(1); + return ProjectCommand.updateProjectJdk(projectUri, jdkPath, monitor); + } case "java.protobuf.generateSources": ProtobufSupport.generateProtobufSources((ArrayList) arguments.get(0), monitor); return null; @@ -177,6 +184,8 @@ public Object executeCommand(String commandId, List arguments, IProgress } SmartDetectionParams smartDetectionParams = JSONUtility.toModel(arguments.get(0), SmartDetectionParams.class); return new SmartDetectionHandler(smartDetectionParams).getLocation(monitor); + case VmCommand.GET_ALL_INSTALL_COMMAND_ID: + return VmCommand.getAllVmInstalls(); default: break; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java index 0d82dd3f2c..1a664dd3ae 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Microsoft Corporation and others. + * Copyright (c) 2020-2023 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IProject; @@ -37,6 +38,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.runtime.jobs.Job; @@ -52,9 +54,13 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.internal.core.ClasspathEntry; +import org.eclipse.jdt.internal.launching.StandardVMType; import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.IVMInstall2; +import org.eclipse.jdt.launching.IVMInstallType; import org.eclipse.jdt.launching.JavaLaunchDelegate; import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jdt.launching.VMStandin; import org.eclipse.jdt.ls.core.internal.IConstants; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; @@ -76,17 +82,19 @@ public class ProjectCommand { * Gets the project settings. * * @param uri - * Uri of the source/class file that needs to be queried. + * Uri of the source/class file that needs to be queried. * @param settingKeys - * the settings we want to query, for example: - * ["org.eclipse.jdt.core.compiler.compliance", - * "org.eclipse.jdt.core.compiler.source"]. - * Besides the options defined in JavaCore, the following keys can also be used: - * - "org.eclipse.jdt.ls.core.vm.location": Get the location of the VM assigned to build the given Java project - * - "org.eclipse.jdt.ls.core.sourcePaths": Get the source root paths of the given Java project - * - "org.eclipse.jdt.ls.core.outputPath": Get the default output path of the given Java project. Note that the default output path - * may not be equal to the output path of each source root. - * - "org.eclipse.jdt.ls.core.referencedLibraries": Get all the referenced library files of the given Java project + * the settings we want to query, for example: + * ["org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"]. + * + * Besides the options defined in JavaCore, the following keys can also be used: + * + * "org.eclipse.jdt.ls.core.vm.location": Get the location of the VM assigned to build the given Java project. + * "org.eclipse.jdt.ls.core.sourcePaths": Get the source root paths of the given Java project. + * "org.eclipse.jdt.ls.core.outputPath": Get the default output path of the given Java project. Note that the default output path + * may not be equal to the output path of each source root. + * "org.eclipse.jdt.ls.core.referencedLibraries": Get all the referenced library files of the given Java project. + * * @return A Map with all the setting keys and * their values. * @throws CoreException @@ -170,12 +178,9 @@ public static ClasspathResult getClasspathsFromJavaProject(IJavaProject javaProj } else { schedulingRule = javaProject.getSchedulingRule(); } - workspace.run(new IWorkspaceRunnable() { - @Override - public void run(IProgressMonitor monitor) throws CoreException { - String[][] paths = delegate.getClasspathAndModulepath(launchConfig); - result[0] = new ClasspathResult(javaProject.getProject().getLocationURI(), paths[0], paths[1]); - } + workspace.run((IWorkspaceRunnable) monitor -> { + String[][] paths = delegate.getClasspathAndModulepath(launchConfig); + result[0] = new ClasspathResult(javaProject.getProject().getLocationURI(), paths[0], paths[1]); }, schedulingRule, IWorkspace.AVOID_UPDATE, new NullProgressMonitor()); if (result[0] != null) { @@ -348,4 +353,82 @@ public static SymbolInformation resolveWorkspaceSymbol(SymbolInformation request return si; } + + public static JdkUpdateResult updateProjectJdk(String projectUri, String jdkPath, IProgressMonitor monitor) throws CoreException, URISyntaxException { + IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(projectUri); + IClasspathEntry[] originalClasspathEntries = javaProject.getRawClasspath(); + IClasspathAttribute[] extraAttributes = null; + List newClasspathEntries = new ArrayList<>(); + for (IClasspathEntry entry : originalClasspathEntries) { + if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && + entry.getPath().toString().startsWith("org.eclipse.jdt.launching.JRE_CONTAINER")) { + extraAttributes = entry.getExtraAttributes(); + } else { + newClasspathEntries.add(entry); + } + } + + IVMInstall vmInstall = getVmInstallByPath(jdkPath); + if (vmInstall == null) { + JavaLanguageServerPlugin.log(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "The select JDK path is not valid.")); + return new JdkUpdateResult(false, "The selected JDK path is not valid."); + } + newClasspathEntries.add(JavaCore.newContainerEntry( + JavaRuntime.newJREContainerPath(vmInstall), + ClasspathEntry.NO_ACCESS_RULES, + extraAttributes, + false /*isExported*/ + )); + javaProject.setRawClasspath(newClasspathEntries.toArray(IClasspathEntry[]::new), monitor); + return new JdkUpdateResult(true, vmInstall.getInstallLocation().getAbsolutePath()); + } + + private static IVMInstall getVmInstallByPath(String path) { + java.nio.file.Path vmPath = new Path(path).toPath(); + IVMInstallType[] vmInstallTypes = JavaRuntime.getVMInstallTypes(); + for (IVMInstallType vmInstallType : vmInstallTypes) { + IVMInstall[] vmInstalls = vmInstallType.getVMInstalls(); + for (IVMInstall vmInstall : vmInstalls) { + if (vmInstall.getInstallLocation().toPath().normalize().compareTo(vmPath) == 0) { + return vmInstall; + } + } + } + + StandardVMType standardType = (StandardVMType) JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); + VMStandin vmStandin = new VMStandin(standardType, path); + File jdkHomeFile = vmPath.toFile(); + vmStandin.setInstallLocation(jdkHomeFile); + String name = jdkHomeFile.getName(); + int i = 1; + while (isDuplicateName(name)) { + name = jdkHomeFile.getName() + '(' + i++ + ')'; + } + vmStandin.setName(name); + IVMInstall install = vmStandin.convertToRealVM(); + if (!(install instanceof IVMInstall2 vm && vm.getJavaVersion() != null)) { + // worksaround: such VMs may cause issue later + // https://github.com/eclipse-jdt/eclipse.jdt.debug/issues/248 + standardType.disposeVMInstall(install.getId()); + return null; + } + return install; + } + + private static boolean isDuplicateName(String name) { + return Stream.of(JavaRuntime.getVMInstallTypes()) // + .flatMap(vmType -> Arrays.stream(vmType.getVMInstalls())) // + .map(IVMInstall::getName) // + .anyMatch(name::equals); + } + + public static final class JdkUpdateResult { + public boolean success; + public String message; + + public JdkUpdateResult(boolean success, String message) { + this.success = success; + this.message = message; + } + } } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/VmCommand.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/VmCommand.java new file mode 100644 index 0000000000..5a13478884 --- /dev/null +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/VmCommand.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2023 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jdt.ls.core.internal.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.IVMInstall2; +import org.eclipse.jdt.launching.IVMInstallType; +import org.eclipse.jdt.launching.JavaRuntime; + +public class VmCommand { + private VmCommand() {} + + public static final String GET_ALL_INSTALL_COMMAND_ID = "java.vm.getAllInstalls"; + + /** + * List all available VM installs on the machine. + */ + public static final List getAllVmInstalls() { + List vmInstallList = new ArrayList<>(); + IVMInstallType[] vmInstallTypes = JavaRuntime.getVMInstallTypes(); + for (IVMInstallType vmInstallType : vmInstallTypes) { + IVMInstall[] vmInstalls = vmInstallType.getVMInstalls(); + for (IVMInstall vmInstall : vmInstalls) { + VmInstall vm = new VmInstall( + vmInstallType.getName(), + vmInstall.getName(), + vmInstall.getInstallLocation().getAbsolutePath() + ); + if (vmInstall instanceof IVMInstall2) { + vm.version = ((IVMInstall2) vmInstall).getJavaVersion(); + } + vmInstallList.add(vm); + } + } + return vmInstallList; + } + + public static final class VmInstall { + public String typeName; + public String name; + public String path; + public String version; + + public VmInstall(String typeName, String name, String path) { + this.typeName = typeName; + this.name = name; + this.path = path; + } + } +} 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 646ef5a9c9..547e2f7468 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Microsoft Corporation and others. + * Copyright (c) 2020-2023 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -26,6 +26,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.jdt.core.ICompilationUnit; @@ -38,6 +39,8 @@ import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathOptions; import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathResult; +import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.JdkUpdateResult; +import org.eclipse.jdt.ls.core.internal.commands.VmCommand.VmInstall; import org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; @@ -51,82 +54,82 @@ */ public class ProjectCommandTest extends AbstractInvisibleProjectBasedTest { - @Test - public void testGetProjectSettingsForMavenJava7() throws Exception { - importProjects("maven/salut"); - IProject project = WorkspaceHelper.getProject("salut"); - String uriString = project.getFile("src/main/java/Foo.java").getLocationURI().toString(); - List settingKeys = Arrays.asList("org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); - Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); - - assertEquals(settingKeys.size(), options.size()); - assertEquals("1.7", options.get("org.eclipse.jdt.core.compiler.compliance")); - assertEquals("1.7", options.get("org.eclipse.jdt.core.compiler.source")); - } - - @Test - public void testGetProjectSettingsForMavenJava8() throws Exception { - importProjects("maven/salut2"); - IProject project = WorkspaceHelper.getProject("salut2"); - String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString(); - List settingKeys = Arrays.asList("org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); - Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); - - assertEquals(settingKeys.size(), options.size()); - assertEquals("1.8", options.get("org.eclipse.jdt.core.compiler.compliance")); - assertEquals("1.8", options.get("org.eclipse.jdt.core.compiler.source")); - } - - @Test - public void testGetProjectVMInstallation() throws Exception { - importProjects("maven/salut2"); - IProject project = WorkspaceHelper.getProject("salut2"); - String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString(); - List settingKeys = Arrays.asList(ProjectCommand.VM_LOCATION); - Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); - - IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(uriString); - IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject); - assertNotNull(vmInstall); - File location = vmInstall.getInstallLocation(); - assertNotNull(location); - assertEquals(location.getAbsolutePath(), options.get(ProjectCommand.VM_LOCATION)); - } - - @Test - public void testGetProjectSourcePaths() throws Exception { - IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); - String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); - List settingKeys = Arrays.asList(ProjectCommand.SOURCE_PATHS); - Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); - String[] actualSourcePaths = (String[]) options.get(ProjectCommand.SOURCE_PATHS); - assertTrue(actualSourcePaths.length == 2); - assertTrue(Arrays.stream(actualSourcePaths).anyMatch(sourcePath -> { - return sourcePath.equals(project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("src").getLocation().toOSString()) - || sourcePath.equals(project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("test").getLocation().toOSString()); - })); - } - - @Test - public void testGetProjectOutputPath() throws Exception { - IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); - String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); - List settingKeys = Arrays.asList(ProjectCommand.OUTPUT_PATH); - Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); - String actualOutputPath = (String) options.get(ProjectCommand.OUTPUT_PATH); - String expectedOutputPath = project.getFolder("bin").getLocation().toOSString(); - assertEquals(expectedOutputPath, actualOutputPath); - } - - @Test - public void testGetProjectReferencedLibraryPaths() throws Exception { - IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); - String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); - List settingKeys = Arrays.asList(ProjectCommand.REFERENCED_LIBRARIES); - Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); - String[] actualReferencedLibraryPaths = (String[]) options.get(ProjectCommand.REFERENCED_LIBRARIES); - String expectedReferencedLibraryPath = project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("lib").getFile("mylib.jar").getLocation().toOSString(); - assertTrue(actualReferencedLibraryPaths.length == 1); + @Test + public void testGetProjectSettingsForMavenJava7() throws Exception { + importProjects("maven/salut"); + IProject project = WorkspaceHelper.getProject("salut"); + String uriString = project.getFile("src/main/java/Foo.java").getLocationURI().toString(); + List settingKeys = Arrays.asList("org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); + Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); + + assertEquals(settingKeys.size(), options.size()); + assertEquals("1.7", options.get("org.eclipse.jdt.core.compiler.compliance")); + assertEquals("1.7", options.get("org.eclipse.jdt.core.compiler.source")); + } + + @Test + public void testGetProjectSettingsForMavenJava8() throws Exception { + importProjects("maven/salut2"); + IProject project = WorkspaceHelper.getProject("salut2"); + String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString(); + List settingKeys = Arrays.asList("org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source"); + Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); + + assertEquals(settingKeys.size(), options.size()); + assertEquals("1.8", options.get("org.eclipse.jdt.core.compiler.compliance")); + assertEquals("1.8", options.get("org.eclipse.jdt.core.compiler.source")); + } + + @Test + public void testGetProjectVMInstallation() throws Exception { + importProjects("maven/salut2"); + IProject project = WorkspaceHelper.getProject("salut2"); + String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString(); + List settingKeys = Arrays.asList(ProjectCommand.VM_LOCATION); + Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); + + IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(uriString); + IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject); + assertNotNull(vmInstall); + File location = vmInstall.getInstallLocation(); + assertNotNull(location); + assertEquals(location.getAbsolutePath(), options.get(ProjectCommand.VM_LOCATION)); + } + + @Test + public void testGetProjectSourcePaths() throws Exception { + IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); + String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); + List settingKeys = Arrays.asList(ProjectCommand.SOURCE_PATHS); + Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); + String[] actualSourcePaths = (String[]) options.get(ProjectCommand.SOURCE_PATHS); + assertTrue(actualSourcePaths.length == 2); + assertTrue(Arrays.stream(actualSourcePaths).anyMatch(sourcePath -> { + return sourcePath.equals(project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("src").getLocation().toOSString()) + || sourcePath.equals(project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("test").getLocation().toOSString()); + })); + } + + @Test + public void testGetProjectOutputPath() throws Exception { + IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); + String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); + List settingKeys = Arrays.asList(ProjectCommand.OUTPUT_PATH); + Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); + String actualOutputPath = (String) options.get(ProjectCommand.OUTPUT_PATH); + String expectedOutputPath = project.getFolder("bin").getLocation().toOSString(); + assertEquals(expectedOutputPath, actualOutputPath); + } + + @Test + public void testGetProjectReferencedLibraryPaths() throws Exception { + IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); + String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); + List settingKeys = Arrays.asList(ProjectCommand.REFERENCED_LIBRARIES); + Map options = ProjectCommand.getProjectSettings(linkedFolder, settingKeys); + String[] actualReferencedLibraryPaths = (String[]) options.get(ProjectCommand.REFERENCED_LIBRARIES); + String expectedReferencedLibraryPath = project.getFolder(ProjectUtils.WORKSPACE_LINK).getFolder("lib").getFile("mylib.jar").getLocation().toOSString(); + assertTrue(actualReferencedLibraryPaths.length == 1); if (Platform.OS_WIN32.equals(Platform.getOS())) { IPath expected = new Path(expectedReferencedLibraryPath); IPath actual = new Path(actualReferencedLibraryPaths[0]); @@ -140,162 +143,162 @@ public void testGetProjectReferencedLibraryPaths() throws Exception { } else { assertEquals(expectedReferencedLibraryPath, actualReferencedLibraryPaths[0]); } - } - - @Test - public void testGetMavenProjectFromUri() throws Exception { - importProjects("maven/salut"); - IProject project = WorkspaceHelper.getProject("salut"); - String javaSource = project.getFile("src/main/java/Foo.java").getLocationURI().toString(); - IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource); - assertNotNull("Can get project from java file uri", javaProject); - - String projectUri = project.getLocationURI().toString(); - javaProject = ProjectCommand.getJavaProjectFromUri(projectUri); - assertNotNull("Can get project from project uri", javaProject); - } - - @Test - public void testGetMultiModuleMavenProjectFromUri() throws Exception { - importProjects("maven/multimodule3"); - IProject project = WorkspaceHelper.getProject("this_is_a_very_long_module_name"); - String javaSource = project.getFile("src/main/org/eclipse/App.java").getLocationURI().toString(); - IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource); - assertEquals("this_is_a_very_long_module_name", javaProject.getElementName()); - - String projectUri = project.getLocationURI().toString(); - javaProject = ProjectCommand.getJavaProjectFromUri(projectUri); - assertEquals("this_is_a_very_long_module_name", javaProject.getElementName()); - } - - @Test - public void testGetInvisibleProjectFromUri() throws Exception { - IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); - String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); - IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(linkedFolder); - assertNotNull("Can get project from linked folder uri", javaProject); - } - - @Test - public void testGetClasspathsForMaven() throws Exception { - importProjects("maven/classpathtest"); - IProject project = WorkspaceHelper.getProject("classpathtest"); - String uriString = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); - ClasspathOptions options = new ClasspathOptions(); - options.scope = "runtime"; - ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(1, result.classpaths.length); - assertEquals(0, result.modulepaths.length); - assertTrue(result.classpaths[0].indexOf("junit") == -1); - - options.scope = "test"; - result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(4, result.classpaths.length); - assertEquals(0, result.modulepaths.length); - boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> { - return element.indexOf("junit") > -1; - }); - assertTrue(containsJunit); - } - - @Test - public void testGetClasspathsForMavenWhenUpdating() throws Exception { - importProjects("maven/classpathtest"); - IProject project = WorkspaceHelper.getProject("classpathtest"); - String uriString = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); - ClasspathOptions options = new ClasspathOptions(); - options.scope = "test"; - - projectsManager.updateProject(project, true); - - for (int i = 0; i < 10; i++) { - ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(4, result.classpaths.length); - assertEquals(0, result.modulepaths.length); - boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> { - return element.indexOf("junit") > -1; - }); - assertTrue(containsJunit); - } - } - - @Test - public void testGetClasspathsForGradle() throws Exception { - importProjects("gradle/simple-gradle"); - IProject project = WorkspaceHelper.getProject("simple-gradle"); - String uriString = project.getFile("src/main/java/Library.java").getLocationURI().toString(); - ClasspathOptions options = new ClasspathOptions(); - 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 -> { - return element.indexOf("junit") > -1; - }); - assertTrue(containsJunit); - } - - @Test - public void testGetClasspathsForMavenModular() throws Exception { - importProjects("maven/modular-project"); - IProject project = WorkspaceHelper.getProject("modular-project"); - String uriString = project.getFile("src/main/java/modular/Main.java").getLocationURI().toString(); - ClasspathOptions options = new ClasspathOptions(); - options.scope = "test"; - ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(0, result.classpaths.length); - assertEquals(1, result.modulepaths.length); - } - - @Test - public void testGetClasspathsForEclipse() throws Exception { - importProjects("eclipse/hello"); - IProject project = WorkspaceHelper.getProject("hello"); - String uriString = project.getFile("src/java/Bar.java").getLocationURI().toString(); - ClasspathOptions options = new ClasspathOptions(); - options.scope = "runtime"; - ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(1, result.classpaths.length); - assertEquals(0, result.modulepaths.length); - - options.scope = "test"; - result = ProjectCommand.getClasspaths(uriString, options); - assertEquals(2, result.classpaths.length); - assertEquals(0, result.modulepaths.length); - } - - @Test - public void testIsTestFileForMaven() throws Exception { - importProjects("maven/classpathtest"); - IProject project = WorkspaceHelper.getProject("classpathtest"); - String srcUri = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); - String testUri = project.getFile("src/test/java/test/AppTest.java").getLocationURI().toString(); - assertFalse(ProjectCommand.isTestFile(srcUri)); - assertTrue(ProjectCommand.isTestFile(testUri)); - } - - @Test - public void testIsTestFileForGradle() throws Exception { - importProjects("gradle/simple-gradle"); - IProject project = WorkspaceHelper.getProject("simple-gradle"); - String srcUri = project.getFile("src/main/java/Library.java").getLocationURI().toString(); - String testUri = project.getFile("src/test/java/LibraryTest.java").getLocationURI().toString(); - assertFalse(ProjectCommand.isTestFile(srcUri)); - assertTrue(ProjectCommand.isTestFile(testUri)); - } - - @Test - public void getAllJavaProject() throws Exception { - importProjects("maven/multimodule"); - List projects = ProjectCommand.getAllJavaProjects(); - assertEquals(4, projects.size()); - } + } + + @Test + public void testGetMavenProjectFromUri() throws Exception { + importProjects("maven/salut"); + IProject project = WorkspaceHelper.getProject("salut"); + String javaSource = project.getFile("src/main/java/Foo.java").getLocationURI().toString(); + IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource); + assertNotNull("Can get project from java file uri", javaProject); + + String projectUri = project.getLocationURI().toString(); + javaProject = ProjectCommand.getJavaProjectFromUri(projectUri); + assertNotNull("Can get project from project uri", javaProject); + } + + @Test + public void testGetMultiModuleMavenProjectFromUri() throws Exception { + importProjects("maven/multimodule3"); + IProject project = WorkspaceHelper.getProject("this_is_a_very_long_module_name"); + String javaSource = project.getFile("src/main/org/eclipse/App.java").getLocationURI().toString(); + IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource); + assertEquals("this_is_a_very_long_module_name", javaProject.getElementName()); + + String projectUri = project.getLocationURI().toString(); + javaProject = ProjectCommand.getJavaProjectFromUri(projectUri); + assertEquals("this_is_a_very_long_module_name", javaProject.getElementName()); + } + + @Test + public void testGetInvisibleProjectFromUri() throws Exception { + IProject project = copyAndImportFolder("singlefile/simple", "src/App.java"); + String linkedFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK).getLocationURI().toString(); + IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(linkedFolder); + assertNotNull("Can get project from linked folder uri", javaProject); + } + + @Test + public void testGetClasspathsForMaven() throws Exception { + importProjects("maven/classpathtest"); + IProject project = WorkspaceHelper.getProject("classpathtest"); + String uriString = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); + ClasspathOptions options = new ClasspathOptions(); + options.scope = "runtime"; + ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(1, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + assertTrue(result.classpaths[0].indexOf("junit") == -1); + + options.scope = "test"; + result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(4, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> { + return element.indexOf("junit") > -1; + }); + assertTrue(containsJunit); + } + + @Test + public void testGetClasspathsForMavenWhenUpdating() throws Exception { + importProjects("maven/classpathtest"); + IProject project = WorkspaceHelper.getProject("classpathtest"); + String uriString = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); + ClasspathOptions options = new ClasspathOptions(); + options.scope = "test"; + + projectsManager.updateProject(project, true); + + for (int i = 0; i < 10; i++) { + ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(4, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + boolean containsJunit = Arrays.stream(result.classpaths).anyMatch(element -> { + return element.indexOf("junit") > -1; + }); + assertTrue(containsJunit); + } + } + + @Test + public void testGetClasspathsForGradle() throws Exception { + importProjects("gradle/simple-gradle"); + IProject project = WorkspaceHelper.getProject("simple-gradle"); + String uriString = project.getFile("src/main/java/Library.java").getLocationURI().toString(); + ClasspathOptions options = new ClasspathOptions(); + 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 -> { + return element.indexOf("junit") > -1; + }); + assertTrue(containsJunit); + } + + @Test + public void testGetClasspathsForMavenModular() throws Exception { + importProjects("maven/modular-project"); + IProject project = WorkspaceHelper.getProject("modular-project"); + String uriString = project.getFile("src/main/java/modular/Main.java").getLocationURI().toString(); + ClasspathOptions options = new ClasspathOptions(); + options.scope = "test"; + ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(0, result.classpaths.length); + assertEquals(1, result.modulepaths.length); + } + + @Test + public void testGetClasspathsForEclipse() throws Exception { + importProjects("eclipse/hello"); + IProject project = WorkspaceHelper.getProject("hello"); + String uriString = project.getFile("src/java/Bar.java").getLocationURI().toString(); + ClasspathOptions options = new ClasspathOptions(); + options.scope = "runtime"; + ClasspathResult result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(1, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + + options.scope = "test"; + result = ProjectCommand.getClasspaths(uriString, options); + assertEquals(2, result.classpaths.length); + assertEquals(0, result.modulepaths.length); + } + + @Test + public void testIsTestFileForMaven() throws Exception { + importProjects("maven/classpathtest"); + IProject project = WorkspaceHelper.getProject("classpathtest"); + String srcUri = project.getFile("src/main/java/main/App.java").getLocationURI().toString(); + String testUri = project.getFile("src/test/java/test/AppTest.java").getLocationURI().toString(); + assertFalse(ProjectCommand.isTestFile(srcUri)); + assertTrue(ProjectCommand.isTestFile(testUri)); + } + + @Test + public void testIsTestFileForGradle() throws Exception { + importProjects("gradle/simple-gradle"); + IProject project = WorkspaceHelper.getProject("simple-gradle"); + String srcUri = project.getFile("src/main/java/Library.java").getLocationURI().toString(); + String testUri = project.getFile("src/test/java/LibraryTest.java").getLocationURI().toString(); + assertFalse(ProjectCommand.isTestFile(srcUri)); + assertTrue(ProjectCommand.isTestFile(testUri)); + } + + @Test + public void getAllJavaProject() throws Exception { + importProjects("maven/multimodule"); + List projects = ProjectCommand.getAllJavaProjects(); + assertEquals(4, projects.size()); + } private static Range START_OF_DOCUMENT = new Range(new Position(0, 0), new Position(0, 0)); @@ -320,6 +323,36 @@ public void testResolveNestedClassSymbol() throws Exception { assertEquals(resolvedSymbol.getLocation().getRange(), new Range(new Position(17, 21), new Position(17, 28))); } + @Test + public void testUpdateProjectJdk() throws Exception { + importProjects("maven/salut"); + IProject project = WorkspaceHelper.getProject("salut"); + List allVmInstalls = VmCommand.getAllVmInstalls(); + VmInstall vmInstall = allVmInstalls.stream() + .filter(vm -> !vm.typeName.contains("org.eclipse.jdt.ls.core.internal.TestVMType")) + .findFirst().get(); + ProjectCommand.updateProjectJdk( + project.getLocationURI().toString(), + vmInstall.path, + new NullProgressMonitor() + ); + IJavaProject javaProject = ProjectUtils.getJavaProject(project); + IVMInstall vm = JavaRuntime.getVMInstall(javaProject); + assertEquals(vmInstall.path, vm.getInstallLocation().getAbsolutePath()); + } + + @Test + public void testUpdateInvalidProjectJdk() throws Exception { + importProjects("maven/salut"); + IProject project = WorkspaceHelper.getProject("salut"); + JdkUpdateResult updateProjectJdk = ProjectCommand.updateProjectJdk( + project.getLocationURI().toString(), + "invalid/path", + new NullProgressMonitor() + ); + assertFalse(updateProjectJdk.success); + } + private SymbolInformation buildClassSymbol(IProject project, String fqClassName) throws Exception { String uriString = buildClassfileUri(project, fqClassName); SymbolInformation si = new SymbolInformation(); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/VmCommandTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/VmCommandTest.java new file mode 100644 index 0000000000..0ad711a203 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/VmCommandTest.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2023 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jdt.ls.core.internal.commands; + +import org.eclipse.jdt.ls.core.internal.commands.VmCommand.VmInstall; +import org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest; + +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Test; + +public class VmCommandTest extends AbstractInvisibleProjectBasedTest { + + @Test + public void testGetAllVmInstalls() { + List allVmInstalls = VmCommand.getAllVmInstalls(); + assertTrue(allVmInstalls.size() > 0); + + assertTrue(allVmInstalls.stream() + .anyMatch(vm -> vm.typeName.contains("org.eclipse.jdt.ls.core.internal.TestVMType")) + ); + } +}
+ * Besides the options defined in JavaCore, the following keys can also be used: + *
Map