Skip to content

Commit dd7a295

Browse files
committed
Use classloader instead of jar filesystem to load tasty
1 parent c5d2c6b commit dd7a295

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import ast.tpd._
1010
import java.io.{ ByteArrayInputStream, DataInputStream, File, IOException }
1111
import java.nio
1212
import java.lang.Integer.toHexString
13+
import java.net.URLClassLoader
14+
1315
import scala.collection.{ mutable, immutable }
1416
import scala.collection.mutable.{ ListBuffer, ArrayBuffer }
1517
import scala.annotation.switch
@@ -784,25 +786,30 @@ class ClassfileParser(
784786
if (scan(tpnme.TASTYATTR)) {
785787
val attrLen = in.nextInt
786788
if (attrLen == 0) { // A tasty attribute implies the existence of the .tasty file
787-
def readTastyForClass(jpath: nio.file.Path): Array[Byte] = {
788-
val plainFile = new PlainFile(io.File(jpath).changeExtension("tasty"))
789-
if (plainFile.exists) plainFile.toByteArray
790-
else {
791-
ctx.error("Could not find " + plainFile)
792-
Array.empty
793-
}
794-
}
795-
val tastyBytes = classfile.underlyingSource match { // TODO: simplify when #3552 is fixed
789+
val tastyBytes: Array[Byte] = classfile.underlyingSource match { // TODO: simplify when #3552 is fixed
796790
case None =>
797791
ctx.error("Could not load TASTY from .tasty for virtual file " + classfile)
798-
Array.empty[Byte]
792+
Array.empty
799793
case Some(jar: ZipArchive) => // We are in a jar
800-
val jarFile = JarArchive.open(io.File(jar.jpath))
801-
readTastyForClass(jarFile.jpath.resolve(classfile.path))
802-
// Do not close the file system as some else might use it later. Once closed it cannot be re-opened.
803-
// TODO find a way to safly close the file system or ose some other abstraction
794+
val cl = new URLClassLoader(Array(jar.jpath.toUri.toURL))
795+
val path = classfile.path.stripSuffix(".class") + ".tasty"
796+
val stream = cl.getResourceAsStream(path)
797+
if (stream != null) {
798+
val tasty = new Array[Byte](stream.available())
799+
val read = stream.read(tasty)
800+
assert(tasty.length == read)
801+
tasty
802+
} else {
803+
ctx.error(s"Could not find $path in $jar")
804+
Array.empty
805+
}
804806
case _ =>
805-
readTastyForClass(classfile.jpath)
807+
val plainFile = new PlainFile(io.File(classfile.jpath).changeExtension("tasty"))
808+
if (plainFile.exists) plainFile.toByteArray
809+
else {
810+
ctx.error("Could not find " + plainFile)
811+
Array.empty
812+
}
806813
}
807814
if (tastyBytes.nonEmpty)
808815
return unpickleTASTY(tastyBytes)

0 commit comments

Comments
 (0)