Skip to content

Commit 6c91941

Browse files
committed
find class names, need to add URL to loader
1 parent 321131a commit 6c91941

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

@@ -506,16 +507,40 @@ class ReplDriver(settings: Array[String],
506507
}
507508

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

520545
case TypeOf(expr) =>
521546
expr match {

0 commit comments

Comments
 (0)