Skip to content

Commit d49469d

Browse files
committed
Remove dotty.tools.io.Path.jfile
1 parent 7fefe01 commit d49469d

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

compiler/src/dotty/tools/io/AbstractFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object AbstractFile {
4242
*/
4343
def getDirectory(file: File): AbstractFile =
4444
if (file.isDirectory) new PlainFile(file)
45-
else if (file.isFile && Path.isExtensionJarOrZip(file.jfile)) ZipArchive fromFile file
45+
else if (file.isFile && Path.isExtensionJarOrZip(file.jpath)) ZipArchive fromFile file
4646
else null
4747

4848
/**

compiler/src/dotty/tools/io/File.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import java.io.{
1212
FileInputStream, FileOutputStream, BufferedWriter, OutputStreamWriter,
1313
BufferedOutputStream, IOException, PrintWriter
1414
}
15+
import java.nio.file.Files
16+
import java.nio.file.StandardOpenOption._
1517

1618
import scala.io.Codec
1719
/**
@@ -54,10 +56,10 @@ class File(jpath: JPath)(implicit constructorCodec: Codec) extends Path(jpath) w
5456
if (cond(this)) Iterator.single(this) else Iterator.empty
5557

5658
/** Obtains an InputStream. */
57-
def inputStream() = new FileInputStream(jfile)
59+
def inputStream() = Files.newInputStream(jpath)
5860

5961
/** Obtains a OutputStream. */
60-
def outputStream(append: Boolean = false) = new FileOutputStream(jfile, append)
62+
def outputStream(append: Boolean = false) = Files.newOutputStream(jpath, APPEND)
6163
def bufferedOutput(append: Boolean = false) = new BufferedOutputStream(outputStream(append))
6264

6365
/** Obtains an OutputStreamWriter wrapped around a FileOutputStream.
@@ -108,7 +110,7 @@ class File(jpath: JPath)(implicit constructorCodec: Codec) extends Path(jpath) w
108110
try classOf[JFile].getMethod("setExecutable", classOf[Boolean], classOf[Boolean])
109111
catch { case _: NoSuchMethodException => return false }
110112

111-
try method.invoke(jfile, executable: JBoolean, ownerOnly: JBoolean).asInstanceOf[JBoolean].booleanValue
113+
try method.invoke(jpath.toFile, executable: JBoolean, ownerOnly: JBoolean).asInstanceOf[JBoolean].booleanValue
112114
catch { case _: Exception => false }
113115
}
114116
}

compiler/src/dotty/tools/io/Jar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Jar(file: File) extends Iterable[JarEntry] {
4242

4343
private implicit def enrichManifest(m: JManifest): Jar.WManifest = Jar.WManifest(m)
4444

45-
lazy val jarFile = new JarFile(file.jfile)
45+
lazy val jarFile = new JarFile(file.jpath.toFile)
4646
lazy val manifest = withJarInput(s => Option(s.getManifest))
4747

4848
def mainClass = manifest map (f => f(Name.MAIN_CLASS))

compiler/src/dotty/tools/io/Path.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import scala.util.Random.alphanumeric
2929
* ''Note: This library is considered experimental and should not be used unless you know what you are doing.''
3030
*/
3131
object Path {
32-
def isExtensionJarOrZip(jfile: JFile): Boolean = isExtensionJarOrZip(jfile.getName)
32+
def isExtensionJarOrZip(jpath: JPath): Boolean = isExtensionJarOrZip(jpath.getFileName.toString)
3333
def isExtensionJarOrZip(name: String): Boolean = {
3434
val ext = extension(name)
3535
ext == "jar" || ext == "zip"
@@ -79,7 +79,7 @@ class Path private[io] (val jpath: JPath) {
7979
def toFile: File = new File(jpath)
8080
def toDirectory: Directory = new Directory(jpath)
8181
def toAbsolute: Path = if (isAbsolute) this else new Path(jpath.toAbsolutePath)
82-
def toCanonical: Path = Path(jfile.getCanonicalPath())
82+
def toCanonical: Path = normalize.toAbsolute
8383
def toURI: URI = jpath.toUri()
8484
def toURL: URL = toURI.toURL()
8585

@@ -112,12 +112,10 @@ class Path private[io] (val jpath: JPath) {
112112
*/
113113
def walk: Iterator[Path] = walkFilter(_ => true)
114114

115-
def jfile: JFile = jpath.toFile
116-
117115
// identity
118116
def name: String = jpath.getFileName().toString
119117
def path: String = jpath.toString
120-
def normalize: Path = Path(jfile.getAbsolutePath())
118+
def normalize: Path = Path(jpath.normalize)
121119

122120
def resolve(other: Path) = if (other.isAbsolute || isEmpty) other else /(other)
123121
def relativize(other: Path) = {
@@ -213,7 +211,7 @@ class Path private[io] (val jpath: JPath) {
213211
}
214212
def createFile(failIfExists: Boolean = false): File = {
215213
val res = tryCreate(Files.createFile(jpath))
216-
jfile.createNewFile()
214+
Files.createFile(jpath)
217215
if (!res && failIfExists && exists) fail("File '%s' already exists." format name)
218216
else if (isFile) toFile
219217
else new File(jpath)
@@ -241,7 +239,7 @@ class Path private[io] (val jpath: JPath) {
241239

242240
def truncate() =
243241
isFile && {
244-
val raf = new RandomAccessFile(jfile, "rw")
242+
val raf = new RandomAccessFile(jpath.toFile, "rw")
245243
raf setLength 0
246244
raf.close()
247245
length == 0

compiler/src/dotty/tools/io/ZipArchive.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ object ZipArchive {
3131
* @param file a File
3232
* @return A ZipArchive if `file` is a readable zip file, otherwise null.
3333
*/
34-
def fromFile(file: File): FileZipArchive = fromFile(file.jfile)
35-
def fromFile(file: JFile): FileZipArchive =
36-
try { new FileZipArchive(file.toPath) }
34+
def fromFile(file: File): FileZipArchive = fromPath(file.jpath)
35+
def fromPath(jpath: JPath): FileZipArchive =
36+
try { new FileZipArchive(jpath) }
3737
catch { case _: IOException => null }
3838

3939
def fromManifestURL(url: URL): AbstractFile = new ManifestResources(url)

compiler/test/dotty/tools/dotc/CompilerTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ abstract class CompilerTest {
121121
def compileFiles(path: String, args: List[String] = Nil, verbose: Boolean = true, runTest: Boolean = false,
122122
compileSubDirs: Boolean = true)(implicit defaultOptions: List[String]): Unit = {
123123
val dir = Directory(path)
124-
val fileNames = dir.files.toArray.map(_.jfile.getName).filter(name => (name endsWith ".scala") || (name endsWith ".java"))
124+
val fileNames = dir.files.toArray.map(_.name).filter(name => (name endsWith ".scala") || (name endsWith ".java"))
125125
for (name <- fileNames) {
126126
if (verbose) log(s"testing $path$name")
127127
compileFile(path, name, args, "", runTest)
128128
}
129129
if (compileSubDirs)
130130
for (subdir <- dir.dirs) {
131131
if (verbose) log(s"testing $subdir")
132-
compileDir(path, subdir.jfile.getName, args, runTest)
132+
compileDir(path, subdir.name, args, runTest)
133133
}
134134
}
135135
def runFiles(path: String, args: List[String] = Nil, verbose: Boolean = true)
@@ -363,13 +363,13 @@ abstract class CompilerTest {
363363

364364
processFileDir(sourceFile, { sf =>
365365
if (extensionsToCopy.contains(sf.extension)) {
366-
dest.parent.jfile.mkdirs
366+
dest.parent.createDirectory(force = true)
367367
copyfile(sf, false)
368368
} else {
369369
log(s"WARNING: ignoring $sf")
370370
}
371371
}, { sdir =>
372-
dest.jfile.mkdirs
372+
dest.createDirectory(force = true)
373373
sdir.list.foreach(path => recCopyFiles(path, dest / path.name))
374374
}, Some("DPCompilerTest.recCopyFiles: sourceFile not found: " + sourceFile))
375375
}

compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ScannerTest extends DottyTest {
3333
def scanDir(path: String): Unit = scanDir(Directory(path))
3434

3535
def scanDir(dir: Directory): Unit = {
36-
if (blackList exists (dir.jfile.toString endsWith _))
37-
println(s"blacklisted package: ${dir.jfile.getAbsolutePath}")
36+
if (blackList exists (dir.jpath.toString endsWith _))
37+
println(s"blacklisted package: ${dir.toAbsolute.jpath}")
3838
else
3939
for (f <- dir.files)
4040
if (f.name.endsWith(".scala"))
41-
if (blackList exists (f.jfile.toString endsWith _))
42-
println(s"blacklisted file: ${f.jfile.getAbsolutePath}")
41+
if (blackList exists (f.jpath.toString endsWith _))
42+
println(s"blacklisted file: ${f.toAbsolute.jpath}")
4343
else
4444
scan(new PlainFile(f))
4545
for (d <- dir.dirs)

compiler/test/dotty/tools/dotc/transform/PatmatExhaustivityTest.scala

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package dotc
44
package transform
55

66
import java.io._
7+
import java.nio.file.{Path => JPath}
78

89
import scala.io.Source._
910
import dotty.tools.io.Directory
@@ -16,53 +17,53 @@ class PatmatExhaustivityTest {
1617
// stop-after: patmatexhaust-huge.scala crash compiler
1718
val options = List("-color:never", "-Ystop-after:splitter", "-Ycheck-all-patmat", "-classpath", TestConfiguration.classPath)
1819

19-
private def compileFile(file: File) = {
20+
private def compileFile(path: JPath) = {
2021
val stringBuffer = new StringWriter()
2122
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
2223

2324
try {
24-
Main.process((file.getPath::options).toArray, reporter, null)
25+
Main.process((path.toString::options).toArray, reporter, null)
2526
} catch {
2627
case e: Throwable =>
27-
println(s"Compile $file exception:")
28+
println(s"Compile $path exception:")
2829
e.printStackTrace()
2930
}
3031

3132
val actual = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n")
32-
val checkFilePath = file.getAbsolutePath.stripSuffix(".scala") + ".check"
33+
val checkFilePath = path.toAbsolutePath.toString.stripSuffix(".scala") + ".check"
3334
val checkContent =
3435
if (new File(checkFilePath).exists)
3536
fromFile(checkFilePath).getLines().map(_.replaceAll("\\s+$", "")).mkString("\n").trim
3637
else ""
3738

38-
(file, checkContent, actual)
39+
(path, checkContent, actual)
3940
}
4041

4142
/** A single test with multiple files grouped in a folder */
42-
private def compileDir(file: File) = {
43+
private def compileDir(path: JPath) = {
4344
val stringBuffer = new StringWriter()
4445
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
4546

46-
val files = Directory(file.getPath).list.toList
47+
val files = Directory(path).list.toList
4748
.filter(f => f.extension == "scala" || f.extension == "java" )
48-
.map(_.jfile.getPath)
49+
.map(_.jpath.toString)
4950

5051
try {
5152
Main.process((options ++ files).toArray, reporter, null)
5253
} catch {
5354
case e: Throwable =>
54-
println(s"Compile $file exception:")
55+
println(s"Compile $path exception:")
5556
e.printStackTrace()
5657
}
5758

5859
val actual = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n")
59-
val checkFilePath = file.getPath + File.separator + "expected.check"
60+
val checkFilePath = path + File.separator + "expected.check"
6061
val checkContent =
6162
if (new File(checkFilePath).exists)
6263
fromFile(checkFilePath).getLines().map(_.replaceAll("\\s+$", "")).mkString("\n").trim
6364
else ""
6465

65-
(file, checkContent, actual)
66+
(path, checkContent, actual)
6667
}
6768

6869
@Test
@@ -71,9 +72,9 @@ class PatmatExhaustivityTest {
7172
.filter(f => f.extension == "scala" || f.isDirectory)
7273
.map { f =>
7374
if (f.isDirectory)
74-
compileDir(f.jfile)
75+
compileDir(f.jpath)
7576
else
76-
compileFile(f.jfile)
77+
compileFile(f.jpath)
7778
}
7879

7980
val failed = res.filter { case (_, expected, actual) => expected != actual }

0 commit comments

Comments
 (0)