Skip to content

Commit 6c92bbf

Browse files
committed
Tweaks and classBytes
1 parent 82f9543 commit 6c92bbf

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed
Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
package dotty.tools.repl
22

3-
import org.junit.Assert._
3+
import org.junit.Assert.*
44
import org.junit.Test
55

66
class AbstractFileClassLoaderTest:
77

88
import dotty.tools.io.{AbstractFile, VirtualDirectory}
9-
import scala.io.Source
10-
import scala.io.Codec, Codec.UTF8
11-
import java.io.{Closeable, InputStream}
9+
import scala.collection.mutable.ArrayBuffer
10+
import scala.io.{Codec, Source}, Codec.UTF8
11+
import java.io.{BufferedInputStream, Closeable, InputStream}
1212
import java.net.{URLClassLoader, URL}
1313

14-
implicit def `we love utf8`: Codec = UTF8
14+
given `we love utf8`: Codec = UTF8
1515

16-
/** Call a function on something Closeable, finally closing it. */
1716
def closing[T <: Closeable, U](stream: T)(f: T => U): U = try f(stream) finally stream.close()
1817

1918
extension (f: AbstractFile) def writeContent(s: String): Unit = closing(f.bufferedOutput)(_.write(s.getBytes(UTF8.charSet)))
20-
def slurp(inputStream: => InputStream)(implicit codec: Codec): String =
21-
val src = Source.fromInputStream(inputStream)(codec)
22-
try src.mkString finally src.close() // Always Be Closing
19+
def slurp(inputStream: => InputStream)(implicit codec: Codec): String = closing(Source.fromInputStream(inputStream)(codec))(_.mkString)
2320
def slurp(url: URL)(implicit codec: Codec): String = slurp(url.openStream())
2421

22+
extension (input: InputStream) def bytes: Array[Byte] =
23+
val bis = new BufferedInputStream(input)
24+
val it = Iterator.continually(bis.read()).takeWhile(_ != -1).map(_.toByte)
25+
new ArrayBuffer[Byte]().addAll(it).toArray
26+
27+
// cf ScalaClassLoader#classBytes
28+
extension (loader: ClassLoader)
29+
// An InputStream representing the given class name, or null if not found.
30+
def classAsStream(className: String) = loader.getResourceAsStream {
31+
if className.endsWith(".class") then className
32+
else s"${className.replace('.', '/')}.class" // classNameToPath
33+
}
34+
// The actual bytes for a class file, or an empty array if it can't be found.
35+
def classBytes(className: String): Array[Byte] = classAsStream(className) match
36+
case null => Array()
37+
case stream => stream.bytes
38+
2539
val NoClassLoader: ClassLoader = null
2640

2741
// virtual dir "fuzz" and "fuzz/buzz/booz.class"
@@ -31,24 +45,21 @@ class AbstractFileClassLoaderTest:
3145
val booz = buzz.fileNamed("booz.class")
3246
(fuzz, booz)
3347

34-
@Test
35-
def afclGetsParent(): Unit =
48+
@Test def afclGetsParent(): Unit =
3649
val p = new URLClassLoader(Array.empty[URL])
3750
val d = new VirtualDirectory("vd", None)
3851
val x = new AbstractFileClassLoader(d, p)
3952
assertSame(p, x.getParent)
4053

41-
@Test
42-
def afclGetsResource(): Unit =
54+
@Test def afclGetsResource(): Unit =
4355
val (fuzz, booz) = fuzzBuzzBooz
4456
booz.writeContent("hello, world")
4557
val sut = new AbstractFileClassLoader(fuzz, NoClassLoader)
4658
val res = sut.getResource("buzz/booz.class")
4759
assertNotNull("Find buzz/booz.class", res)
4860
assertEquals("hello, world", slurp(res))
4961

50-
@Test
51-
def afclGetsResourceFromParent(): Unit =
62+
@Test def afclGetsResourceFromParent(): Unit =
5263
val (fuzz, booz) = fuzzBuzzBooz
5364
val (fuzz_, booz_) = fuzzBuzzBooz
5465
booz.writeContent("hello, world")
@@ -59,8 +70,7 @@ class AbstractFileClassLoaderTest:
5970
assertNotNull("Find buzz/booz.class", res)
6071
assertEquals("hello, world", slurp(res))
6172

