diff --git a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java index a0b8ab3e4615..8cda2ed3718e 100644 --- a/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java +++ b/java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaSdkImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// 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 com.intellij.openapi.projectRoots.impl; import com.intellij.codeInsight.BaseExternalAnnotationsManager; @@ -53,9 +53,9 @@ import javax.swing.*; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; @@ -223,10 +223,10 @@ public final class JavaSdkImpl extends JavaSdk { @Override public String adjustSelectedSdkHome(@NotNull String homePath) { if (SystemInfo.isMac) { - Path home = Paths.get(homePath, "/Home"); + Path home = Path.of(homePath, "/Home"); if (Files.exists(home)) return home.toString(); - home = Paths.get(homePath, "Contents/Home"); + home = Path.of(homePath, "Contents/Home"); if (Files.exists(home)) return home.toString(); } @@ -281,7 +281,7 @@ public final class JavaSdkImpl extends JavaSdk { public void setupSdkPaths(@NotNull Sdk sdk) { String homePath = sdk.getHomePath(); assert homePath != null : sdk; - Path jdkHome = Paths.get(homePath); + Path jdkHome = Path.of(homePath); SdkModificator sdkModificator = sdk.getSdkModificator(); List classes = findClasses(jdkHome, false); @@ -442,9 +442,9 @@ public final class JavaSdkImpl extends JavaSdk { @NotNull @Override public Sdk createJdk(@NotNull String jdkName, @NotNull String home, boolean isJre) { - Path jdkHomePath = Paths.get(home); + Path jdkHomePath = Path.of(home); if (!Files.exists(jdkHomePath)) { - throw new IllegalArgumentException(jdkHomePath.toAbsolutePath().toString() + " doesn't exist"); + throw new IllegalArgumentException(jdkHomePath.toAbsolutePath() + " doesn't exist"); } ProjectJdkImpl jdk = new ProjectJdkImpl(jdkName, this); SdkModificator sdkModificator = jdk.getSdkModificator(); @@ -498,18 +498,18 @@ public final class JavaSdkImpl extends JavaSdk { private static List findClasses(@NotNull Path jdkHome, boolean isJre) { List result = new ArrayList<>(); - if (JdkUtil.isExplodedModularRuntime(jdkHome.toString())) { + if (JdkUtil.isExplodedModularRuntime(jdkHome)) { try { - Path[] exploded = Files.list(jdkHome.resolve("modules")).toArray(Path[]::new); - for (Path root : exploded) { - result.add(VfsUtil.getUrlForLibraryRoot(root.toFile())); + try (DirectoryStream roots = Files.newDirectoryStream(jdkHome.resolve("modules"))) { + for (Path root : roots) { + result.add(VfsUtil.getUrlForLibraryRoot(root.toFile())); + } } } - catch (IOException ignore){ - } + catch (IOException ignore) { } } - else if (JdkUtil.isModularRuntime(jdkHome.toString())) { - String jrtBaseUrl = JrtFileSystem.PROTOCOL_PREFIX + getPath(jdkHome) + JrtFileSystem.SEPARATOR; + else if (JdkUtil.isModularRuntime(jdkHome)) { + String jrtBaseUrl = JrtFileSystem.PROTOCOL_PREFIX + vfsPath(jdkHome) + JrtFileSystem.SEPARATOR; List modules = readModulesFromReleaseFile(jdkHome); if (modules != null) { for (String module : modules) { @@ -567,11 +567,7 @@ public final class JavaSdkImpl extends JavaSdk { } Path srcDir = jdkHome.resolve("src"); - if (Files.isDirectory(srcDir)) { - return LocalFileSystem.getInstance().findFileByPath(getPath(srcDir)); - } - - return null; + return Files.isDirectory(srcDir) ? LocalFileSystem.getInstance().findFileByNioFile(srcDir) : null; } private void addDocs(Path jdkHome, SdkModificator sdkModificator, @Nullable Sdk sdk) { @@ -618,17 +614,17 @@ public final class JavaSdkImpl extends JavaSdk { @Nullable private static VirtualFile findDocs(@NotNull Path jdkHome, @NotNull String relativePath) { Path docDir = jdkHome.resolve(relativePath); - return Files.isDirectory(docDir) ? LocalFileSystem.getInstance().findFileByPath(getPath(docDir)) : null; + return Files.isDirectory(docDir) ? LocalFileSystem.getInstance().findFileByNioFile(docDir) : null; } private static VirtualFile findInJar(Path jarFile, String relativePath) { if (!Files.exists(jarFile)) return null; - String url = JarFileSystem.PROTOCOL_PREFIX + getPath(jarFile) + JarFileSystem.JAR_SEPARATOR + relativePath; + String url = JarFileSystem.PROTOCOL_PREFIX + vfsPath(jarFile) + JarFileSystem.JAR_SEPARATOR + relativePath; return VirtualFileManager.getInstance().findFileByUrl(url); } - private static String getPath(Path jarFile) { - return FileUtil.toSystemIndependentName(jarFile.toAbsolutePath().toString()); + private static String vfsPath(Path path) { + return FileUtil.toSystemIndependentName(path.toAbsolutePath().toString()); } @Override @@ -638,4 +634,4 @@ public final class JavaSdkImpl extends JavaSdk { type == JavadocOrderRootType.getInstance() || type == AnnotationOrderRootType.getInstance(); } -} \ No newline at end of file +} diff --git a/java/java-impl/src/com/intellij/openapi/vfs/impl/jrt/JrtFileSystemImpl.java b/java/java-impl/src/com/intellij/openapi/vfs/impl/jrt/JrtFileSystemImpl.java index 7fcefa51f430..83646cd01248 100644 --- a/java/java-impl/src/com/intellij/openapi/vfs/impl/jrt/JrtFileSystemImpl.java +++ b/java/java-impl/src/com/intellij/openapi/vfs/impl/jrt/JrtFileSystemImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// 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 com.intellij.openapi.vfs.impl.jrt; import com.intellij.openapi.application.Application; @@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; +import java.nio.file.Path; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -148,7 +149,7 @@ public class JrtFileSystemImpl extends JrtFileSystem { @Override protected boolean isCorrectFileType(@NotNull VirtualFile local) { - String path = local.getPath(); + Path path = local.toNioPath(); return JdkUtil.isModularRuntime(path) && !JdkUtil.isExplodedModularRuntime(path); } @@ -159,4 +160,4 @@ public class JrtFileSystemImpl extends JrtFileSystem { if (handler == null) throw new IllegalArgumentException(localPath + " not in " + myHandlers.keySet()); handler.dispose(); } -} \ No newline at end of file +} diff --git a/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java b/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java index e6dbcddfe5b6..2bd6f3aabbb4 100644 --- a/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java +++ b/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// 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 com.intellij.testFramework; import com.intellij.openapi.Disposable; @@ -26,7 +26,6 @@ import org.junit.Assume; import java.io.File; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -90,9 +89,6 @@ public final class IdeaTestUtil { "classes are included into classpath and that the plugin isn't disabled by using 'idea.load.plugins', 'idea.load.plugins.id', 'idea.load.plugins.category' system properties"); } - String homePath = PathUtil.toSystemIndependentName(path); - Path jdkHomeFile = Paths.get(homePath); - MultiMap roots = MultiMap.create(); SdkModificator sdkModificator = new SdkModificator() { @NotNull @@ -117,11 +113,12 @@ public final class IdeaTestUtil { } }; + Path jdkHomeFile = Path.of(path); JavaSdkImpl.addClasses(jdkHomeFile, sdkModificator, isJre); JavaSdkImpl.addSources(jdkHomeFile, sdkModificator); JavaSdkImpl.attachJdkAnnotations(sdkModificator); - return new MockSdk(name, homePath, name, roots, () -> JavaSdk.getInstance()); + return new MockSdk(name, PathUtil.toSystemIndependentName(path), name, roots, () -> JavaSdk.getInstance()); } public static @NotNull Sdk getMockJdk14() { @@ -284,4 +281,4 @@ public final class IdeaTestUtil { if (result != 0) throw new IllegalStateException("javac failed with exit code " + result); } } -} \ No newline at end of file +} diff --git a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaSdkUtil.java b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaSdkUtil.java index d4ce07f70f2f..a77153473f21 100644 --- a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaSdkUtil.java +++ b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaSdkUtil.java @@ -1,8 +1,7 @@ -// Copyright 2000-2020 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. +// 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.io.FileUtilRt; import com.intellij.openapi.util.registry.Registry; import com.intellij.util.containers.CollectionFactory; import com.intellij.util.containers.ContainerUtil; @@ -11,16 +10,14 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Set; -import java.util.function.Predicate; public final class JavaSdkUtil { - private static final Path[] EMPTY_PATH_ARRAY = new Path[0]; - /** * @deprecated use {@link #getJdkClassesRoots(Path, boolean)} instead */ @@ -51,7 +48,7 @@ public final class JavaSdkUtil { } } else if (Files.exists(home.resolve("lib/jrt-fs.jar"))) { - jarDirs = EMPTY_PATH_ARRAY; + jarDirs = new Path[0]; } else { Path libDir = home.resolve(isJre ? "lib" : "jre/lib"); @@ -60,7 +57,6 @@ public final class JavaSdkUtil { jarDirs = new Path[]{libEndorsedDir, libDir, libExtDir}; } - Predicate jarFileFilter = path -> FileUtilRt.extensionEquals(path.toString(), "jar"); Set pathFilter = CollectionFactory.createFilePathSet(); List rootFiles = new ArrayList<>(); if (Registry.is("project.structure.add.tools.jar.to.new.jdk", false)) { @@ -71,31 +67,39 @@ public final class JavaSdkUtil { } for (Path jarDir : jarDirs) { if (jarDir != null && Files.isDirectory(jarDir)) { - for (Path jarFile : listFiles(jarDir, jarFileFilter)) { - String jarFileName = jarFile.getFileName().toString(); - if (jarFileName.equals("alt-rt.jar") || jarFileName.equals("alt-string.jar")) { - continue; // filter out alternative implementations + try (DirectoryStream stream = Files.newDirectoryStream(jarDir, "*.jar")) { + for (Path jarFile : stream) { + String jarFileName = jarFile.getFileName().toString(); + if (jarFileName.equals("alt-rt.jar") || jarFileName.equals("alt-string.jar")) { + continue; // filter out alternative implementations + } + String canonicalPath = getCanonicalPath(jarFile); + if (canonicalPath == null || !pathFilter.add(canonicalPath)) { + continue; // filter out duplicate (symbolically linked) .jar files commonly found in OS X JDK distributions + } + rootFiles.add(jarFile); } - String canonicalPath = getCanonicalPath(jarFile); - if (canonicalPath == null || !pathFilter.add(canonicalPath)) { - continue; // filter out duplicate (symbolically linked) .jar files commonly found in OS X JDK distributions - } - rootFiles.add(jarFile); } + catch (IOException ignored) { } } } - List ibmJdkLookupDirs = ContainerUtil.newArrayList(home.resolve(isJre ? "bin" : "jre/bin")); - ContainerUtil.addAll(ibmJdkLookupDirs, listFiles(home.resolve(isJre ? "lib" : "jre/lib"), path -> Files.isDirectory(path))); + List ibmJdkLookupDirs = new ArrayList<>(); + ibmJdkLookupDirs.add(home.resolve(isJre ? "bin" : "jre/bin")); + try (DirectoryStream stream = Files.newDirectoryStream(home.resolve(isJre ? "lib" : "jre/lib"), Files::isDirectory)) { + for (Path path : stream) ibmJdkLookupDirs.add(path); + } + catch (IOException ignored) { } for (Path candidate : ibmJdkLookupDirs) { - Path[] vmJarDirs = - listFiles(candidate.resolve("default"), f -> f.getFileName().toString().startsWith("jclSC") && Files.isDirectory(f)); - for (Path dir : vmJarDirs) { - Path vmJar = dir.resolve("vm.jar"); - if (Files.isRegularFile(vmJar)) { - rootFiles.add(vmJar); + try (DirectoryStream stream = Files.newDirectoryStream(candidate, p -> p.getFileName().toString().startsWith("jclSC") && Files.isDirectory(p))) { + for (Path dir : stream) { + Path vmJar = dir.resolve("vm.jar"); + if (Files.isRegularFile(vmJar)) { + rootFiles.add(vmJar); + } } } + catch (IOException ignored) { } } Path classesZip = home.resolve("lib/classes.zip"); @@ -113,15 +117,6 @@ public final class JavaSdkUtil { return rootFiles; } - private static Path[] listFiles(Path dir, Predicate filter) { - try { - return Files.list(dir).filter(filter).toArray(Path[]::new); - } - catch (IOException e) { - return EMPTY_PATH_ARRAY; - } - } - @Nullable private static String getCanonicalPath(Path file) { try { @@ -131,4 +126,4 @@ public final class JavaSdkUtil { return null; } } -} \ No newline at end of file +} diff --git a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JpsJavaExtensionServiceImpl.java b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JpsJavaExtensionServiceImpl.java index ec18d49ae803..25ed60df434e 100644 --- a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JpsJavaExtensionServiceImpl.java +++ b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JpsJavaExtensionServiceImpl.java @@ -1,9 +1,6 @@ -/* - * Copyright 2000-2017 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. - */ +// 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.io.FileUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jps.model.JpsDummyElement; @@ -132,8 +129,7 @@ public class JpsJavaExtensionServiceImpl extends JpsJavaExtensionService { assert jdkInfo != null : homePath; String version = JdkVersionDetector.formatVersionString(jdkInfo.version); JpsTypedLibrary> sdk = global.addSdk(name, homePath, version, JpsJavaSdkType.INSTANCE); - Path homeDir = Paths.get(FileUtil.toSystemDependentName(homePath)); - List roots = JavaSdkUtil.getJdkClassesRoots(homeDir, false); + List roots = JavaSdkUtil.getJdkClassesRoots(Paths.get(homePath), false); for (Path root : roots) { sdk.addRoot(root.toFile(), JpsOrderRootType.COMPILED); } @@ -233,4 +229,4 @@ public class JpsJavaExtensionServiceImpl extends JpsJavaExtensionService { public JavaModuleIndex getJavaModuleIndex(@NotNull JpsProject project) { return project.getContainer().getOrSetChild(JavaModuleIndexRole.INSTANCE, () -> getCompilerConfiguration(project).getCompilerExcludes()); } -} \ No newline at end of file +} diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java index d9b47386b4af..5191eb981268 100644 --- a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// 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 com.intellij.openapi.projectRoots; import com.intellij.execution.CantRunException; @@ -24,7 +24,6 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; import java.util.Objects; import java.util.jar.Attributes; @@ -93,43 +92,36 @@ public final class JdkUtil { } public static boolean checkForJdk(@NotNull String homePath) { - return checkForJdk(Paths.get(homePath)); + return checkForJdk(Path.of(homePath)); } public static boolean checkForJdk(@NotNull Path homePath) { - Path bin = homePath.resolve("bin"); - return (Files.exists(bin.resolve("javac")) || Files.exists(bin.resolve("javac.exe"))) && - checkForRuntime(homePath.toAbsolutePath().toString()); + return (Files.exists(homePath.resolve("bin/javac")) || Files.exists(homePath.resolve("bin/javac.exe"))) && + (isModularRuntime(homePath) || // Jigsaw JDK/JRE + Files.exists(homePath.resolve("jre/lib/rt.jar")) || // pre-modular JDK + Files.isDirectory(homePath.resolve("classes")) || // custom build + Files.exists(homePath.resolve("jre/lib/vm.jar")) || // IBM JDK + Files.exists(homePath.resolve("../Classes/classes.jar"))); // Apple JDK } public static boolean checkForJre(@NotNull String homePath) { - return checkForJre(Paths.get(homePath)); + return checkForJre(Path.of(homePath)); } public static boolean checkForJre(@NotNull Path homePath) { - Path bin = homePath.resolve("bin"); - return Files.exists(bin.resolve("java")) || Files.exists(bin.resolve("java.exe")); - } - - public static boolean checkForRuntime(@NotNull String homePath) { - return new File(homePath, "jre/lib/rt.jar").exists() || // JDK - new File(homePath, "lib/rt.jar").exists() || // JRE - isModularRuntime(homePath) || // Jigsaw JDK/JRE - new File(homePath, "../Classes/classes.jar").exists() || // Apple JDK - new File(homePath, "jre/lib/vm.jar").exists() || // IBM JDK - new File(homePath, "classes").isDirectory(); // custom build + return Files.exists(homePath.resolve("bin/java")) || Files.exists(homePath.resolve("bin/java.exe")); } public static boolean isModularRuntime(@NotNull String homePath) { - return isModularRuntime(Paths.get(FileUtil.toSystemDependentName(homePath))); + return isModularRuntime(Path.of(homePath)); } public static boolean isModularRuntime(@NotNull Path homePath) { - return Files.isRegularFile(homePath.resolve("lib/jrt-fs.jar")) || isExplodedModularRuntime(homePath.toString()); + return Files.isRegularFile(homePath.resolve("lib/jrt-fs.jar")) || isExplodedModularRuntime(homePath); } - public static boolean isExplodedModularRuntime(@NotNull String homePath) { - return Files.isDirectory(Paths.get(homePath, "modules/java.base")); + public static boolean isExplodedModularRuntime(@NotNull Path homePath) { + return Files.isDirectory(homePath.resolve("modules/java.base")); } @ApiStatus.Internal @@ -205,4 +197,4 @@ public final class JdkUtil { commandLine.getEnvironment().putAll(generalCommandLine.getEnvironment()); } // -} \ No newline at end of file +}