@@ -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,31 @@ 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 = Array .newBuilder[Byte ]
799
+ tasty.sizeHint(stream.available())
800
+ while (stream.available() > 0 ) // TODO improve performance
801
+ tasty += stream.read().toByte
802
+ tasty.result()
803
+ } else {
804
+ ctx.error(s " Could not find $path in $jar" )
805
+ Array .empty
806
+ }
804
807
case _ =>
805
- readTastyForClass(classfile.jpath)
808
+ val plainFile = new PlainFile (io.File (classfile.jpath).changeExtension(" tasty" ))
809
+ if (plainFile.exists) plainFile.toByteArray
810
+ else {
811
+ ctx.error(" Could not find " + plainFile)
812
+ Array .empty
813
+ }
806
814
}
807
815
if (tastyBytes.nonEmpty)
808
816
return unpickleTASTY(tastyBytes)
0 commit comments