LAB-99 Fix JDK discovery on /fsd: paths

* Use NIO instead of legacy Java IO
* Don't query `SystemInfo`, since JDK maybe on different OS
* Remove obsolete path from macOS jar dir search path (`jre/lib/rt.jar`)

GitOrigin-RevId: d90a927c2c459cc832ab5f2938728aa8b74807d4
This commit is contained in:
Florian Kistner
2021-08-09 15:15:40 +02:00
committed by intellij-monorepo-bot
parent 14bd2509ad
commit 25c95b4356
3 changed files with 37 additions and 34 deletions

View File

@@ -340,23 +340,28 @@ public final class JavaSdkImpl extends JavaSdk {
static VirtualFile internalJdkAnnotationsPath(@NotNull List<? super String> pathsChecked, boolean refresh) {
Path javaPluginClassesRootPath = PathManager.getJarForClass(JavaSdkImpl.class);
LOG.assertTrue(javaPluginClassesRootPath != null);
File javaPluginClassesRoot = javaPluginClassesRootPath.toFile();
VirtualFile root;
VirtualFileManager vfm = VirtualFileManager.getInstance();
LocalFileSystem lfs = LocalFileSystem.getInstance();
if (javaPluginClassesRoot.isFile()) {
String annotationsJarPath = FileUtil.toSystemIndependentName(new File(javaPluginClassesRoot.getParentFile(), "jdkAnnotations.jar").getAbsolutePath());
String url = "jar://" + annotationsJarPath + "!/";
if (Files.isRegularFile(javaPluginClassesRootPath)) {
Path annotationsJarPath = javaPluginClassesRootPath.resolveSibling("jdkAnnotations.jar").toAbsolutePath();
String annotationsJarPathString = FileUtil.toSystemIndependentName(annotationsJarPath.toString());
String url = "jar://" + annotationsJarPathString + "!/";
root = refresh ? vfm.refreshAndFindFileByUrl(url) : vfm.findFileByUrl(url);
pathsChecked.add(annotationsJarPath);
pathsChecked.add(annotationsJarPathString);
}
else {
// when run against IDEA plugin JDK, something like this comes up: "$IDEA_HOME$/out/classes/production/intellij.java.impl"
File projectRoot = JBIterable.generate(javaPluginClassesRoot, File::getParentFile).get(4);
File root1 = new File(projectRoot, "community/java/jdkAnnotations");
File root2 = new File(projectRoot, "java/jdkAnnotations");
root = root1.exists() && root1.isDirectory() ? refresh ? lfs.refreshAndFindFileByIoFile(root1) : lfs.findFileByIoFile(root1) :
root2.exists() && root2.isDirectory() ? refresh ? lfs.refreshAndFindFileByIoFile(root2) : lfs.findFileByIoFile(root2) : null;
Path projectRoot = JBIterable.generate(javaPluginClassesRootPath, Path::getParent).get(4);
if (projectRoot != null) {
Path root1 = projectRoot.resolve("community/java/jdkAnnotations");
Path root2 = projectRoot.resolve("java/jdkAnnotations");
root = Files.isDirectory(root1) ? (refresh ? lfs.refreshAndFindFileByNioFile(root1) : lfs.findFileByNioFile(root1)) :
Files.isDirectory(root2) ? (refresh ? lfs.refreshAndFindFileByNioFile(root2) : lfs.findFileByNioFile(root2)) : null;
}
else {
root = null;
}
}
if (root == null) {
String url = "jar://" + FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar!/";
@@ -464,7 +469,7 @@ public final class JavaSdkImpl extends JavaSdk {
try {
try (DirectoryStream<Path> roots = Files.newDirectoryStream(jdkHome.resolve("modules"))) {
for (Path root : roots) {
result.add(VfsUtil.getUrlForLibraryRoot(root.toFile()));
result.add(VfsUtil.getUrlForLibraryRoot(root));
}
}
}
@@ -489,7 +494,7 @@ public final class JavaSdkImpl extends JavaSdk {
}
else {
for (Path root : JavaSdkUtil.getJdkClassesRoots(jdkHome, isJre)) {
result.add(VfsUtil.getUrlForLibraryRoot(root.toFile()));
result.add(VfsUtil.getUrlForLibraryRoot(root));
}
}

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.jps.model.java.impl;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.util.containers.CollectionFactory;
import com.intellij.util.containers.ContainerUtil;
@@ -28,22 +27,12 @@ public final class JavaSdkUtil {
public static @NotNull List<Path> getJdkClassesRoots(@NotNull Path home, boolean isJre) {
Path[] jarDirs;
if (SystemInfo.isMac && !home.getFileName().startsWith("mockJDK")) {
Path openJdkRtJar = home.resolve("jre/lib/rt.jar");
if (Files.isReadable(openJdkRtJar)) {
Path libDir = home.resolve("lib");
Path classesDir = openJdkRtJar.getParent();
Path libExtDir = openJdkRtJar.resolveSibling("ext");
Path libEndorsedDir = libDir.resolve("endorsed");
jarDirs = new Path[]{libEndorsedDir, libDir, classesDir, libExtDir};
}
else {
Path libDir = home.resolve("lib");
Path classesDir = home.resolveSibling("Classes");
Path libExtDir = libDir.resolve("ext");
Path libEndorsedDir = libDir.resolve("endorsed");
jarDirs = new Path[]{libEndorsedDir, libDir, classesDir, libExtDir};
}
if ("Home".equals(home.getFileName().toString()) && Files.exists(home.resolve("../Classes/classes.jar"))) {
Path libDir = home.resolve("lib");
Path classesDir = home.resolveSibling("Classes");
Path libExtDir = libDir.resolve("ext");
Path libEndorsedDir = libDir.resolve("endorsed");
jarDirs = new Path[]{libEndorsedDir, libDir, classesDir, libExtDir};
}
else if (Files.exists(home.resolve("lib/jrt-fs.jar"))) {
jarDirs = new Path[0];

View File

@@ -280,11 +280,20 @@ public final class VfsUtil extends VfsUtilCore {
@NotNull
public static String getUrlForLibraryRoot(@NotNull File libraryRoot) {
String path = FileUtil.toSystemIndependentName(libraryRoot.getAbsolutePath());
if (FileTypeRegistry.getInstance().getFileTypeByFileName(libraryRoot.getName()) == ArchiveFileType.INSTANCE) {
return VirtualFileManager.constructUrl(StandardFileSystems.JAR_PROTOCOL, path + URLUtil.JAR_SEPARATOR);
}
return VirtualFileManager.constructUrl(LocalFileSystem.getInstance().getProtocol(), path);
return getUrlForLibraryRoot(libraryRoot.getAbsolutePath(), libraryRoot.getName());
}
@NotNull
public static String getUrlForLibraryRoot(@NotNull Path libraryRoot) {
return getUrlForLibraryRoot(libraryRoot.toAbsolutePath().toString(), libraryRoot.getFileName().toString());
}
@NotNull
private static String getUrlForLibraryRoot(@NotNull String libraryRootAbsolutePath, @NotNull String libraryRootFileName) {
String path = FileUtil.toSystemIndependentName(libraryRootAbsolutePath);
return FileTypeRegistry.getInstance().getFileTypeByFileName(libraryRootFileName) == ArchiveFileType.INSTANCE
? VirtualFileManager.constructUrl(StandardFileSystems.JAR_PROTOCOL, path + URLUtil.JAR_SEPARATOR)
: VirtualFileManager.constructUrl(LocalFileSystem.getInstance().getProtocol(), path);
}
public static @NotNull String getNextAvailableName(@NotNull VirtualFile dir,