Skip to content

Instantly share code, notes, and snippets.

@papamitra
Created June 1, 2011 13:03
Show Gist options
  • Save papamitra/1002245 to your computer and use it in GitHub Desktop.
Save papamitra/1002245 to your computer and use it in GitHub Desktop.
package localhost
import scala.tools.nsc
import nsc.Global
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent
import nsc.transform.Transform
class LazyTracePlugin(val global: Global) extends Plugin {
import global._
import definitions._
val name = "lazy-trace"
val description = "print stack trace in creating lazy value"
val components = List[PluginComponent](LazyTraceComponent)
def mkTraceTree =
Apply( // sym=<none>, tpe=null
Select( // sym=<none>, sym.tpe=<notype>, tpe=null
Select( // sym=<none>, sym.tpe=<notype>, tpe=null
Select( // sym=<none>, sym.tpe=<notype>, tpe=null
Ident("Thread"), // sym=<none>, sym.tpe=<notype>, tpe=null,
"currentThread"),
"getStackTrace"),
"foreach"),
List( // 1 arguments(s)
Ident("println") // sym=<none>, sym.tpe=<notype>, tpe=null
)
)
private object LazyTraceComponent extends PluginComponent with Transform{
val global: LazyTracePlugin.this.global.type = LazyTracePlugin.this.global
val runsAfter = List[String]("parser")
val phaseName = LazyTracePlugin.this.name
def newTransformer(unit: global.CompilationUnit) = LazyTraceTransformer
object LazyTraceTransformer extends global.Transformer {
override def transform(tree: global.Tree) = {
tree match {
case ValDef(mods, name, tpe, rhs) if mods.isLazy=>
ValDef(mods, name, tpe, Block(mkTraceTree,rhs))
case t => super.transform(t)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment