Skip to content

Commit 2b09bc3

Browse files
authored
Avoid URL constructor deprecated on JDK20 (#17403)
Forward ports tweaks from scala/scala#10335
2 parents 0e00420 + afbc53b commit 2b09bc3

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

compiler/src/dotty/tools/dotc/classpath/DirectoryClassPath.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package dotty.tools.dotc.classpath
66
import scala.language.unsafeNulls
77

88
import java.io.{File => JFile}
9-
import java.net.URL
9+
import java.net.{URI, URL}
1010
import java.nio.file.{FileSystems, Files}
1111

1212
import dotty.tools.dotc.classpath.PackageNameUtils.{packageContains, separatePkgAndClassNames}
@@ -194,7 +194,7 @@ final class JrtClassPath(fs: java.nio.file.FileSystem) extends ClassPath with No
194194
if (inPackage.isRoot) ClassPathEntries(packages(inPackage), Nil)
195195
else ClassPathEntries(packages(inPackage), classes(inPackage))
196196

197-
def asURLs: Seq[URL] = Seq(new URL("jrt:/"))
197+
def asURLs: Seq[URL] = Seq(new URI("jrt:/").toURL)
198198
// We don't yet have a scheme to represent the JDK modules in our `-classpath`.
199199
// java models them as entries in the new "module path", we'll probably need to follow this.
200200
def asClassPathStrings: Seq[String] = Nil

compiler/src/dotty/tools/dotc/classpath/VirtualDirectoryClassPath.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package dotty.tools.dotc.classpath
22

33
import scala.language.unsafeNulls
44

5-
import dotty.tools.io.ClassRepresentation
5+
import dotty.tools.io.{ClassPath, ClassRepresentation}
66
import dotty.tools.io.{AbstractFile, VirtualDirectory}
77
import FileUtils._
8-
import java.net.URL
9-
10-
import dotty.tools.io.ClassPath
8+
import java.net.{URI, URL}
119

1210
case class VirtualDirectoryClassPath(dir: VirtualDirectory) extends ClassPath with DirectoryLookup[ClassFileEntryImpl] with NoSourcePaths {
1311
type F = AbstractFile
@@ -37,7 +35,7 @@ case class VirtualDirectoryClassPath(dir: VirtualDirectory) extends ClassPath wi
3735
def isPackage(f: AbstractFile): Boolean = f.isPackage
3836

3937
// mimic the behavior of the old nsc.util.DirectoryClassPath
40-
def asURLs: Seq[URL] = Seq(new URL(dir.name))
38+
def asURLs: Seq[URL] = Seq(new URI(dir.name).toURL)
4139
def asClassPathStrings: Seq[String] = Seq(dir.path)
4240

4341
override def findClass(className: String): Option[ClassRepresentation] = findClassFile(className) map ClassFileEntryImpl.apply

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ package io
99

1010
import scala.language.unsafeNulls
1111

12-
import java.net.MalformedURLException
13-
import java.net.URL
12+
import java.net.{MalformedURLException, URI, URISyntaxException, URL}
1413
import java.util.regex.PatternSyntaxException
1514

1615
import File.pathSeparator
@@ -182,8 +181,8 @@ object ClassPath {
182181
}
183182

184183
def specToURL(spec: String): Option[URL] =
185-
try Some(new URL(spec))
186-
catch { case _: MalformedURLException => None }
184+
try Some(new URI(spec).toURL)
185+
catch case _: MalformedURLException | _: URISyntaxException => None
187186

188187
def manifests: List[java.net.URL] = {
189188
import scala.jdk.CollectionConverters.EnumerationHasAsScala

compiler/src/dotty/tools/repl/AbstractFileClassLoader.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import java.util.Collections
2323
class AbstractFileClassLoader(val root: AbstractFile, parent: ClassLoader) extends ClassLoader(parent):
2424
private def findAbstractFile(name: String) = root.lookupPath(name.split('/').toIndexedSeq, directory = false)
2525

26+
// on JDK 20 the URL constructor we're using is deprecated,
27+
// but the recommended replacement, URL.of, doesn't exist on JDK 8
28+
@annotation.nowarn("cat=deprecation")
2629
override protected def findResource(name: String) =
2730
findAbstractFile(name) match
2831
case null => null

project/DocumentationWebsite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.io.File
2+
import java.net.URI
23
import java.nio.file.Paths
34
import sbt._
45
import Build._
@@ -48,7 +49,7 @@ object DocumentationWebsite {
4849
sbt.IO.touch(inkuireDestinationFile)
4950

5051
def tryFetch(retries: Int, timeout: Duration): Unit = {
51-
val downloadProcess = (new java.net.URL(inkuireLink) #> inkuireDestinationFile).run()
52+
val downloadProcess = (new URI(inkuireLink).toURL #> inkuireDestinationFile).run()
5253
val result: Future[Int] = Future(blocking(downloadProcess.exitValue()))
5354
try {
5455
Await.result(result, timeout) match {

0 commit comments

Comments
 (0)