Skip to content

Commit 24a2ef9

Browse files
committed
SI-6502 Refactorings suggested by review
- Moves mergeUrlsIntoClassPath from Global into ClassPath - Revises and documents AbstractFile.getURL
1 parent 9e56c7a commit 24a2ef9

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -846,36 +846,12 @@ class Global(var currentSettings: Settings, var reporter: Reporter)
846846

847847
/** Extend classpath of `platform` and rescan updated packages. */
848848
def extendCompilerClassPath(urls: URL*): Unit = {
849-
val newClassPath = mergeUrlsIntoClassPath(platform, urls: _*)
849+
val newClassPath = platform.classPath.mergeUrlsIntoClassPath(urls: _*)
850850
platform.currentClassPath = Some(newClassPath)
851851
// Reload all specified jars into this compiler instance
852852
invalidateClassPathEntries(urls.map(_.getPath): _*)
853853
}
854854

855-
/** Merge classpath of `platform` and `urls` into merged classpath */
856-
def mergeUrlsIntoClassPath(platform: JavaPlatform, urls: URL*): MergedClassPath[AbstractFile] = {
857-
// Collect our new jars/directories and add them to the existing set of classpaths
858-
val prevEntries = platform.classPath match {
859-
case mcp: MergedClassPath[AbstractFile] => mcp.entries
860-
case cp: ClassPath[AbstractFile] => List(cp)
861-
}
862-
val allEntries = (prevEntries ++
863-
urls.map(url => platform.classPath.context.newClassPath(
864-
if (url.getProtocol == "file") {
865-
val f = new File(url.getPath)
866-
if (f.isDirectory) io.AbstractFile.getDirectory(f)
867-
else io.AbstractFile.getFile(f)
868-
} else {
869-
io.AbstractFile.getURL(url)
870-
}
871-
)
872-
)
873-
).distinct
874-
875-
// Combine all of our classpaths (old and new) into one merged classpath
876-
new MergedClassPath(allEntries, platform.classPath.context)
877-
}
878-
879855
// ------------ Invalidations ---------------------------------
880856

881857
/** Is given package class a system package class that cannot be invalidated?

src/compiler/scala/tools/nsc/util/ClassPath.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ abstract class ClassPath[T] {
197197
def packages: IndexedSeq[ClassPath[T]]
198198
def sourcepaths: IndexedSeq[AbstractFile]
199199

200+
/** The entries this classpath is composed of. In class `ClassPath` it's just the singleton list containing `this`.
201+
* Subclasses such as `MergedClassPath` typically return lists with more elements.
202+
*/
203+
def entries: IndexedSeq[ClassPath[T]] = IndexedSeq(this)
204+
205+
/** Merge classpath of `platform` and `urls` into merged classpath */
206+
def mergeUrlsIntoClassPath(urls: URL*): MergedClassPath[T] = {
207+
// Collect our new jars/directories and add them to the existing set of classpaths
208+
val allEntries =
209+
(entries ++
210+
urls.map(url => context.newClassPath(io.AbstractFile.getURL(url)))
211+
).distinct
212+
213+
// Combine all of our classpaths (old and new) into one merged classpath
214+
new MergedClassPath(allEntries, context)
215+
}
216+
200217
/**
201218
* Represents classes which can be loaded with a ClassfileLoader and/or SourcefileLoader.
202219
*/
@@ -322,7 +339,7 @@ extends MergedClassPath[T](original.entries map (e => subst getOrElse (e, e)), o
322339
* A classpath unifying multiple class- and sourcepath entries.
323340
*/
324341
class MergedClassPath[T](
325-
val entries: IndexedSeq[ClassPath[T]],
342+
override val entries: IndexedSeq[ClassPath[T]],
326343
val context: ClassPathContext[T])
327344
extends ClassPath[T] {
328345
def this(entries: TraversableOnce[ClassPath[T]], context: ClassPathContext[T]) =

src/reflect/scala/reflect/io/AbstractFile.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ object AbstractFile {
4848
else null
4949

5050
/**
51-
* If the specified URL exists and is a readable zip or jar archive,
52-
* returns an abstract directory backed by it. Otherwise, returns
53-
* `null`.
51+
* If the specified URL exists and is a regular file or a directory, returns an
52+
* abstract regular file or an abstract directory, respectively, backed by it.
53+
* Otherwise, returns `null`.
5454
*/
55-
def getURL(url: URL): AbstractFile = {
56-
if (url == null || !Path.isExtensionJarOrZip(url.getPath)) null
57-
else ZipArchive fromURL url
58-
}
55+
def getURL(url: URL): AbstractFile =
56+
if (url.getProtocol == "file") {
57+
val f = new java.io.File(url.getPath)
58+
if (f.isDirectory) getDirectory(f)
59+
else getFile(f)
60+
} else null
5961

6062
def getResources(url: URL): AbstractFile = ZipArchive fromManifestURL url
6163
}

0 commit comments

Comments
 (0)