diff options
author | Lukas Rytz <lukas.rytz@gmail.com> | 2014-08-27 14:00:36 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@gmail.com> | 2014-09-10 00:05:11 +0200 |
commit | 35c53af7e3bbe19d50845e698c02a49d0a022409 (patch) | |
tree | 2d9dce67ed018e48b8342a1bd65773af301870bc /test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala | |
parent | 44b5c261a8e585c5747380895aa06c84f9d63f6c (diff) | |
download | scala-35c53af7e3bbe19d50845e698c02a49d0a022409.tar.gz scala-35c53af7e3bbe19d50845e698c02a49d0a022409.tar.bz2 scala-35c53af7e3bbe19d50845e698c02a49d0a022409.zip |
Tools to run the compiler in JUnit tests
Diffstat (limited to 'test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala')
-rw-r--r-- | test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala index 256dff85c3..8518d5c832 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala @@ -1,7 +1,11 @@ package scala.tools.nsc.backend.jvm +import scala.reflect.internal.util.BatchSourceFile +import scala.reflect.io.VirtualDirectory import scala.tools.asm.Opcodes import scala.tools.asm.tree.{AbstractInsnNode, LabelNode, ClassNode, MethodNode} +import scala.tools.cmd.CommandLineParser +import scala.tools.nsc.{Settings, Global} import scala.tools.partest.ASMConverters import scala.collection.JavaConverters._ @@ -24,4 +28,34 @@ object CodeGenTools { cls.methods.add(method) cls } + + private def resetOutput(compiler: Global): Unit = { + compiler.settings.outputDirs.setSingleOutput(new VirtualDirectory("(memory)", None)) + } + + def newCompiler(defaultArgs: String = "-usejavacp", extraArgs: String = ""): Global = { + val settings = new Settings() + val args = (CommandLineParser tokenize defaultArgs) ++ (CommandLineParser tokenize extraArgs) + settings.processArguments(args, processAll = true) + val compiler = new Global(settings) + resetOutput(compiler) + compiler + } + + def compile(compiler: Global = newCompiler())(code: String): List[(String, Array[Byte])] = { + compiler.reporter.reset() + resetOutput(compiler) + val run = new compiler.Run() + run.compileSources(List(new BatchSourceFile("unitTestSource.scala", code))) + val outDir = compiler.settings.outputDirs.getSingleOutput.get + (for (f <- outDir.iterator if !f.isDirectory) yield (f.name, f.toByteArray)).toList + } + + def compileClasses(compiler: Global = newCompiler())(code: String): List[ClassNode] = { + compile(compiler)(code).map(p => AsmUtils.readClass(p._2)).sortBy(_.name) + } + + def compileMethods(compiler: Global = newCompiler())(code: String): List[MethodNode] = { + compileClasses(compiler)(s"class C { $code }").head.methods.asScala.toList.filterNot(_.name == "<init>") + } } |