Skip to content

Commit a4244e8

Browse files
committed
Make InterruptInstrumentation typesafe by using enum
1 parent 3a4b402 commit a4244e8

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,30 @@ import io.AbstractFile
2222
import java.net.{URL, URLConnection, URLStreamHandler}
2323
import java.util.Collections
2424

25-
class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader, interruptInstrumentation: String)
25+
import AbstractFileClassLoader.InterruptInstrumentation
26+
27+
28+
object AbstractFileClassLoader:
29+
enum InterruptInstrumentation(val stringValue: String):
30+
case Disabled extends InterruptInstrumentation("false")
31+
case Enabled extends InterruptInstrumentation("true")
32+
case Local extends InterruptInstrumentation("local")
33+
34+
def is(value: InterruptInstrumentation): Boolean = this == value
35+
def isOneOf(others: InterruptInstrumentation*): Boolean = others.contains(this)
36+
37+
object InterruptInstrumentation:
38+
def fromString(string: String): InterruptInstrumentation = string match {
39+
case "false" => Disabled
40+
case "true" => Enabled
41+
case "local" => Local
42+
case _ => throw new IllegalArgumentException(s"Invalid interrupt instrumentation value: $string")
43+
}
44+
45+
class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader, interruptInstrumentation: InterruptInstrumentation)
2646
extends io.AbstractFileClassLoader(root, parent):
2747

28-
def this(root: AbstractFile, parent: ClassLoader) = this(root, parent, ScalaSettings.XreplInterruptInstrumentation.default)
48+
def this(root: AbstractFile, parent: ClassLoader) = this(root, parent, InterruptInstrumentation.fromString(ScalaSettings.XreplInterruptInstrumentation.default))
2949

3050
override def findClass(name: String): Class[?] = {
3151
var file: AbstractFile | Null = root
@@ -39,7 +59,7 @@ class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader, interrupt
3959

4060
val bytes = file.toByteArray
4161

42-
if interruptInstrumentation != "false" then defineClassInstrumented(name, bytes)
62+
if !interruptInstrumentation.is(InterruptInstrumentation.Enabled) then defineClassInstrumented(name, bytes)
4363
else defineClass(name, bytes, 0, bytes.length)
4464
}
4565

@@ -49,8 +69,8 @@ class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader, interrupt
4969
}
5070

5171
override def loadClass(name: String): Class[?] =
52-
if interruptInstrumentation == "false" || interruptInstrumentation == "local"
53-
then return super.loadClass(name)
72+
if interruptInstrumentation.isOneOf(InterruptInstrumentation.Disabled, InterruptInstrumentation.Local) then
73+
return super.loadClass(name)
5474

5575
val loaded = findLoadedClass(name) // Check if already loaded
5676
if loaded != null then return loaded

repl/src/dotty/tools/repl/DependencyResolver.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import java.net.{URL, URLClassLoader}
77
import scala.jdk.CollectionConverters.*
88
import scala.util.control.NonFatal
99

10+
import dotty.tools.repl.AbstractFileClassLoader
11+
1012
import coursierapi.{Repository, Dependency, MavenRepository}
1113
import com.virtuslab.using_directives.UsingDirectivesProcessor
1214
import com.virtuslab.using_directives.custom.model.{Path, StringValue, Value}
@@ -90,7 +92,7 @@ object DependencyResolver:
9092
import dotty.tools.dotc.classpath.ClassPathFactory
9193
import dotty.tools.dotc.core.SymbolLoaders
9294
import dotty.tools.dotc.core.Symbols.defn
93-
import dotty.tools.io.*
95+
import dotty.tools.io.{AbstractFile, ClassPath}
9496
import dotty.tools.repl.ScalaClassLoader.fromURLsParallelCapable
9597

9698
// Create a classloader with all the resolved JAR files
@@ -106,10 +108,10 @@ object DependencyResolver:
106108
SymbolLoaders.mergeNewEntries(defn.RootClass, ClassPath.RootPackage, jarClassPath, ctx.platform.classPath)
107109

108110
// Create new classloader with previous output dir and resolved dependencies
109-
new dotty.tools.repl.AbstractFileClassLoader(
111+
new AbstractFileClassLoader(
110112
prevOutputDir,
111113
depsClassLoader,
112-
ctx.settings.XreplInterruptInstrumentation.value
114+
AbstractFileClassLoader.InterruptInstrumentation.fromString(ctx.settings.XreplInterruptInstrumentation.value)
113115
)
114116

115117
end DependencyResolver

repl/src/dotty/tools/repl/Rendering.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
8686
myClassLoader = new AbstractFileClassLoader(
8787
ctx.settings.outputDir.value,
8888
parent,
89-
ctx.settings.XreplInterruptInstrumentation.value
89+
AbstractFileClassLoader.InterruptInstrumentation.fromString(ctx.settings.XreplInterruptInstrumentation.value)
9090
)
9191
myClassLoader
9292
}

repl/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ class ReplDriver(settings: Array[String],
607607
rendering.myClassLoader = new AbstractFileClassLoader(
608608
prevOutputDir,
609609
jarClassLoader,
610-
ctx.settings.XreplInterruptInstrumentation.value
610+
AbstractFileClassLoader.InterruptInstrumentation.fromString(ctx.settings.XreplInterruptInstrumentation.value)
611611
)
612612

613613
out.println(s"Added '$path' to classpath.")

0 commit comments

Comments
 (0)