Skip to content

Commit c742cff

Browse files
authored
Merge pull request scala#7782 from retronym/topic/nio-file-tooling
Publicise PlainNioFile and implement underlyingSource
2 parents d645559 + 16a3216 commit c742cff

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/reflect/scala/reflect/io/PlainFile.scala

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class PlainFile(val givenPath: Path) extends AbstractFile {
101101
new PlainFile(givenPath / name)
102102
}
103103

104-
private[scala] class PlainNioFile(nioPath: java.nio.file.Path) extends AbstractFile {
104+
final class PlainNioFile(val nioPath: java.nio.file.Path) extends AbstractFile {
105105
import java.nio.file._
106106

107107
assert(nioPath ne null)
@@ -115,7 +115,29 @@ private[scala] class PlainNioFile(nioPath: java.nio.file.Path) extends AbstractF
115115

116116
override lazy val canonicalPath = super.canonicalPath
117117

118-
override def underlyingSource = Some(this)
118+
override def underlyingSource = {
119+
val fileSystem = nioPath.getFileSystem
120+
fileSystem.provider().getScheme match {
121+
case "jar" =>
122+
val fileStores = fileSystem.getFileStores.iterator()
123+
if (fileStores.hasNext) {
124+
val jarPath = fileStores.next().name
125+
try {
126+
Some(new PlainNioFile(Paths.get(jarPath.stripSuffix(fileSystem.getSeparator))))
127+
} catch {
128+
case _: InvalidPathException =>
129+
None
130+
}
131+
} else None
132+
case "jrt" =>
133+
if (nioPath.getNameCount > 2 && nioPath.startsWith("/modules")) {
134+
// TODO limit this to OpenJDK based JVMs?
135+
val moduleName = nioPath.getName(1)
136+
Some(new PlainNioFile(Paths.get(System.getProperty("java.home"), "jmods", moduleName.toString + ".jmod")))
137+
} else None
138+
case _ => None
139+
}
140+
}
119141

120142
private val fpath = nioPath.toAbsolutePath.toString
121143

0 commit comments

Comments
 (0)