java - scala matchers works from jar not from sbt -


i have code blurb thats doing reflection on scala class files , looking annotations,something like

// create new class loader directory val cl = new urlclassloader(allpaths.toarray)   gettypenamesfrompath(sourcedir).map(s => {   val cls = cl.loadclass(s)   cls.getannotations.map(an =>     match {       case q: javax.ws.rs.path => println("found annotation")       case _ =>      }) }) 

this code prints "found annotation" when assembling in jar , running using java -jar, doesn't print when running sbt run.

sbt version 13.8, scala version 2.11.7

for completeness

 private def gettypenamesfrompath(file: file, currentpath: mutable.stack[string] = new mutable.stack[string]()): list[string] = {     if (file.isdirectory) {       var list = list[string]()       (f <- file.listfiles()) {         currentpath.push(f.getname)         list = list ++ gettypenamesfrompath(f, currentpath)       }       return list     }      if (currentpath.isempty)       throw new illegalargumentexception(file.getabsolutepath)      currentpath.pop()      if (file.getname.endswith(".class")) {       return list(currentpath.foldright("")((s, b) => if (b.isempty) s else b + "." + s) + "." + file.getname.stripsuffix(".class"))     }     return list()   } 

so turns out need 2 things...

  1. use urlclassloader scala.tools.nsc.util.scalaclassloader.urlclassloader, solve loading right classes.

  2. set fork := true in build.sbt, because once step #1, run issues class name clashes because sbt runs own class paths , own version of class loader, more on here http://www.scala-sbt.org/0.13.0/docs/detailed-topics/forking.html


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -