@@ -10,6 +10,8 @@ import ast.tpd._
10
10
import java .io .{ ByteArrayInputStream , DataInputStream , File , IOException }
11
11
import java .nio
12
12
import java .lang .Integer .toHexString
13
+ import java .net .URLClassLoader
14
+
13
15
import scala .collection .{ mutable , immutable }
14
16
import scala .collection .mutable .{ ListBuffer , ArrayBuffer }
15
17
import scala .annotation .switch
@@ -784,25 +786,30 @@ class ClassfileParser(
784
786
if (scan(tpnme.TASTYATTR )) {
785
787
val attrLen = in.nextInt
786
788
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
796
790
case None =>
797
791
ctx.error(" Could not load TASTY from .tasty for virtual file " + classfile)
798
- Array .empty[ Byte ]
792
+ Array .empty
799
793
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
+ }
804
806
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
+ }
806
813
}
807
814
if (tastyBytes.nonEmpty)
808
815
return unpickleTASTY(tastyBytes)
0 commit comments