Repository: joernio/joern
Pull request #824 (2021-12-15 13:38:25)

Stop using classes from scala.reflect.io.
Preparation for scala 3 migration.


Patch:
diff --git a/console/src/main/scala/io/joern/console/QueryDatabase.scala b/console/src/main/scala/io/joern/console/QueryDatabase.scala
index 74611ea46..ebaec74b0 100644
--- a/console/src/main/scala/io/joern/console/QueryDatabase.scala
+++ b/console/src/main/scala/io/joern/console/QueryDatabase.scala
@@ -139,14 +139,21 @@ class QueryDatabase(defaultArgumentProvider: DefaultArgumentProvider = new Defau
   * */
 class DefaultArgumentProvider {
 
-  def defaultArgument(method: MethodSymbol, im: InstanceMirror, @unused x: Symbol, i: Int): Option[Any] = {
-    val typeSignature = im.symbol.typeSignature
-    val defaultMethodName = s"${method.name}$$default$$${i + 1}"
-    val m = typeSignature.member(TermName(defaultMethodName))
-    if (m.isMethod) {
-      Some(im.reflectMethod(m.asMethod).apply())
-    } else {
-      None
+  def typeSpecificDefaultArg(argTypeFullName: String): Option[Any] = {
+    None
+  }
+
+  final def defaultArgument(method: MethodSymbol, im: InstanceMirror, x: Symbol, i: Int): Option[Any] = {
+    val defaultArgOption = typeSpecificDefaultArg(x.typeSignature.toString)
+    defaultArgOption.orElse {
+      val typeSignature = im.symbol.typeSignature
+      val defaultMethodName = s"${method.name}$$default$$${i + 1}"
+      val m = typeSignature.member(TermName(defaultMethodName))
+      if (m.isMethod) {
+        Some(im.reflectMethod(m.asMethod).apply())
+      } else {
+        None
+      }
     }
   }
 
diff --git a/console/src/main/scala/io/joern/console/workspacehandling/WorkspaceManager.scala b/console/src/main/scala/io/joern/console/workspacehandling/WorkspaceManager.scala
index faff18609..d849af88c 100644
--- a/console/src/main/scala/io/joern/console/workspacehandling/WorkspaceManager.scala
+++ b/console/src/main/scala/io/joern/console/workspacehandling/WorkspaceManager.scala
@@ -11,7 +11,6 @@ import overflowdb.Config
 import java.net.URLEncoder
 import java.nio.file.Path
 import scala.collection.mutable.ListBuffer
-import scala.reflect.io.Directory
 import scala.util.{Failure, Success, Try}
 
 object DefaultLoader extends WorkspaceLoader[Project] {
@@ -72,7 +71,7 @@ class WorkspaceManager[ProjectType <: Project](path: String, loader: WorkspaceLo
   def removeProject(name: String): Unit = {
     closeProject(name)
     removeProjectFromList(name)
-    new Directory(projectNameToDir(name).toFile).deleteRecursively()
+    File(projectNameToDir(name)).delete()
   }
 
   /**
@@ -121,7 +120,7 @@ class WorkspaceManager[ProjectType <: Project](path: String, loader: WorkspaceLo
       throw new RuntimeException(s"Directory ${dirFile.toString} does not exist")
     }
 
-    new Directory(dirFile.toJava).deleteRecursively()
+    dirFile.delete()
   }
 
   /**
@@ -411,7 +410,7 @@ class WorkspaceManager[ProjectType <: Project](path: String, loader: WorkspaceLo
   private def deleteProject(project: Project): Unit = {
     removeProjectFromList(project.name)
     if (project.path.toString != "") {
-      new Directory(File(project.path).toJava).deleteRecursively()
+      File(project.path).delete()
     }
   }
 
diff --git a/joern-cli/src/main/scala/io/joern/JoernScan.scala b/joern-cli/src/main/scala/io/joern/JoernScan.scala
index 27141aac5..8da137dd7 100644
--- a/joern-cli/src/main/scala/io/joern/JoernScan.scala
+++ b/joern-cli/src/main/scala/io/joern/JoernScan.scala
@@ -16,7 +16,6 @@ import io.joern.Scan.{allTag, defaultTag}
 import io.shiftleft.semanticcpg.language.{DefaultNodeExtensionFinder, NodeExtensionFinder}
 
 import scala.jdk.CollectionConverters._
-import scala.reflect.runtime.universe._
 
 object JoernScanConfig {
   val defaultDbVersion: String = "latest"
@@ -284,11 +283,11 @@ class Scan(options: ScanOptions)(implicit engineContext: EngineContext) extends
 
 class JoernDefaultArgumentProvider(maxCallDepth: Int)(implicit context: EngineContext) extends DefaultArgumentProvider {
 
-  override def defaultArgument(method: MethodSymbol, im: InstanceMirror, x: Symbol, i: Int): Option[Any] = {
-    if (x.typeSignature.toString.endsWith("EngineContext")) {
+  override def typeSpecificDefaultArg(argTypeFullName: String): Option[Any] = {
+    if (argTypeFullName.endsWith("EngineContext")) {
       Some(context.copy(config = EngineConfig(maxCallDepth = maxCallDepth)))
     } else {
-      super.defaultArgument(method, im, x, i)
+      None
     }
   }
 }
diff --git a/querydb/src/main/scala/io/joern/dumpq/Main.scala b/querydb/src/main/scala/io/joern/dumpq/Main.scala
index 7c45c9348..7e937b4a3 100644
--- a/querydb/src/main/scala/io/joern/dumpq/Main.scala
+++ b/querydb/src/main/scala/io/joern/dumpq/Main.scala
@@ -6,8 +6,6 @@ import io.joern.dataflowengineoss.semanticsloader.Semantics
 import org.json4s.{Formats, NoTypeHints}
 import org.json4s.native.Serialization
 
-import scala.reflect.runtime.universe._
-
 object Main extends App {
 
   dumpQueries()
@@ -30,11 +28,11 @@ object Main extends App {
   class JoernDefaultArgumentProvider(maxCallDepth: Int)(implicit context: EngineContext)
       extends DefaultArgumentProvider {
 
-    override def defaultArgument(method: MethodSymbol, im: InstanceMirror, x: Symbol, i: Int): Option[Any] = {
-      if (x.typeSignature.toString.endsWith("EngineContext")) {
+    override def typeSpecificDefaultArg(argTypeFullName: String): Option[Any] = {
+      if (argTypeFullName.endsWith("EngineContext")) {
         Some(context.copy(config = EngineConfig(maxCallDepth = maxCallDepth)))
       } else {
-        super.defaultArgument(method, im, x, i)
+        None
       }
     }
   }


Test patch:
diff --git a/console/src/test/scala/io/joern/console/workspacehandling/WorkspaceLoaderTests.scala b/console/src/test/scala/io/joern/console/workspacehandling/WorkspaceLoaderTests.scala
index ea782863f..cdf25ca39 100644
--- a/console/src/test/scala/io/joern/console/workspacehandling/WorkspaceLoaderTests.scala
+++ b/console/src/test/scala/io/joern/console/workspacehandling/WorkspaceLoaderTests.scala
@@ -5,8 +5,6 @@ import better.files.File
 import org.scalatest.matchers.should.Matchers
 import org.scalatest.wordspec.AnyWordSpec
 
-import scala.reflect.io.Directory
-
 class WorkspaceLoaderTests extends AnyWordSpec with Matchers {
 
   private val tmpDirPrefix = "workspace-tests"
@@ -15,12 +13,12 @@ class WorkspaceLoaderTests extends AnyWordSpec with Matchers {
 
     "create workspace and workspace directory if nonexistent" in {
       val dir = File.newTemporaryDirectory(tmpDirPrefix)
-      new Directory(dir.toJava).deleteRecursively()
+      dir.delete()
       TestLoader().load(dir.path.toString)
       try {
         dir.exists shouldBe true
       } finally {
-        new Directory(dir.toJava).deleteRecursively()
+        dir.delete()
       }
     }
 
diff --git a/joern-cli/frontends/jimple2cpg/src/test/scala/io/joern/jimple2cpg/unpacking/JarUnpacking.scala b/joern-cli/frontends/jimple2cpg/src/test/scala/io/joern/jimple2cpg/unpacking/JarUnpacking.scala
index bbc7e9be9..6d24b87e8 100644
--- a/joern-cli/frontends/jimple2cpg/src/test/scala/io/joern/jimple2cpg/unpacking/JarUnpacking.scala
+++ b/joern-cli/frontends/jimple2cpg/src/test/scala/io/joern/jimple2cpg/unpacking/JarUnpacking.scala
@@ -1,5 +1,6 @@
 package io.joern.jimple2cpg.unpacking
 
+import better.files.File
 import io.joern.jimple2cpg.Jimple2Cpg
 import io.shiftleft.codepropertygraph.Cpg
 import io.shiftleft.semanticcpg.language.{toMethodTraversalExtGen, toNodeTypeStarters}
@@ -9,7 +10,6 @@ import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper
 import org.scalatest.wordspec.AnyWordSpec
 import io.shiftleft.semanticcpg.language._
 
-import scala.reflect.io.File
 import scala.util.{Failure, Success, Try}
 
 class JarUnpacking extends AnyWordSpec with Matchers with BeforeAndAfterAll {
@@ -29,8 +29,8 @@ class JarUnpacking extends AnyWordSpec with Matchers with BeforeAndAfterAll {
   "should extract files" in {
     Try(getClass.getResource("/unpacking")) match {
       case Success(x) =>
-        val fs = File(x.getPath).toDirectory.walk.toSeq
-        fs.filter(_.name.contains(".jar")).map(_.name).toList shouldBe List("HelloWorld.jar")
+        val fs = File(x.getPath).walk().toList
+        fs.filter(_.name.contains(".jar")).map(_.name) shouldBe List("HelloWorld.jar")
         fs.count(_.name.contains(".class")) shouldBe 2
       case Failure(x: Throwable) =>
         fail("Unable to view test repository", x)
diff --git a/querydb/src/test/scala/io/joern/suites/QDBArgumentProvider.scala b/querydb/src/test/scala/io/joern/suites/QDBArgumentProvider.scala
index 204ac38a7..dae31fc6a 100644
--- a/querydb/src/test/scala/io/joern/suites/QDBArgumentProvider.scala
+++ b/querydb/src/test/scala/io/joern/suites/QDBArgumentProvider.scala
@@ -4,19 +4,17 @@ import io.joern.console.DefaultArgumentProvider
 import io.joern.dataflowengineoss.queryengine.EngineContext
 import io.joern.dataflowengineoss.semanticsloader.{Parser, Semantics}
 
-import scala.reflect.runtime.universe._
-
 class QDBArgumentProvider(maxCallDepth: Int) extends DefaultArgumentProvider {
   def testSemanticsFilename = "src/test/resources/default.semantics"
 
-  override def defaultArgument(method: MethodSymbol, im: InstanceMirror, x: Symbol, i: Int): Option[Any] = {
-    if (x.typeSignature.toString.endsWith("EngineContext")) {
+  override def typeSpecificDefaultArg(argTypeFullName: String): Option[Any] = {
+    if (argTypeFullName.endsWith("EngineContext")) {
       val newsemantics = Semantics.fromList(new Parser().parseFile(testSemanticsFilename))
       val engineContext = EngineContext(newsemantics)
       engineContext.config.maxCallDepth = maxCallDepth
       Some(engineContext)
     } else {
-      super.defaultArgument(method, im, x, i)
+      None
     }
   }
 }