Skip to content

Commit

Permalink
Set JDK entry attribute according to the project module description
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo committed Mar 18, 2024
1 parent aa546aa commit ef7d729
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,36 +624,36 @@ public static JdkUpdateResult updateProjectJdk(String projectUri, String jdkPath
IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(projectUri);
List<IClasspathEntry> newClasspathEntries = new ArrayList<>();
try {
newClasspathEntries.add(getNewJdkEntry(javaProject, jdkPath));
for (IClasspathEntry entry : javaProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER &&
entry.getPath().toString().startsWith(JavaRuntime.JRE_CONTAINER)) {
newClasspathEntries.add(getNewJdkEntry(javaProject, jdkPath));
} else {
newClasspathEntries.add(entry);
}
}
javaProject.setRawClasspath(newClasspathEntries.toArray(IClasspathEntry[]::new), monitor);
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
return new JdkUpdateResult(false, e.getMessage());
}
javaProject.setRawClasspath(newClasspathEntries.toArray(IClasspathEntry[]::new), monitor);
return new JdkUpdateResult(true, "");
return new JdkUpdateResult(true, jdkPath);
}

private static IClasspathEntry getNewJdkEntry(IJavaProject javaProject, String jdkPath) throws CoreException {
IClasspathEntry[] originalClasspathEntries = javaProject.getRawClasspath();
IClasspathAttribute[] extraAttributes = null;
List<IClasspathEntry> newClasspathEntries = new ArrayList<>();
for (IClasspathEntry entry : originalClasspathEntries) {
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER &&
entry.getPath().toString().startsWith(JavaRuntime.JRE_CONTAINER)) {
extraAttributes = entry.getExtraAttributes();
} else {
newClasspathEntries.add(entry);
}
}

IVMInstall vmInstall = getVmInstallByPath(jdkPath);
List<IClasspathAttribute> extraAttributes = new ArrayList<>();
if (vmInstall == null) {
throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "The select JDK path is not valid."));
}
if (javaProject.getOwnModuleDescription() != null) {
extraAttributes.add(JavaCore.newClasspathAttribute(IClasspathAttribute.MODULE, "true"));
}

return JavaCore.newContainerEntry(
JavaRuntime.newJREContainerPath(vmInstall),
ClasspathEntry.NO_ACCESS_RULES,
extraAttributes,
extraAttributes.toArray(IClasspathAttribute[]::new),
false /*isExported*/
);
}
Expand Down

0 comments on commit ef7d729

Please sign in to comment.