Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala run time code compilation

Tags:

scala

I am trying to port my java code to pure scala, so would appreciate any help in this regard.

The code below works by first translating my business logic into java code. Here I'm using freemarker template to do template based code generation. After file creation I then use the java comiler to compile the code and create a jar file, which is persisted in a temporary directory

I am currently using the javax.tools.* package that provides runtime compilation. What is the Scala equiavalent to that approach? I'd like to generate pure Scala code using freemarker templates and then run Scala compilation to create a jar file.

Below is sample Java code I am using to make this happen.

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);
    StringBuilder builder = new StringBuilder();
    builder.append(service.getConfig().getProp("coreLib"));
    builder.append(";" +result.getCodeContext().getOmClasspath());
    builder.append(";" +jarBuilder.toString());
    builder.append(";" +service.getConfig().getProp("tempCodeGen"));
    String[] compileOptions = new String[]{"-d", result.getCodeContext().getOmClasspath(),"-cp",builder.toString()} ;
    Iterable<String> compilationOptionss = Arrays.asList(compileOptions);
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, compilationOptionss, null, compilationUnits) ;
    boolean status = compilerTask.call();
like image 203
GammaVega Avatar asked Jul 23 '26 18:07

GammaVega


1 Answers

Here's some methods from my own code to compile a project and package it into a jar. Its very far from polished, or properly commented, but hopefully it will indicate where you need to start. I don't think you need to use String Builder as this is not performance critical:

def buildAll(name: String, projDir: String, mainClass: String = ""): Unit =
{
  import scala.tools.nsc.{Settings,Global}
  val relSrc : List[String] = List()
  val maniVersion = "None"
  def targDir: String = projDir + "/targ"
  def srcDir: String = projDir + "/src"
  def srcDirs: List[String] = srcDir :: relSrc

  import java.io._
  val sings = new scala.tools.nsc.Settings     
  new File(targDir).mkdir 
  sings.outputDirs.setSingleOutput(targDir.toString)     
  val comp = new Global(sings)      
  val crun: comp.Run  = new comp.Run
  def getList(fName: String): List[String] =
  {
     println("starting getList " + fName)
     val file = new File(fName)
     if (file.isDirectory) file.listFiles.toList.flatMap(i => getList(fName + "/" + i.getName))
     else List(fName)
  }  

  crun.compile(srcDirs.flatMap(i => getList(i))) 
  import sys.process._
  ("cp -r /sdat/projects/ScalaLibs/scala " + targDir + "/scala").!

  import java.util.jar._
  val manif = new Manifest
  val mf = manif.getMainAttributes
  mf.put(Attributes.Name.MANIFEST_VERSION, maniVersion)
  if (mainClass != "") mf.put(Attributes.Name.MAIN_CLASS, mainClass)
  val jarName = name + ".jar"
  val jarOut: JarOutputStream = new JarOutputStream(new FileOutputStream(projDir + "/" + jarName), manif)  
  AddAllToJar(targDir, jarOut)      
  jarOut.close   
}   

def addToJar(jarOut: JarOutputStream, file: File, reldir: String): Unit =
{
  val fName = reldir + file.getName         
  val fNameMod = if (file.isDirectory) fName + "/" else fName
  val entry = new JarEntry(fNameMod)
  entry.setTime(file.lastModified)         
  jarOut.putNextEntry(entry)
  if (file.isDirectory)
  {
     jarOut.closeEntry
     file.listFiles.foreach(i => addToJar(jarOut, i, fName + "/"))
  }
  else
  {         
     var buf = new Array[Byte](1024)
     val in = new FileInputStream(file)
     Stream.continually(in.read(buf)).takeWhile(_ != -1).foreach(jarOut.write(buf, 0, _))
     in.close
     jarOut.closeEntry()
  }         
}
def AddAllToJar(targDir: String, jarOut: JarOutputStream): Unit =
  new java.io.File(targDir).listFiles.foreach(i => addToJar(jarOut, i, ""))

You need to add the Scala Compiler to the build path. The Scala compiler takes a list of sourcefiles and produces the compiled class files in the directory set in the output directory. Getting to grips with the full capabilities of the compiler is a major task though.

like image 118
Rich Oliver Avatar answered Jul 25 '26 08:07

Rich Oliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!