Bug 545086 - [12] Refactor to reuse all code to remove '/' from path

Change-Id: I81a2a21f4c342ecee730b8016fabaab9d2bd8587
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java
index 6399b84..75bad93 100644
--- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java
+++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java
@@ -31,7 +31,6 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
 import java.util.ServiceLoader;
diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java
index c54b616..a34fbbf 100644
--- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java
+++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/util/JrtFileSystem.java
@@ -92,8 +92,8 @@
 			}
 
 			@Override
-			public FileVisitResult visitModule(Path mod) throws IOException {
-				JrtFileSystem.this.modulePathMap.put(mod.getFileName().toString(), mod);
+			public FileVisitResult visitModule(Path path, String name) throws IOException {
+				JrtFileSystem.this.modulePathMap.put(name, path);
 				return FileVisitResult.CONTINUE;
 			}
 		}, JRTUtil.NOTIFY_MODULES);
diff --git a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java
index 07cadc2..a140e15 100644
--- a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java
+++ b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/JrtFileSystem.java
@@ -92,8 +92,8 @@
 			}
 
 			@Override
-			public FileVisitResult visitModule(Path mod) throws IOException {
-				JrtFileSystem.this.modulePathMap.put(mod.getFileName().toString(), mod);
+			public FileVisitResult visitModule(Path path, String name) throws IOException {
+				JrtFileSystem.this.modulePathMap.put(name, path);
 				return FileVisitResult.CONTINUE;
 			}
 		}, JRTUtil.NOTIFY_MODULES);
diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247.java b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247.java
index 408f946..8dfa35f 100644
--- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247.java
+++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247.java
@@ -164,7 +164,7 @@
 									if (content == null)
 										return FileVisitResult.CONTINUE;
 									ClasspathJep247.this.acceptModule(content);
-									ClasspathJep247.this.moduleNamesCache.add(f.getFileName().toString());
+									ClasspathJep247.this.moduleNamesCache.add(JRTUtil.sanitizedFileName(f));
 								}
 								return FileVisitResult.CONTINUE;
 							}
