Skip to content

Commit d528014

Browse files
aherlihydwijnand
authored andcommitted
find class names, need to add URL to loader
1 parent f8cd3ba commit d528014

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

compiler/src/dotty/tools/repl/ParseResult.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object Load {
5252
val command: String = ":load"
5353
}
5454

55-
/** `:require <path>` adds a jar to the classpath
55+
/** `:require <path>` adds a jar to the classpath
5656
*/
5757
case class Require(path: String) extends Command
5858
object Require {

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import scala.annotation.tailrec
3939
import scala.collection.mutable
4040
import scala.compiletime.uninitialized
4141
import scala.jdk.CollectionConverters.*
42+
import scala.tools.asm.ClassReader
4243
import scala.util.control.NonFatal
4344
import scala.util.Using
4445

@@ -511,16 +512,40 @@ class ReplDriver(settings: Array[String],
511512
}
512513

513514
case Require(path) =>
514-
val file = new JFile(path)
515-
if (file.exists) {
516-
// val contents = Using(scala.io.Source.fromFile(file, StandardCharsets.UTF_8.name))(_.mkString).get
517-
// run(contents)
518-
???
519-
}
520-
else {
521-
out.println(s"""Couldn't find file "${file.getCanonicalPath}"""")
515+
val f = new JFile(path)
516+
val jarFile = AbstractFile.getDirectory(path)
517+
if (!f.exists || jarFile == null)
518+
out.println(s"""Cannot add "$path" to classpath.""")
519+
state
520+
else
521+
def flatten(f: AbstractFile): Iterator[AbstractFile] =
522+
if (f.isClassContainer) f.iterator.flatMap(flatten)
523+
else Iterator(f)
524+
525+
val entries = flatten(jarFile)
526+
527+
def classNameOf(classFile: AbstractFile): String = {
528+
val input = classFile.input
529+
try {
530+
val reader = new ClassReader(input)
531+
reader.getClassName.replace('/', '.')
532+
} finally {
533+
input.close()
534+
}
535+
}
536+
537+
val clsl = rendering.classLoader()(using state.context)
538+
539+
// def alreadyDefined(clsName: String) = classLoader.tryToLoadClass(clsName).isDefined
540+
// val existingClass = entries.filter(_.ext.isClass).map(classNameOf).find(alreadyDefined)
541+
542+
// if (existingClass.nonEmpty) out.println(s"The path '$f' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
543+
// else {
544+
// intp.addUrlsToClassPath(f.toURI.toURL)
545+
// out.println(s"Added '$path' to classpath.")
546+
// out.println("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.classPathString))
547+
// }
522548
state
523-
}
524549

525550
case KindOf(expr) =>
526551
out.println(s"""The :kind command is not currently supported.""")

0 commit comments

Comments
 (0)