62-
@Test
63-
def afclGetsResourceInDefaultPackage(): Unit =
73+
@Test def afclGetsResourceInDefaultPackage(): Unit =
6474
val fuzz = new VirtualDirectory("fuzz", None)
6575
val booz = fuzz.fileNamed("booz.class")
6676
val bass = fuzz.fileNamed("bass")
@@ -73,8 +83,7 @@ class AbstractFileClassLoaderTest:
7383
assertEquals("lo tone", slurp(sut.getResource("bass")))
7484

7585
// scala/bug#8843
76-
@Test
77-
def afclGetsResources(): Unit =
86+
@Test def afclGetsResources(): Unit =
7887
val (fuzz, booz) = fuzzBuzzBooz
7988
booz.writeContent("hello, world")
8089
val sut = new AbstractFileClassLoader(fuzz, NoClassLoader)
@@ -83,8 +92,7 @@ class AbstractFileClassLoaderTest:
8392
assertEquals("hello, world", slurp(e.nextElement))
8493
assertFalse(e.hasMoreElements)
8594

86-
@Test
87-
def afclGetsResourcesFromParent(): Unit =
95+
@Test def afclGetsResourcesFromParent(): Unit =
8896
val (fuzz, booz) = fuzzBuzzBooz
8997
val (fuzz_, booz_) = fuzzBuzzBooz
9098
booz.writeContent("hello, world")
@@ -98,36 +106,29 @@ class AbstractFileClassLoaderTest:
98106
assertEquals("hello, world_", slurp(e.nextElement))
99107
assertFalse(e.hasMoreElements)
100108

101-
@Test
102-
def afclGetsResourceAsStream(): Unit =
109+
@Test def afclGetsResourceAsStream(): Unit =
103110
val (fuzz, booz) = fuzzBuzzBooz
104111
booz.writeContent("hello, world")
105112
val x = new AbstractFileClassLoader(fuzz, NoClassLoader)
106113
val r = x.getResourceAsStream("buzz/booz.class")
107114
assertNotNull(r)
108115
assertEquals("hello, world", closing(r)(is => Source.fromInputStream(is).mkString))
109116

110-
/*
111-
@Test
112-
def afclGetsClassBytes(): Unit = {
117+
@Test def afclGetsClassBytes(): Unit =
113118
val (fuzz, booz) = fuzzBuzzBooz
114-
booz writeContent "hello, world"
115-
val x = new AbstractFileClassLoader(fuzz, NoClassLoader)
116-
val b = x.classBytes("buzz/booz.class")
119+
booz.writeContent("hello, world")
120+
val sut = new AbstractFileClassLoader(fuzz, NoClassLoader)
121+
val b = sut.classBytes("buzz/booz.class")
117122
assertEquals("hello, world", new String(b, UTF8.charSet))
118-
}
119123

120-
@Test
121-
def afclGetsClassBytesFromParent(): Unit = {
124+
@Test def afclGetsClassBytesFromParent(): Unit =
122125
val (fuzz, booz) = fuzzBuzzBooz
123126
val (fuzz_, booz_) = fuzzBuzzBooz
124-
booz writeContent "hello, world"
125-
booz_ writeContent "hello, world_"
127+
booz.writeContent("hello, world")
128+
booz_.writeContent("hello, world_")
126129

127130
val p = new AbstractFileClassLoader(fuzz, NoClassLoader)
128-
val x = new AbstractFileClassLoader(fuzz_, p)
129-
val b = x.classBytes("buzz/booz.class")
131+
val sut = new AbstractFileClassLoader(fuzz_, p)
132+
val b = sut.classBytes("buzz/booz.class")
130133
assertEquals("hello, world", new String(b, UTF8.charSet))
131-
}
132-
*/
133134
end AbstractFileClassLoaderTest

0 commit comments

Comments
 (0)