Skip to content

Commit 9f76a7b

Browse files
committed
DottyBytecodeTests#checkBCode: support .java files
Will be used in the next commit.
1 parent 69b6bb9 commit 9f76a7b

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,21 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
246246
}
247247
}
248248

249-
def compileFromStrings(sourceCodes: String*): Unit = {
250-
val sourceFiles = sourceCodes.map {sourceCode =>
251-
val virtualFile = new VirtualFile(s"compileFromString-${java.util.UUID.randomUUID().toString}")
249+
def compileFromStrings(scalaSources: List[String], javaSources: List[String] = Nil): Unit = {
250+
def sourceFile(source: String, isJava: Boolean): SourceFile = {
251+
val uuid = java.util.UUID.randomUUID().toString
252+
val ext = if (isJava) ".java" else ".scala"
253+
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
252254
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
253-
writer.write(sourceCode)
255+
writer.write(source)
254256
writer.close()
255257
new SourceFile(virtualFile, Codec.UTF8)
256258
}
257-
compileSources(sourceFiles.toList)
259+
val sources =
260+
scalaSources.map(sourceFile(_, isJava = false)) ++
261+
javaSources.map(sourceFile(_, isJava = true))
262+
263+
compileSources(sources)
258264
}
259265

260266
/** Print summary; return # of errors encountered */

compiler/test/dotty/tools/DottyTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ trait DottyTest extends ContextEscapeDetection {
6565
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Context = {
6666
val c = compilerWithChecker(checkAfterPhase)(assertion)
6767
val run = c.newRun
68-
run.compileFromStrings(source)
68+
run.compileFromStrings(List(source))
6969
run.runContext
7070
}
7171

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTest.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ trait DottyBytecodeTest {
5252
ctx0.setSetting(ctx0.settings.outputDir, outputDir)
5353
}
5454

55+
def checkBCode(scalaSource: String)(checkOutput: AbstractFile => Unit): Unit =
56+
checkBCode(List(scalaSource))(checkOutput)
57+
5558
/** Checks source code from raw strings */
56-
def checkBCode(sources: String*)(checkOutput: AbstractFile => Unit): Unit = {
59+
def checkBCode(scalaSources: List[String], javaSources: List[String] = Nil)(checkOutput: AbstractFile => Unit): Unit = {
5760
implicit val ctx: Context = initCtx
5861

5962
val compiler = new Compiler
6063
val run = compiler.newRun
61-
compiler.newRun.compileFromStrings(sources: _*)
64+
compiler.newRun.compileFromStrings(scalaSources, javaSources)
6265

6366
checkOutput(ctx.settings.outputDir.value)
6467
}

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ class TestBCode extends DottyBytecodeTest {
762762
|}
763763
""".stripMargin
764764

765-
checkBCode(sourceA, sourceB) { dir =>
765+
checkBCode(List(sourceA, sourceB)) { dir =>
766766
val clsNodeA = loadClassNode(dir.lookupName("A.class", directory = false).input, skipDebugInfo = false)
767767
val clsNodeB = loadClassNode(dir.lookupName("B.class", directory = false).input, skipDebugInfo = false)
768768
val a1 = getMethod(clsNodeA, "a1")

0 commit comments

Comments
 (0)