@@ -221,7 +221,7 @@
 		List<String> sub = new ArrayList<>();
 		try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(this.releasePath)) {
 			for (final java.nio.file.Path subdir: stream) {
-				String rel = subdir.getFileName().toString();
+				String rel = JRTUtil.sanitizedFileName(subdir);
 				if (rel.contains(this.releaseInHex)) {
 					sub.add(rel);
 				} else {
diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247Jdk12.java b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247Jdk12.java
index 4ae242d..917fde6 100644
--- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247Jdk12.java
+++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJep247Jdk12.java
@@ -74,7 +74,7 @@
 						Path p = this.fs.getPath(rel);
 						try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(p)) {
 							for (final java.nio.file.Path subdir: stream) {
-								Path f = this.fs.getPath(rel, subdir.getFileName().toString(), qualifiedBinaryFileName);
+								Path f = this.fs.getPath(rel, JRTUtil.sanitizedFileName(subdir), qualifiedBinaryFileName);
 								if (Files.exists(f)) {
 									content = JRTUtil.safeReadBytes(f);
 									if (content != null)
@@ -138,7 +138,7 @@
 		List<String> sub = new ArrayList<>();
 		try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(this.releasePath)) {
 			for (final java.nio.file.Path subdir: stream) {
-				String rel = subdir.getFileName().toString();
+				String rel = JRTUtil.sanitizedFileName(subdir);
 				if (rel.contains(this.releaseInHex))
 					sub.add(rel);
 			}
@@ -161,7 +161,7 @@
 		if (this.modules == null) {
 			try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(this.releasePath)) {
 				for (final java.nio.file.Path subdir: stream) {
-					String rel = subdir.getFileName().toString();
+					String rel = JRTUtil.sanitizedFileName(subdir);
 					if (!rel.contains(this.releaseInHex)) {
 						continue;
 					}
@@ -183,8 +183,9 @@
 								if (content == null)
 									return FileVisitResult.CONTINUE;
 								Path m = f.subpath(1, f.getNameCount() - 1);
-								ClasspathJep247Jdk12.this.acceptModule(m.getFileName().toString(), content);
-								ClasspathJep247Jdk12.this.moduleNamesCache.add(m.getFileName().toString());
+								String name = JRTUtil.sanitizedFileName(m);
+								ClasspathJep247Jdk12.this.acceptModule(name, content);
+								ClasspathJep247Jdk12.this.moduleNamesCache.add(name);
 							}
 							return FileVisitResult.SKIP_SIBLINGS;
 						}
@@ -264,7 +265,7 @@
 		this.packageCache.add(Util.EMPTY_STRING);
 		try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(this.releasePath)) {
 			for (final java.nio.file.Path subdir: stream) {
-				String rel = subdir.getFileName().toString();
+				String rel = JRTUtil.sanitizedFileName(subdir);
 				if (!rel.contains(this.releaseInHex)) {
 					continue;
 				}
diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJrt.java b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJrt.java
index e76860d..117ba47 100644
--- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJrt.java
+++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathJrt.java
@@ -156,10 +156,10 @@
 				}
 
 				@Override
-				public FileVisitResult visitModule(java.nio.file.Path modPath) throws IOException {
+				public FileVisitResult visitModule(Path p, String name) throws IOException {
 					if (moduleName == null)
 						return FileVisitResult.CONTINUE;
-					if (!moduleName.equals(modPath.toString())) {
+					if (!moduleName.equals(name)) {
 						return FileVisitResult.SKIP_SUBTREE;
 					}
 					return FileVisitResult.CONTINUE;
@@ -219,10 +219,10 @@
 					}
 
 					@Override
-					public FileVisitResult visitModule(Path mod) throws IOException {
+					public FileVisitResult visitModule(Path p, String name) throws IOException {
 						try {
-							ClasspathJrt.this.acceptModule(JRTUtil.getClassfileContent(ClasspathJrt.this.file, IModule.MODULE_INFO_CLASS, mod.toString()));
-							ClasspathJrt.this.moduleNamesCache.add(mod.getFileName().toString());
+							ClasspathJrt.this.acceptModule(JRTUtil.getClassfileContent(ClasspathJrt.this.file, IModule.MODULE_INFO_CLASS, name));
+							ClasspathJrt.this.moduleNamesCache.add(name);
 						} catch (ClassFormatException e) {
 							e.printStackTrace();
 						}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/JRTUtil.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/JRTUtil.java
index ae56b65..5d9b78b 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/JRTUtil.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/JRTUtil.java
@@ -79,7 +79,7 @@
 		 * how the JRT needs to be processed, for e.g., clients can skip a particular module
 		 * by returning FileVisitResult.SKIP_SUBTREE
 		 */
-		public FileVisitResult visitModule(T mod) throws IOException;
+		public FileVisitResult visitModule(T path, String name) throws IOException;
 	}
 
 	static abstract class AbstractFileVisitor<T> implements FileVisitor<T> {
@@ -184,6 +184,16 @@
 	public static boolean hasCompilationUnit(File jrt, String qualifiedPackageName, String moduleName) {
 		return getJrtSystem(jrt).hasClassFile(qualifiedPackageName, moduleName);
 	}
+	/*
+	 * Returns only the file name after removing trailing '/' if any for folders
+	 */
+	public static String sanitizedFileName(Path path) {
+		String p = path.getFileName().toString();
+		if (p.length() > 1 && p.charAt(p.length() - 1) == '/') {
+			return p.substring(0, p.length() - 1);
+		}
+		return p;
+	}
 	/**
 	 * Tries to read all bytes of the file denoted by path,
 	 * returns null if the file could not be found or if the read was interrupted.
@@ -257,7 +267,7 @@
 			List<String> sub = new ArrayList<>();
 			try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(releasePath)) {
 				for (final java.nio.file.Path subdir: stream) {
-					String r = subdir.getFileName().toString();
+					String r = JRTUtil.sanitizedFileName(subdir);
 					if (r.contains(this.releaseInHex)) {
 						sub.add(r);
 					} else {
@@ -293,7 +303,7 @@
 								return FileVisitResult.SKIP_SUBTREE;
 							}
 							return ((notify & JRTUtil.NOTIFY_MODULES) == 0) ? FileVisitResult.CONTINUE
-									: visitor.visitModule(dir);
+									: visitor.visitModule(dir, JRTUtil.sanitizedFileName(mod));
 						}
 						if ((notify & JRTUtil.NOTIFY_PACKAGES) == 0) {
 							// client is not interested in packages
@@ -467,7 +477,7 @@
 	byte[] getClassfileContent(String fileName, String module) throws IOException, ClassFormatException {
 		byte[] content = null;
 		if (module != null) {
-			content = getClassfileBytes(fileName, new String(module.toCharArray()));
+			content = getClassfileBytes(fileName, module);
 		} else {
 			String[] modules = getModules(fileName);
 			for (String mod : modules) {
@@ -544,7 +554,7 @@
 						return FileVisitResult.SKIP_SUBTREE;
 					}
 					return ((notify & JRTUtil.NOTIFY_MODULES) == 0) ? 
-							FileVisitResult.CONTINUE : visitor.visitModule(mod);
+							FileVisitResult.CONTINUE : visitor.visitModule(dir, JRTUtil.sanitizedFileName(mod));
 				}
 				if ((notify & JRTUtil.NOTIFY_PACKAGES) == 0) {
 					// We are dealing with a module or not client is not interested in packages
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
index 301df83..fdb13ad 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
@@ -935,8 +935,8 @@
 				}
 
 				@Override
-				public FileVisitResult visitModule(java.nio.file.Path mod) throws IOException {
-					JrtPackageFragmentRoot root = new JrtPackageFragmentRoot(imagePath, mod.toString(), JavaProject.this);
+				public FileVisitResult visitModule(java.nio.file.Path path, String name) throws IOException {
+					JrtPackageFragmentRoot root = new JrtPackageFragmentRoot(imagePath, name, JavaProject.this);
 					roots.add(root);
 					if (rootToResolvedEntries != null) 
 						rootToResolvedEntries.put(root, ((ClasspathEntry)resolvedEntry).combineWith((ClasspathEntry) referringEntry));
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
index cd3e815..dba3e1a 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
@@ -78,8 +78,8 @@
 				}
 
 				@Override
-				public FileVisitResult visitModule(Path mod) throws IOException {
-					if (!JrtPackageFragmentRoot.this.moduleName.equals(mod.toString())) {
+				public FileVisitResult visitModule(Path path, String name) throws IOException {
+					if (!JrtPackageFragmentRoot.this.moduleName.equals(name)) {
 						return FileVisitResult.SKIP_SUBTREE;
 					}
 					return FileVisitResult.CONTINUE;
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceMapper.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceMapper.java
index 64cf749..4c99d4a 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceMapper.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceMapper.java
@@ -530,7 +530,7 @@
 		}
 
 		@Override
-		public FileVisitResult visitModule(java.nio.file.Path mod) throws IOException {
+		public FileVisitResult visitModule(java.nio.file.Path path, String name) throws IOException {
 			return FileVisitResult.CONTINUE;
 		}
 	}
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
index c9a57cd..9576ce7 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
@@ -101,8 +101,7 @@
 			}
 
 			@Override
-			public FileVisitResult visitModule(Path mod) throws IOException {
-				String name = mod.toString();
+			public FileVisitResult visitModule(Path path, String name) throws IOException {
 				try {
 					jrt.acceptModule(JRTUtil.getClassfileContent(imageFile, IModule.MODULE_INFO_CLASS, name));
 				} catch (ClassFormatException e) {
@@ -144,9 +143,9 @@
 				}
 
 				@Override
-				public FileVisitResult visitModule(Path mod) throws IOException {
+				public FileVisitResult visitModule(Path path, String name) throws IOException {
 					try {
-						jrt.acceptModule(JRTUtil.getClassfileContent(imageFile, IModule.MODULE_INFO_CLASS, mod.toString()));
+						jrt.acceptModule(JRTUtil.getClassfileContent(imageFile, IModule.MODULE_INFO_CLASS, name));
 					} catch (ClassFormatException e) {
 						e.printStackTrace();
 					}
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 fdef682..9a38839 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
@@ -96,7 +96,7 @@
 	private boolean isJRE12Plus(Path path) {
 		try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(path)) {
 			for (final java.nio.file.Path subdir : stream) {
-				String rel = subdir.getFileName().toString();
+				String rel = JRTUtil.sanitizedFileName(subdir);
 				if (Files.exists(this.fs.getPath(rel, "system-modules"))) { //$NON-NLS-1$
 					int parseInt = Integer.parseInt(rel, 16);
 					return (parseInt > 11);
@@ -158,7 +158,7 @@
 			List<String> sub = new ArrayList<>();
 			try (DirectoryStream<java.nio.file.Path> stream = Files.newDirectoryStream(releasePath)) {
 				for (final java.nio.file.Path subdir : stream) {
-					String rel = subdir.getFileName().toString();
+					String rel = JRTUtil.sanitizedFileName(subdir);
 					if (rel.contains(this.releaseInHex)) {
 						sub.add(rel);
 					} else {
@@ -205,8 +205,7 @@
 						}
 
 						@Override
-						public FileVisitResult visitModule(Path mod) throws IOException {
-							String name = mod.getName(1).toString();
+						public FileVisitResult visitModule(Path path, String name) throws IOException {
 							this.packageSet = new SimpleSet(41);
 							this.packageSet.add(""); //$NON-NLS-1$
 							packagesInModule.put(name, this.packageSet);
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/AddJrtToIndex.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/AddJrtToIndex.java
index df04833..baa8c67 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/AddJrtToIndex.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/AddJrtToIndex.java
@@ -98,7 +98,7 @@
 		@Override
 		public FileVisitResult visitFile(java.nio.file.Path path, java.nio.file.Path mod, BasicFileAttributes attrs)
 				throws IOException {
-			String name = path.getFileName().toString();
+			String name = JRTUtil.sanitizedFileName(path);
 			if (Util.isClassFileName(name) && 
 					isValidPackageNameForClassOrisModule(name)) {
 				this.indexedFileNames.put(name, FILE_INDEX_STATE.EXISTS);
@@ -106,7 +106,7 @@
 			return FileVisitResult.CONTINUE;
 		}
 		@Override
-		public FileVisitResult visitModule(java.nio.file.Path mod) throws IOException {
+		public FileVisitResult visitModule(java.nio.file.Path path, String name) throws IOException {
 			return FileVisitResult.CONTINUE;
 		}
 	}
@@ -132,7 +132,7 @@
 		@Override
 		public FileVisitResult visitFile(java.nio.file.Path path, java.nio.file.Path mod, BasicFileAttributes attrs)
 				throws IOException {
-			String name = path.getFileName().toString();
+			String name = JRTUtil.sanitizedFileName(path);
 			if (Util.isClassFileName(name) && 
 					isValidPackageNameForClassOrisModule(name)) {
 				try {