Cleanup (IDEA-CR-70012)

(closing directory streams; dropping unnecessary conversions; migrating more methods to NIO2)

GitOrigin-RevId: 9d9f3cc4756aaca97d2a7a9618876f5093a10d58
This commit is contained in:
Roman Shevchenko
2021-01-08 12:33:18 +00:00
committed by intellij-monorepo-bot
parent 90fbe7953d
commit ea445ba366
6 changed files with 76 additions and 99 deletions
@@ -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<String> 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<String> findClasses(@NotNull Path jdkHome, boolean isJre) {
List<String> 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<Path> 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<String> 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();
}
}
}
@@ -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();
}
}
}
@@ -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<OrderRootType, VirtualFile> 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);
}
}
}
}
@@ -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<Path> jarFileFilter = path -> FileUtilRt.extensionEquals(path.toString(), "jar");
Set<String> pathFilter = CollectionFactory.createFilePathSet();
List<Path> 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<Path> 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<Path> ibmJdkLookupDirs = ContainerUtil.newArrayList(home.resolve(isJre ? "bin" : "jre/bin"));
ContainerUtil.addAll(ibmJdkLookupDirs, listFiles(home.resolve(isJre ? "lib" : "jre/lib"), path -> Files.isDirectory(path)));
List<Path> ibmJdkLookupDirs = new ArrayList<>();
ibmJdkLookupDirs.add(home.resolve(isJre ? "bin" : "jre/bin"));
try (DirectoryStream<Path> 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<Path> 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<Path> 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;
}
}
}
}
@@ -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<JpsSdk<JpsDummyElement>> sdk = global.addSdk(name, homePath, version, JpsJavaSdkType.INSTANCE);
Path homeDir = Paths.get(FileUtil.toSystemDependentName(homePath));
List<Path> roots = JavaSdkUtil.getJdkClassesRoots(homeDir, false);
List<Path> 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());
}
}
}
@@ -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());
}
//</editor-fold>
}
}