diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java index bce7b07c56a..c475f2946aa 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -42,7 +43,7 @@ public class JrtFileSystem extends Archive { - public HashMap modulePathMap; + Map modulePathMap; Path modules; private java.nio.file.FileSystem jrtfs; @@ -55,7 +56,7 @@ public void initialize() throws IOException { // initialize packages this.modulePathMap = new HashMap<>(); if (this.file.exists()) { - this.jrtfs = JRTUtil.getJrtFileSystem(this.file.getAbsolutePath()); + this.jrtfs = JRTUtil.getJrtFileSystem(this.file.toPath()); this.modules = this.jrtfs.getPath(JRTUtil.MODULES_SUBDIR); } else { return; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java index 40d26cb53fd..e751a4519e6 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java @@ -164,8 +164,7 @@ class SystemLocationContainer extends LocationContainer { public SystemLocationContainer(Location loc, JrtFileSystem jrt) throws IOException { super(loc); jrt.initialize(); - HashMap modulePathMap = jrt.modulePathMap; - Set keySet = modulePathMap.keySet(); + Set keySet = jrt.modulePathMap.keySet(); for (String mod : keySet) { Path path = jrt.file.toPath(); ModuleLocationWrapper wrapper = new ModuleLocationWrapper(loc, mod, false, diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/JRTUtil.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/JRTUtil.java index b7f06d29875..9a9d95c51b6 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/JRTUtil.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/JRTUtil.java @@ -17,7 +17,6 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -28,12 +27,11 @@ import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; -import java.nio.file.Paths; import java.nio.file.ProviderNotFoundException; +import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collections; @@ -54,7 +52,7 @@ public class JRTUtil { public static final boolean DISABLE_CACHE = Boolean.getBoolean("org.eclipse.jdt.disable_JRT_cache"); //$NON-NLS-1$ public static final boolean PROPAGATE_IO_ERRORS = Boolean.getBoolean("org.eclipse.jdt.propagate_io_errors"); //$NON-NLS-1$ - public static final String JAVA_BASE = "java.base".intern(); //$NON-NLS-1$ + public static final String JAVA_BASE = "java.base"; //$NON-NLS-1$ public static final char[] JAVA_BASE_CHAR = JAVA_BASE.toCharArray(); public static final String MODULES_SUBDIR = "/modules"; //$NON-NLS-1$ static final String[] DEFAULT_MODULE = new String[]{JAVA_BASE}; @@ -71,12 +69,11 @@ public class JRTUtil { // TODO: Java 9 Think about clearing the cache too. private static Map images = new ConcurrentHashMap<>(); - /** * Map from JDK home path to ct.sym file (located in /lib in the JDK) */ private static final Map ctSymFiles = new ConcurrentHashMap<>(); - private static final Map JRT_FILE_SYSTEMS = new ConcurrentHashMap<>(); + private static final Map JRT_FILE_SYSTEMS = new ConcurrentHashMap<>(); static final SoftClassCache classCache = new SoftClassCache(); @@ -101,28 +98,6 @@ public default FileVisitResult visitModule(T path, String name) throws IOExcepti } } - public static abstract class AbstractFileVisitor implements FileVisitor { - @Override - public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(T file, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - } - /** * @param image the path to the root of the JRE whose libraries we are interested in. * @return may return {@code null} @@ -145,10 +120,9 @@ public static JrtFileSystem getJrtSystem(File image) { */ public static JrtFileSystem getJrtSystem(File image, String release) throws IOException { Jdk jdk = new Jdk(image); - String key = jdk.path; - + String key = jdk.path.toString(); if (release != null && !jdk.sameRelease(release)) { - key = key + "|" + release; //$NON-NLS-1$ + key += "|" + release; //$NON-NLS-1$ } try { JrtFileSystem system = images.computeIfAbsent(key, x -> { @@ -163,7 +137,7 @@ public static JrtFileSystem getJrtSystem(File image, String release) throws IOEx }); return system; } catch (RuntimeIOException e) { - throw e.getCause(); + throw e.getCause(); } } @@ -174,11 +148,11 @@ public static JrtFileSystem getJrtSystem(File image, String release) throws IOEx * @throws IOException * on any error */ - public static FileSystem getJrtFileSystem(String path) throws IOException { + public static FileSystem getJrtFileSystem(Path path) throws IOException { try { - FileSystem fs = JRT_FILE_SYSTEMS.computeIfAbsent(path, p -> { + FileSystem fs = JRT_FILE_SYSTEMS.computeIfAbsent(path.toRealPath(), p -> { try { - return FileSystems.newFileSystem(JRTUtil.JRT_URI, Map.of("java.home", p)); //$NON-NLS-1$ + return FileSystems.newFileSystem(JRTUtil.JRT_URI, Map.of("java.home", p.toString())); //$NON-NLS-1$ } catch (IOException e) { throw new RuntimeIOException(e); } @@ -222,7 +196,7 @@ public static FileSystem getJarFileSystem(Path path) throws IOException { public static CtSym getCtSym(Path jdkHome) throws IOException { CtSym ctSym; try { - ctSym = ctSymFiles.compute(jdkHome, (Path x, CtSym current) -> { + ctSym = ctSymFiles.compute(jdkHome.toRealPath(), (Path x, CtSym current) -> { if (current == null || !current.getFs().isOpen()) { try { return new CtSym(x); @@ -261,7 +235,7 @@ public static void reset() { * @param visitor an instance of JrtFileVisitor to be notified of the entries in the JRT image. * @param notify flag indicating the notifications the client is interested in. */ - public static void walkModuleImage(File image, final JRTUtil.JrtFileVisitor visitor, int notify) throws IOException { + public static void walkModuleImage(File image, final JRTUtil.JrtFileVisitor visitor, int notify) throws IOException { JrtFileSystem system = getJrtSystem(image, null); if (system == null) { return; @@ -269,7 +243,7 @@ public static void walkModuleImage(File image, final JRTUtil.JrtFileVisitor visitor, int notify) throws IOException { + public static void walkModuleImage(File image, String release, final JRTUtil.JrtFileVisitor visitor, int notify) throws IOException { JrtFileSystem system = getJrtSystem(image, release); if (system == null) { return; @@ -386,7 +360,7 @@ class JrtFileSystemWithOlderRelease extends JrtFileSystem { JrtFileSystemWithOlderRelease(Jdk jdkHome, String release) throws IOException { super(jdkHome, release); String releaseCode = CtSym.getReleaseCode(this.release); - this.ctSym = JRTUtil.getCtSym(Paths.get(this.jdk.path)); + this.ctSym = JRTUtil.getCtSym(this.jdk.path); this.fs = this.ctSym.getFs(); if (!Files.exists(this.fs.getPath(releaseCode)) || Files.exists(this.fs.getPath(releaseCode, "system-modules"))) { //$NON-NLS-1$ @@ -396,11 +370,11 @@ class JrtFileSystemWithOlderRelease extends JrtFileSystem { } @Override - void walkModuleImage(final JRTUtil.JrtFileVisitor visitor, final int notify) throws IOException { + void walkModuleImage(final JRTUtil.JrtFileVisitor visitor, final int notify) throws IOException { for (Path p : this.releaseRoots) { - Files.walkFileTree(p, new JRTUtil.AbstractFileVisitor() { + Files.walkFileTree(p, new SimpleFileVisitor<>() { @Override - public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttributes attrs) + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { int count = dir.getNameCount(); if (count == 1) { @@ -408,7 +382,7 @@ public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttrib } if (count == 2) { // e.g. /9A/java.base - java.nio.file.Path mod = dir.getName(1); + Path mod = dir.getName(1); if ((JRTUtil.MODULE_TO_LOAD != null && JRTUtil.MODULE_TO_LOAD.length() > 0 && JRTUtil.MODULE_TO_LOAD.indexOf(mod.toString()) == -1)) { return FileVisitResult.SKIP_SUBTREE; @@ -424,7 +398,7 @@ public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttrib } @Override - public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if ((notify & JRTUtil.NOTIFY_FILES) == 0) { return FileVisitResult.CONTINUE; @@ -459,21 +433,20 @@ public synchronized IOException getCause() { } class Jdk { - final String path; + final Path path; final String release; - static final Map pathToRelease = new ConcurrentHashMap<>(); + private static final Map pathToRelease = new ConcurrentHashMap<>(); public Jdk(File jrt) throws IOException { this.path = toJdkHome(jrt); try { - String rel = pathToRelease.computeIfAbsent(this.path, key -> { + this.release = pathToRelease.computeIfAbsent(this.path.toRealPath(), p -> { try { - return readJdkReleaseFile(this.path); + return readJdkReleaseFile(p); } catch (IOException e) { throw new RuntimeIOException(e); } }); - this.release = rel; } catch (RuntimeIOException rio) { throw rio.getCause(); } @@ -502,21 +475,21 @@ boolean sameRelease(String other) { return Long.compare(jdkLevel, otherJdkLevel) == 0; } - static String toJdkHome(File jrt) { - String home; + static Path toJdkHome(File jrt) { + Path home; Path normalized = jrt.toPath().normalize(); if (jrt.getName().equals(JRTUtil.JRT_FS_JAR)) { - home = normalized.getParent().getParent().toString(); + home = normalized.getParent().getParent(); } else { - home = normalized.toString(); + home = normalized; } return home; } - static String readJdkReleaseFile(String javaHome) throws IOException { + static String readJdkReleaseFile(Path javaHome) throws IOException { Properties properties = new Properties(); - try(FileReader reader = new FileReader(new File(javaHome, "release"))){ //$NON-NLS-1$ - properties.load(reader); + try (InputStream in = Files.newInputStream(javaHome.resolve("release"))) { //$NON-NLS-1$ + properties.load(in); } // Something like JAVA_VERSION="1.8.0_05" String ver = properties.getProperty("JAVA_VERSION"); //$NON-NLS-1$ @@ -534,8 +507,8 @@ class JrtFileSystem { private final Map> packageToModules = new HashMap<>(); FileSystem fs; - Path modRoot; - Jdk jdk; + final Path modRoot; + final Jdk jdk; final String release; public static JrtFileSystem getNewJrtFileSystem(Jdk jdk, String release) throws IOException { @@ -718,16 +691,16 @@ public ClassFileReader getClassfile(String fileName, String module) throws IOExc } void walkJrtForModules() throws IOException { - Iterable roots = this.fs.getRootDirectories(); - for (java.nio.file.Path path : roots) { - try (DirectoryStream stream = Files.newDirectoryStream(path)) { - for (final java.nio.file.Path subdir: stream) { + Iterable roots = this.fs.getRootDirectories(); + for (Path path : roots) { + try (DirectoryStream stream = Files.newDirectoryStream(path)) { + for (final Path subdir: stream) { if (!subdir.toString().equals(JRTUtil.MODULES_SUBDIR)) { - Files.walkFileTree(subdir, new JRTUtil.AbstractFileVisitor() { + Files.walkFileTree(subdir, new SimpleFileVisitor<>() { @Override - public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { // e.g. /modules/java.base - java.nio.file.Path relative = subdir.relativize(file); + Path relative = subdir.relativize(file); cachePackage(relative.getParent().toString(), relative.getFileName().toString()); return FileVisitResult.CONTINUE; } @@ -740,15 +713,15 @@ public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes at } } - void walkModuleImage(final JRTUtil.JrtFileVisitor visitor, final int notify) throws IOException { - Files.walkFileTree(this.modRoot, new JRTUtil.AbstractFileVisitor() { + void walkModuleImage(final JRTUtil.JrtFileVisitor visitor, final int notify) throws IOException { + Files.walkFileTree(this.modRoot, new SimpleFileVisitor<>() { @Override - public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttributes attrs) throws IOException { + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { int count = dir.getNameCount(); if (count == 1) return FileVisitResult.CONTINUE; if (count == 2) { // e.g. /modules/java.base - java.nio.file.Path mod = dir.getName(1); + Path mod = dir.getName(1); if ((JRTUtil.MODULE_TO_LOAD != null && JRTUtil.MODULE_TO_LOAD.length() > 0 && JRTUtil.MODULE_TO_LOAD.indexOf(mod.toString()) == -1)) { return FileVisitResult.SKIP_SUBTREE; @@ -764,7 +737,7 @@ public FileVisitResult preVisitDirectory(java.nio.file.Path dir, BasicFileAttrib } @Override - public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if ((notify & JRTUtil.NOTIFY_FILES) == 0) return FileVisitResult.CONTINUE; int count = file.getNameCount(); diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/SoftClassCache.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/SoftClassCache.java index 770e1c314b6..073a4d2e065 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/SoftClassCache.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/SoftClassCache.java @@ -25,7 +25,7 @@ */ class SoftClassCache { - private final ConcurrentMap jdks = new ConcurrentHashMap<>(); + private final ConcurrentMap jdks = new ConcurrentHashMap<>(); void clear() { this.jdks.clear(); @@ -37,9 +37,9 @@ public byte[] getClassBytes(Jdk jdk, Path path) throws IOException { private static final class JdkClasses { private final ConcurrentMap classes = new ConcurrentHashMap<>(10007); - private final String jdkPath; + private final Path jdkPath; - public JdkClasses(String jdkPath) { + public JdkClasses(Path jdkPath) { this.jdkPath = jdkPath; } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrtWithReleaseOption.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrtWithReleaseOption.java index b2bfac7488b..e6bbd1b6b49 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrtWithReleaseOption.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrtWithReleaseOption.java @@ -18,7 +18,7 @@ import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Collection; import java.util.Collections; @@ -29,7 +29,6 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; @@ -74,10 +73,9 @@ public ClasspathJrtWithReleaseOption(String zipFilename, AccessRuleSet accessRul } this.release = getReleaseOptionFromCompliance(release); try { - this.ctSym = JRTUtil.getCtSym(Paths.get(this.zipFilename).getParent().getParent()); + this.ctSym = JRTUtil.getCtSym(Path.of(this.zipFilename).getParent().getParent()); } catch (IOException e) { - throw new CoreException(new Status(IStatus.ERROR, ClasspathJrtWithReleaseOption.class, - "Failed to init ct.sym for " + this.zipFilename, e)); //$NON-NLS-1$ + throw new CoreException(Status.error("Failed to init ct.sym for " + this.zipFilename, e)); //$NON-NLS-1$ } initialize(); loadModules(); @@ -98,8 +96,7 @@ private String getReleaseOptionFromCompliance(String comp) throws CoreException if (comp.indexOf('.') == -1) { return comp; } - throw new CoreException(new Status(IStatus.ERROR, ClasspathJrtWithReleaseOption.class, - "Invalid value for --release argument:" + comp)); //$NON-NLS-1$ + throw new CoreException(Status.error("Invalid value for --release argument:" + comp)); //$NON-NLS-1$ } } @@ -121,7 +118,7 @@ protected void initialize() throws CoreException { if (!Files.exists(this.releasePath.resolve(this.releaseCode))) { Exception e = new IllegalArgumentException("release " + this.release + " is not found in the system"); //$NON-NLS-1$//$NON-NLS-2$ - throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, e.getMessage(), e)); + throw new CoreException(Status.error(e.getMessage(), e)); } if (Files.exists(this.fs.getPath(this.releaseCode, "system-modules"))) { //$NON-NLS-1$ this.fs = null; // Fallback to default version, all classes are on jrt fs, not here. @@ -162,7 +159,7 @@ public void loadModules() { Map newCache = new HashMap<>(); for (Path root : releaseRoots) { try { - Files.walkFileTree(root, Collections.emptySet(), 2, new JRTUtil.AbstractFileVisitor() { + Files.walkFileTree(root, Collections.emptySet(), 2, new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path f, BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory() || f.getNameCount() < 3) {