Skip to content

Commit a04c07a

Browse files
committed
cleanup
1 parent 817e279 commit a04c07a

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

repl-embedded/src/scala/tools/repl/EmbeddedReplMain.scala

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,21 @@ class UnshadingClassLoader(parent: ClassLoader) extends ClassLoader(parent) {
2525
!name.startsWith(SHADED_PREFIX)
2626

2727
if (shouldUnshade) {
28-
// Try to find the shaded version
29-
val shadedName = SHADED_PREFIX + name
30-
31-
// First check if we've already loaded this class
3228
val loaded = findLoadedClass(name)
3329
if (loaded != null) return loaded
3430

3531
try {
3632
// Load the shaded class bytes from parent
37-
val resourceName = shadedName.replace('.', '/') + ".class"
38-
val is = getParent.getResourceAsStream(resourceName)
33+
val is = getParent.getResourceAsStream((SHADED_PREFIX + name).replace('.', '/') + ".class")
3934

4035
if (is != null) {
4136
try {
42-
// Read the class bytes
43-
val bytes = readAllBytes(is)
44-
37+
val bytes = is.readAllBytes()
4538
// Define the class with the unshaded name
4639
val clazz = defineClass(name, bytes, 0, bytes.length)
4740
if (resolve) resolveClass(clazz)
4841
return clazz
49-
} finally {
50-
is.close()
51-
}
42+
} finally is.close()
5243
}
5344
} catch {
5445
case _: Exception => // Fall through to parent
@@ -58,18 +49,6 @@ class UnshadingClassLoader(parent: ClassLoader) extends ClassLoader(parent) {
5849
// For everything else (scala.* and already shaded classes), delegate to parent
5950
super.loadClass(name, resolve)
6051
}
61-
62-
private def readAllBytes(is: InputStream): Array[Byte] = {
63-
val buffer = new Array[Byte](8192)
64-
var bytesRead = 0
65-
val baos = new java.io.ByteArrayOutputStream()
66-
67-
while ({bytesRead = is.read(buffer); bytesRead != -1}) {
68-
baos.write(buffer, 0, bytesRead)
69-
}
70-
71-
baos.toByteArray()
72-
}
7352
}
7453

7554
/**
@@ -82,17 +61,13 @@ object EmbeddedReplMain {
8261
def main(args: Array[String]): Unit = {
8362
// Get the location of the current jar to use as classpath
8463
val codeSource = getClass.getProtectionDomain.getCodeSource
85-
val jarPath = if (codeSource != null) {
86-
val location = codeSource.getLocation
87-
if (location.getProtocol == "file") {
88-
new java.io.File(location.toURI).getAbsolutePath
89-
} else {
90-
location.toString
64+
val jarPath =
65+
if (codeSource == null) System.getProperty("java.class.path") // Fallback: try to extract from classpath
66+
else {
67+
val location = codeSource.getLocation
68+
if (location.getProtocol == "file") new java.io.File(location.toURI).getAbsolutePath
69+
else location.toString
9170
}
92-
} else {
93-
// Fallback: try to extract from classpath
94-
System.getProperty("java.class.path")
95-
}
9671

9772
// Add -classpath argument pointing to the shaded jar itself
9873
// This allows the ReplDriver's compiler to find scala.* classes
@@ -104,31 +79,16 @@ object EmbeddedReplMain {
10479

10580
// Create the unshading classloader with the current classloader as parent
10681
// This ensures it has access to all dependencies in the shaded jar
107-
val unshadingClassLoader = new UnshadingClassLoader(
108-
getClass.getClassLoader // Use current classloader to access all dependencies
109-
)
82+
val unshadingClassLoader = new UnshadingClassLoader(getClass.getClassLoader)
11083

111-
// Load the ReplDriver class through the unshading classloader
11284
val replDriverClass = unshadingClassLoader.loadClass("dotty.tools.repl.ReplDriver")
113-
114-
// Get the constructor: ReplDriver(Array[String], PrintStream, Option[ClassLoader], String)
115-
val constructor = replDriverClass.getConstructors()(0)
116-
117-
// Create an Option[ClassLoader] containing the external classloader
118-
val scalaOptionClass = unshadingClassLoader.loadClass("scala.Option")
119-
val scalaOptionModule = unshadingClassLoader.loadClass("scala.Option$")
120-
val someMethod = scalaOptionModule.getField("MODULE$").get(null)
121-
.asInstanceOf[Object].getClass.getMethod("apply", classOf[Object])
122-
val classLoaderOption = someMethod.invoke(
123-
scalaOptionModule.getField("MODULE$").get(null),
124-
getClass.getClassLoader // Pass the external classloader
125-
)
85+
val constructor = replDriverClass.getConstructors().head
12686

12787
// Create the ReplDriver instance with classpath argument
12888
val replDriver = constructor.newInstance(
12989
argsWithClasspath, // settings: Array[String] (now includes -classpath)
13090
System.out, // out: PrintStream
131-
classLoaderOption, // classLoader: Option[ClassLoader]
91+
Option(getClass.getClassLoader), // classLoader: Option[ClassLoader]
13292
"" // extraPredef: String
13393
)
13494

0 commit comments

Comments
 (0)