@@ -16,25 +16,26 @@ class UnshadingClassLoader(parent: ClassLoader) extends ClassLoader(parent) {
1616
1717 private val SHADED_PREFIX = " dotty.tools.repl.shaded."
1818
19- // Packages that were shaded
2019 private val SHADED_PACKAGES = Seq (" dotty." , " org." , " com." , " io." , " coursier." , " coursierapi." , " dependency." , " pprint." , " fansi." , " sourcecode." , " xsbti." )
2120
22- // Packages that are NOT shaded (even though they match SHADED_PACKAGES patterns)
2321 private val UNSHADED_PACKAGES = Seq (" scala." , " scala.tools.repl." , " org.jline." )
2422
25- override def loadClass (name : String , resolve : Boolean ): Class [? ] = {
26- // Check if this is a class from a package we shaded (and not already in the shaded package)
27- // Also exclude packages that are explicitly not shaded
28- val shouldUnshade = SHADED_PACKAGES .exists(pkg => name.startsWith(pkg)) &&
29- ! name.startsWith(SHADED_PREFIX ) &&
30- ! UNSHADED_PACKAGES .exists(pkg => name.startsWith(pkg))
23+ /** Check if a class/resource name should be loaded from the shaded location */
24+ private def shouldUnshade (name : String ): Boolean = {
25+ SHADED_PACKAGES .exists(pkg => name.startsWith(pkg)) &&
26+ ! name.startsWith(SHADED_PREFIX ) &&
27+ ! UNSHADED_PACKAGES .exists(pkg => name.startsWith(pkg)) ||
28+ name.startsWith(" scala.tools.asm" )
29+ }
3130
32- if (shouldUnshade) {
31+ override def loadClass (name : String , resolve : Boolean ): Class [? ] = {
32+ if (shouldUnshade(name)) {
3333 val loaded = findLoadedClass(name)
3434 if (loaded != null ) return loaded
3535
3636 try {
37- val is = getParent.getResourceAsStream((SHADED_PREFIX + name).replace('.' , '/' ) + " .class" )
37+ val shadedPath = (SHADED_PREFIX + name).replace('.' , '/' ) + " .class"
38+ val is = getParent.getResourceAsStream(shadedPath)
3839
3940 if (is != null ) {
4041 try {
@@ -49,9 +50,20 @@ class UnshadingClassLoader(parent: ClassLoader) extends ClassLoader(parent) {
4950 }
5051 }
5152
52- // For everything else (scala.* and already shaded classes), delegate to parent
5353 super .loadClass(name, resolve)
5454 }
55+
56+ override def getResourceAsStream (name : String ): InputStream | Null = {
57+ val nameAsDots = name.replace('/' , '.' )
58+
59+ if (shouldUnshade(nameAsDots)) {
60+ val shadedPath = SHADED_PREFIX .replace('.' , '/' ) + name
61+ val shadedStream = super .getResourceAsStream(shadedPath)
62+ if (shadedStream != null ) return shadedStream
63+ }
64+
65+ super .getResourceAsStream(name)
66+ }
5567}
5668
5769/**
@@ -64,23 +76,22 @@ object EmbeddedReplMain {
6476 def main (args : Array [String ]): Unit = {
6577 val argsWithClasspath =
6678 if (args.exists(arg => arg == " -classpath" || arg == " -cp" )) args
67- else Array (" -classpath" , System .getProperty(" java.class.path" )) ++ args
79+ else Array (" -classpath" , System .getProperty(" java.class.path" )) ++ args
6880
6981 val unshadingClassLoader = new UnshadingClassLoader (getClass.getClassLoader)
70- try {
7182
72- val replDriverClass = unshadingClassLoader.loadClass(" dotty.tools.repl.ReplDriver" )
73- val constructor = replDriverClass.getConstructors().head
83+ val replDriverClass = unshadingClassLoader.loadClass(" dotty.tools.repl.ReplDriver" )
84+ val constructor = replDriverClass.getConstructors().head
85+
86+ // Create the ReplDriver instance with classpath argument
87+ val replDriver = constructor.newInstance(
88+ argsWithClasspath, // settings: Array[String] (now includes -classpath)
89+ System .out, // out: PrintStream
90+ Option (getClass.getClassLoader), // classLoader: Option[ClassLoader]
91+ " " // extraPredef: String
92+ )
7493
75- // Create the ReplDriver instance with classpath argument
76- val replDriver = constructor.newInstance(
77- argsWithClasspath, // settings: Array[String] (now includes -classpath)
78- System .out, // out: PrintStream
79- Option (getClass.getClassLoader), // classLoader: Option[ClassLoader]
80- " " // extraPredef: String
81- )
94+ replDriverClass.getMethod(" tryRunning" ).invoke(replDriver)
8295
83- replDriverClass.getMethod(" tryRunning" ).invoke(replDriver)
84- }finally unshadingClassLoader.close
8596 }
8697}
0 commit comments