Repository: scalameta/scalameta
Pull request #3554 (2024-02-13 22:38:15)

Warnings: replace deprecated JavaConverters


Patch:
diff --git a/bench/suite/src/main/scala/scala/meta/internal/bench/FileFixtures.scala b/bench/suite/src/main/scala/scala/meta/internal/bench/FileFixtures.scala
index 00985d5b7..a8edef1e0 100644
--- a/bench/suite/src/main/scala/scala/meta/internal/bench/FileFixtures.scala
+++ b/bench/suite/src/main/scala/scala/meta/internal/bench/FileFixtures.scala
@@ -1,7 +1,7 @@
 package scala.meta.internal.bench
 
 import java.nio.file._
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 
 trait FileFixtures {
   lazy val scalapDir: Path = {
@@ -9,7 +9,7 @@ trait FileFixtures {
   }
 
   lazy val scalapFiles: List[Path] = {
-    val all = Files.walk(scalapDir).iterator.asScala.toList
+    val all = Files.walk(scalapDir).iterator.toScala.toList
     all.filter(p => !Files.isDirectory(p))
   }
 }
diff --git a/scalameta/common/shared/src/main/scala-2.11/org/scalameta/collections/Conversions.scala b/scalameta/common/shared/src/main/scala-2.11/org/scalameta/collections/Conversions.scala
index 644a32533..283a8d1aa 100644
--- a/scalameta/common/shared/src/main/scala-2.11/org/scalameta/collections/Conversions.scala
+++ b/scalameta/common/shared/src/main/scala-2.11/org/scalameta/collections/Conversions.scala
@@ -1,20 +1,42 @@
 package org.scalameta.collections
 
-import scala.collection.immutable.{Map => ScalaMap}
-import java.util.{HashMap => JavaMap}
+import java.{util => ju}
+import scala.collection.immutable._
+import scala.collection.mutable
 
 private[collections] trait Conversions {
-  implicit class XtensionScalaMap[T, U](scalaMap: ScalaMap[T, U]) {
-    def toJavaMap: JavaMap[T, U] = {
-      import scala.collection.JavaConversions._
-      new JavaMap[T, U](mapAsJavaMap(scalaMap))
+  import scala.collection.JavaConversions._
+
+  implicit class XtensionScalaMap[T, U](obj: Map[T, U]) {
+    def toJava: ju.HashMap[T, U] = {
+      new ju.HashMap[T, U](mapAsJavaMap(obj))
+    }
+  }
+
+  implicit class XtensionScalaIterator[T](obj: Iterator[T]) {
+    def toJava: ju.Iterator[T] = {
+      asJavaIterator(obj)
+    }
+  }
+
+  implicit class XtensionJavaMap[T, U](obj: ju.HashMap[T, U]) {
+    def toScala: Map[T, U] = {
+      obj.toMap
+    }
+  }
+
+  implicit class XtensionJavaList[T](obj: ju.List[T]) {
+    def toScalaBuffer: mutable.Buffer[T] = {
+      asScalaBuffer(obj)
+    }
+    def toScala: List[T] = {
+      toScalaBuffer.toList
     }
   }
 
-  implicit class XtensionJavaMap[T, U](javaMap: JavaMap[T, U]) {
-    def toScalaMap: ScalaMap[T, U] = {
-      import scala.collection.JavaConversions._
-      javaMap.toMap
+  implicit class XtensionJavaIterator[T](obj: ju.Iterator[T]) {
+    def toScala: Iterator[T] = {
+      asScalaIterator(obj)
     }
   }
 }
diff --git a/scalameta/common/shared/src/main/scala-2.12/org/scalameta/collections/Conversions.scala b/scalameta/common/shared/src/main/scala-2.12/org/scalameta/collections/Conversions.scala
index 76b39ca94..dbe37ac01 100644
--- a/scalameta/common/shared/src/main/scala-2.12/org/scalameta/collections/Conversions.scala
+++ b/scalameta/common/shared/src/main/scala-2.12/org/scalameta/collections/Conversions.scala
@@ -1,20 +1,42 @@
 package org.scalameta.collections
 
-import scala.collection.immutable.{Map => ScalaMap}
-import java.util.{HashMap => JavaMap}
+import java.{util => ju}
+import scala.collection.immutable._
+import scala.collection.mutable
 
 private[collections] trait Conversions {
-  implicit class XtensionScalaMap[T, U](scalaMap: ScalaMap[T, U]) {
-    def toJavaMap: JavaMap[T, U] = {
-      import scala.collection.JavaConverters._
-      new JavaMap[T, U](mapAsJavaMap(scalaMap))
+  import scala.collection.JavaConverters._
+
+  implicit class XtensionScalaMap[T, U](obj: Map[T, U]) {
+    def toJava: ju.HashMap[T, U] = {
+      new ju.HashMap[T, U](mapAsJavaMap(obj))
+    }
+  }
+
+  implicit class XtensionScalaIterator[T](obj: Iterator[T]) {
+    def toJava: ju.Iterator[T] = {
+      obj.asJava
+    }
+  }
+
+  implicit class XtensionJavaMap[T, U](obj: ju.HashMap[T, U]) {
+    def toScala: Map[T, U] = {
+      obj.asScala.toMap
+    }
+  }
+
+  implicit class XtensionJavaList[T](obj: ju.List[T]) {
+    def toScalaBuffer: mutable.Buffer[T] = {
+      obj.asScala
+    }
+    def toScala: List[T] = {
+      toScalaBuffer.toList
     }
   }
 
-  implicit class XtensionJavaMap[T, U](javaMap: JavaMap[T, U]) {
-    def toScalaMap: ScalaMap[T, U] = {
-      import scala.collection.JavaConverters._
-      javaMap.asScala.toMap
+  implicit class XtensionJavaIterator[T](obj: ju.Iterator[T]) {
+    def toScala: Iterator[T] = {
+      obj.asScala
     }
   }
 }
diff --git a/scalameta/common/shared/src/main/scala-2.13/org/scalameta/collections/Conversions.scala b/scalameta/common/shared/src/main/scala-2.13/org/scalameta/collections/Conversions.scala
index 76b39ca94..fd6b95341 100644
--- a/scalameta/common/shared/src/main/scala-2.13/org/scalameta/collections/Conversions.scala
+++ b/scalameta/common/shared/src/main/scala-2.13/org/scalameta/collections/Conversions.scala
@@ -1,20 +1,42 @@
 package org.scalameta.collections
 
-import scala.collection.immutable.{Map => ScalaMap}
-import java.util.{HashMap => JavaMap}
+import java.{util => ju}
+import scala.collection.immutable._
+import scala.collection.mutable
 
 private[collections] trait Conversions {
-  implicit class XtensionScalaMap[T, U](scalaMap: ScalaMap[T, U]) {
-    def toJavaMap: JavaMap[T, U] = {
-      import scala.collection.JavaConverters._
-      new JavaMap[T, U](mapAsJavaMap(scalaMap))
+  import scala.jdk.CollectionConverters._
+
+  implicit class XtensionScalaMap[T, U](obj: Map[T, U]) {
+    def toJava: ju.HashMap[T, U] = {
+      new ju.HashMap[T, U](obj.asJava)
+    }
+  }
+
+  implicit class XtensionScalaIterator[T](obj: Iterator[T]) {
+    def toJava: ju.Iterator[T] = {
+      obj.asJava
+    }
+  }
+
+  implicit class XtensionJavaMap[T, U](obj: ju.HashMap[T, U]) {
+    def toScala: Map[T, U] = {
+      obj.asScala.toMap
+    }
+  }
+
+  implicit class XtensionJavaList[T](obj: ju.List[T]) {
+    def toScalaBuffer: mutable.Buffer[T] = {
+      obj.asScala
+    }
+    def toScala: List[T] = {
+      toScalaBuffer.toList
     }
   }
 
-  implicit class XtensionJavaMap[T, U](javaMap: JavaMap[T, U]) {
-    def toScalaMap: ScalaMap[T, U] = {
-      import scala.collection.JavaConverters._
-      javaMap.asScala.toMap
+  implicit class XtensionJavaIterator[T](obj: ju.Iterator[T]) {
+    def toScala: Iterator[T] = {
+      obj.asScala
     }
   }
 }
diff --git a/scalameta/io/js/src/main/scala/scala/meta/internal/io/NodeNIOPath.scala b/scalameta/io/js/src/main/scala/scala/meta/internal/io/NodeNIOPath.scala
index f83aa8a90..26c1683b0 100644
--- a/scalameta/io/js/src/main/scala/scala/meta/internal/io/NodeNIOPath.scala
+++ b/scalameta/io/js/src/main/scala/scala/meta/internal/io/NodeNIOPath.scala
@@ -4,7 +4,7 @@ import java.io.File
 import java.net.URI
 import java.nio.file.Path
 import java.util
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 
 // Rough implementation of java.nio.Path, should work similarly for the happy
 // path but has undefined behavior for error handling.
@@ -83,7 +83,7 @@ case class NodeNIOPath(filename: String) extends Path {
   override def toString: String =
     filename
   override def iterator(): util.Iterator[Path] =
-    filename.split(File.separator).iterator.map(name => NodeNIOPath(name): Path).asJava
+    filename.split(File.separator).iterator.map(name => NodeNIOPath(name): Path).toJava
 }
 
 object NodeNIOPath {
diff --git a/scalameta/io/jvm/src/main/scala/scala/meta/internal/io/PlatformFileIO.scala b/scalameta/io/jvm/src/main/scala/scala/meta/internal/io/PlatformFileIO.scala
index f1f57d20c..21b3a72c2 100644
--- a/scalameta/io/jvm/src/main/scala/scala/meta/internal/io/PlatformFileIO.scala
+++ b/scalameta/io/jvm/src/main/scala/scala/meta/internal/io/PlatformFileIO.scala
@@ -54,16 +54,16 @@ object PlatformFileIO {
     Files.isDirectory(path.toNIO)
 
   def listAllFilesRecursively(root: AbsolutePath): ListFiles = {
-    import scala.collection.JavaConverters._
+    import org.scalameta.collections._
     val relativeFiles = Files
       .walk(root.toNIO)
       .collect(Collectors.toList[Path])
-      .asScala
+      .toScala
       .collect {
         case path if Files.isRegularFile(path) =>
           RelativePath(root.toNIO.relativize(path))
       }
-    new ListFiles(root, relativeFiles.toList)
+    ListFiles(root, relativeFiles)
   }
 
   def jarRootPath(jarFile: AbsolutePath, create: Boolean = false): AbsolutePath = {
diff --git a/scalameta/io/shared/src/main/scala/scala/meta/io/RelativePath.scala b/scalameta/io/shared/src/main/scala/scala/meta/io/RelativePath.scala
index 2a26c43f8..35deffe31 100644
--- a/scalameta/io/shared/src/main/scala/scala/meta/io/RelativePath.scala
+++ b/scalameta/io/shared/src/main/scala/scala/meta/io/RelativePath.scala
@@ -6,7 +6,7 @@ import java.nio.file.Path
 import java.nio.{file => nio}
 import java.nio.file.Paths
 import scala.meta.internal.io.PathIO
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 
 /** Wrapper around a relative nio.Path. */
 sealed abstract case class RelativePath(toNIO: Path) {
@@ -41,7 +41,7 @@ object RelativePath {
   private[meta] def toURI(path: Path, isDirectory: Boolean): URI = {
     val suffix = if (isDirectory) "/" else ""
     // Can't use toNIO.toUri because it produces an absolute URI.
-    val names = path.iterator().asScala
+    val names = path.iterator().toScala
     val uris = names.map { name =>
       // URI encode each part of the path individually.
       new URI(null, null, name.toString, null)
diff --git a/semanticdb/metacp/src/main/scala/scala/meta/internal/classpath/ClasspathIndex.scala b/semanticdb/metacp/src/main/scala/scala/meta/internal/classpath/ClasspathIndex.scala
index 0398b7d02..f9b4a96a8 100644
--- a/semanticdb/metacp/src/main/scala/scala/meta/internal/classpath/ClasspathIndex.scala
+++ b/semanticdb/metacp/src/main/scala/scala/meta/internal/classpath/ClasspathIndex.scala
@@ -9,7 +9,7 @@ import java.nio.file.SimpleFileVisitor
 import java.nio.file.attribute.BasicFileAttributes
 import java.util.jar.JarFile
 import scala.collection.mutable
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 import scala.meta.io.AbsolutePath
 import scala.meta.io.Classpath
 import scala.meta.internal.io.PathIO
@@ -87,10 +87,10 @@ object ClasspathIndex {
       val fs = FileSystems.getFileSystem(URI.create("jrt:/"))
       val dir = fs.getPath("/packages")
       for {
-        pkg <- Files.newDirectoryStream(dir).iterator().asScala
+        pkg <- Files.newDirectoryStream(dir).iterator().toScala
         symbol = pkg.toString.stripPrefix("/packages/").replace('.', '/') + "/"
         classdir = getClassdir(symbol)
-        moduleLink <- Files.list(pkg).iterator().asScala
+        moduleLink <- Files.list(pkg).iterator().toScala
       } {
         val module =
           if (!Files.isSymbolicLink(moduleLink)) moduleLink
diff --git a/semanticdb/metacp/src/main/scala/scala/meta/internal/javacp/Javacp.scala b/semanticdb/metacp/src/main/scala/scala/meta/internal/javacp/Javacp.scala
index de5638499..680aa34d8 100644
--- a/semanticdb/metacp/src/main/scala/scala/meta/internal/javacp/Javacp.scala
+++ b/semanticdb/metacp/src/main/scala/scala/meta/internal/javacp/Javacp.scala
@@ -1,7 +1,7 @@
 package scala.meta.internal.javacp
 
+import org.scalameta.collections._
 import org.scalameta.internal.ScalaCompat._
-import scala.collection.JavaConverters._
 import scala.collection.mutable.ArrayBuffer
 import scala.collection.mutable.ListBuffer
 import scala.meta.internal.classpath.ClasspathIndex
@@ -62,7 +62,7 @@ object Javacp {
     val classSymbol = ssym(node.name)
     val classDisplayName = sdisplayName(node.name)
     val classAccess = node.access | access
-    val hasOuterClassReference = node.fields.asScala.exists(isOuterClassReference)
+    val hasOuterClassReference = node.fields.toScala.exists(isOuterClassReference)
     val isEnum = node.access.hasFlag(o.ACC_ENUM)
 
     val classKind =
@@ -77,7 +77,7 @@ object Javacp {
         // when assigning classParents below.
         ClassSignature.simple("impossible", Nil)
       } else if (node.signature == null) {
-        ClassSignature.simple(node.superName, node.interfaces.asScala.toList)
+        ClassSignature.simple(node.superName, node.interfaces.toScala.toList)
       } else {
         JavaTypeSignature.parse(node.signature, new ClassSignatureVisitor)
       }
@@ -93,7 +93,7 @@ object Javacp {
       if (isJavaLangObject) Nil
       else classSignature.parents.map(_.toSemanticTpe(classScope))
 
-    node.fields.asScala
+    node.fields.toScala
       .filterNot(_.access.hasFlag(o.ACC_SYNTHETIC))
       .foreach { field: FieldNode =>
         if (isOuterClassReference(field)) {
@@ -118,7 +118,7 @@ object Javacp {
         }
       }
 
-    val methodSignatures = node.methods.asScala
+    val methodSignatures = node.methods.toScala
       .filterNot(_.access.hasFlag(o.ACC_SYNTHETIC))
       .map { method: MethodNode =>
         val signature = JavaTypeSignature.parse(
@@ -248,7 +248,7 @@ object Javacp {
     }
 
     // node.innerClasses includes all inner classes, both direct and those nested inside other inner classes.
-    val directInnerClasses = node.innerClasses.asScala.filter(_.outerName == node.name)
+    val directInnerClasses = node.innerClasses.toScala.filter(_.outerName == node.name)
     directInnerClasses.foreach { ic =>
       val innerClassSymbol = ssym(ic.name)
       decls += innerClassSymbol
@@ -286,7 +286,7 @@ object Javacp {
   // ClassNode.innerClasses includes all inner classes of a compilation unit, both nested inner classes as well
   // as enclosing outer classes. Anonymous classes are distinguished by InnerClassNode.innerName == null.
   private def isAnonymousClass(node: ClassNode): Boolean = {
-    node.innerClasses.asScala.exists { ic: InnerClassNode =>
+    node.innerClasses.toScala.exists { ic: InnerClassNode =>
       ic.name == node.name &&
       ic.innerName == null
     }
diff --git a/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/ClassfileInfos.scala b/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/ClassfileInfos.scala
index 643b5bd1d..2a302c8b3 100644
--- a/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/ClassfileInfos.scala
+++ b/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/ClassfileInfos.scala
@@ -1,7 +1,7 @@
 package scala.meta.internal.metacp
 
 import java.nio.file.Files
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 import scala.meta.cli._
 import scala.meta.internal.classpath.ClasspathIndex
 import scala.meta.internal.io.FileIO
@@ -46,11 +46,11 @@ object ClassfileInfos {
       case Some(scalaSig) =>
         Some(Scalacp.parse(scalaSig, classpathIndex, settings, reporter))
       case None =>
-        val attrs = if (node.attrs != null) node.attrs.asScala else Nil
+        val attrs = if (node.attrs != null) node.attrs.toScala else Nil
         if (attrs.exists(_.`type` == "Scala")) {
           None
         } else {
-          val innerClassNode = node.innerClasses.asScala.find(_.name == node.name)
+          val innerClassNode = node.innerClasses.toScala.find(_.name == node.name)
           if (innerClassNode.isEmpty) {
             if (node.name != "module-info") Some(Javacp.parse(node, classpathIndex))
             else None
diff --git a/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/package.scala b/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/package.scala
index 30204a28a..a2464856d 100644
--- a/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/package.scala
+++ b/semanticdb/metacp/src/main/scala/scala/meta/internal/metacp/package.scala
@@ -10,7 +10,7 @@ import scala.meta.io.AbsolutePath
 import scala.tools.asm._
 import scala.tools.asm.ClassReader._
 import scala.tools.asm.tree._
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 import scala.meta.internal.scalacp.ScalaSigAttribute
 import scala.meta.internal.scalacp.ScalaSigNode
 import scala.reflect.internal.pickling.ByteCodecs
@@ -25,7 +25,7 @@ package object metacp {
       if (node.attrs == null) None
       else {
         for {
-          scalaSigAttribute <- node.attrs.asScala.collectFirst { case ScalaSigAttribute(scalaSig) =>
+          scalaSigAttribute <- node.attrs.toScala.collectFirst { case ScalaSigAttribute(scalaSig) =>
             scalaSig
           }
           scalaSig <- {
@@ -38,18 +38,18 @@ package object metacp {
     private def fromScalaSigAnnotation: Option[ScalaSig] = {
       if (node.visibleAnnotations == null) None
       else {
-        node.visibleAnnotations.asScala.collectFirst {
+        node.visibleAnnotations.toScala.collectFirst {
           case annot
               if annot.desc == Main.SCALA_SIG_ANNOTATION ||
                 annot.desc == Main.SCALA_LONG_SIG_ANNOTATION =>
-            annot.values.asScala match {
+            annot.values.toScala match {
               case collection.Seq("bytes", anyBytes) =>
                 val baos = new ByteArrayOutputStream()
                 val bytes: Array[Byte] = anyBytes match {
                   case bytesString: String =>
                     bytesString.getBytes(StandardCharsets.UTF_8)
                   case bytesArray: util.ArrayList[_] =>
-                    bytesArray.asScala.foreach { case bytesString: String =>
+                    bytesArray.toScala.foreach { case bytesString: String =>
                       baos.write(bytesString.getBytes(StandardCharsets.UTF_8))
                     }
                     baos.toByteArray
@@ -76,7 +76,7 @@ package object metacp {
     }
     def hasScalaSig: Boolean = {
       val classNode = readInputStreamToClassNode(classfile.openInputStream())
-      classNode.attrs != null && classNode.attrs.asScala.exists(_.`type` match {
+      classNode.attrs != null && classNode.attrs.toScala.exists(_.`type` match {
         case "Scala" | "ScalaSig" => true
         case _ => false
       })
diff --git a/semanticdb/scalac/library/src/main/scala/scala/meta/internal/semanticdb/scalac/ReflectionToolkit.scala b/semanticdb/scalac/library/src/main/scala/scala/meta/internal/semanticdb/scalac/ReflectionToolkit.scala
index d1612ee3d..3721659b6 100644
--- a/semanticdb/scalac/library/src/main/scala/scala/meta/internal/semanticdb/scalac/ReflectionToolkit.scala
+++ b/semanticdb/scalac/library/src/main/scala/scala/meta/internal/semanticdb/scalac/ReflectionToolkit.scala
@@ -65,12 +65,12 @@ trait ReflectionToolkit {
     def toMap: Map[String, Any] =
       carrier.attachments
         .get[java.util.HashMap[String, Any]]
-        .map(_.toScalaMap)
+        .map(_.toScala)
         .getOrElse(Map[String, Any]())
     def toOption: Option[Map[String, Any]] =
-      carrier.attachments.get[java.util.HashMap[String, Any]].map(_.toScalaMap)
+      carrier.attachments.get[java.util.HashMap[String, Any]].map(_.toScala)
     def transform(f: Map[String, Any] => Map[String, Any]): Unit =
-      carrier.updateAttachment(f(toMap).toJavaMap)
+      carrier.updateAttachment(f(toMap).toJava)
     def contains(key: String): Boolean = toMap.contains(key)
     def apply(key: String): Any = toMap(key)
     def get(key: String): Option[Any] = toMap.get(key)
diff --git a/semanticdb/semanticdb/src/main/scala/scala/meta/internal/semanticdb/Locator.scala b/semanticdb/semanticdb/src/main/scala/scala/meta/internal/semanticdb/Locator.scala
index ca4b04949..9a6e2c2dc 100644
--- a/semanticdb/semanticdb/src/main/scala/scala/meta/internal/semanticdb/Locator.scala
+++ b/semanticdb/semanticdb/src/main/scala/scala/meta/internal/semanticdb/Locator.scala
@@ -2,7 +2,7 @@ package scala.meta.internal.semanticdb
 
 import java.nio.file._
 import java.util.jar._
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 
 object Locator {
   def apply(paths: List[Path])(fn: (Path, TextDocuments) => Unit): Unit = {
@@ -15,7 +15,7 @@ object Locator {
         Files
           .walk(path)
           .iterator()
-          .asScala
+          .toScala
           .filter(_.toString.endsWith(".semanticdb"))
           .toArray
           // NOTE: nio.file.Path.compareTo is file system specific,


Test patch:
diff --git a/scalameta/testkit/jvm/src/main/scala/scala/meta/testkit/SyntaxAnalysis.scala b/scalameta/testkit/jvm/src/main/scala/scala/meta/testkit/SyntaxAnalysis.scala
index e683c6469..1087a1a8f 100644
--- a/scalameta/testkit/jvm/src/main/scala/scala/meta/testkit/SyntaxAnalysis.scala
+++ b/scalameta/testkit/jvm/src/main/scala/scala/meta/testkit/SyntaxAnalysis.scala
@@ -4,7 +4,7 @@ import java.util.concurrent.CopyOnWriteArrayList
 import java.util.concurrent.atomic.AtomicInteger
 
 import scala.collection.{GenIterable, GenTraversableOnce, mutable}
-import scala.collection.JavaConverters._
+import org.scalameta.collections._
 import scala.meta._
 import scala.util.control.NonFatal
 
@@ -57,7 +57,7 @@ object SyntaxAnalysis {
       }
     }
     corpus.foreach(analyze)
-    results.asScala
+    results.toScalaBuffer
   }
 
   def onParsed[A](