Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import tools.nsc.plugins.{Plugin, PluginComponent}
import tools.nsc.Global
import tools.nsc.transform.{TypingTransformers, InfoTransform, Transform}
import tools.nsc.symtab.Flags
class MyPlugin(val global: Global) extends Plugin {
val name = "annotations-inject-implicit"
val description = "generates code which adds an implicit parameter of type BindingModule when an AutoInjectable trait is mixed in"
val components = List[PluginComponent](AnnotationsInjectComponent)
private object AnnotationsInjectComponent extends PluginComponent with Transform with TypingTransformers {
val global: =
import global._
val runsAfter = List[String]("parser")
override val runsBefore = List[String]("namer")
val phaseName =
def newTransformer(unit: CompilationUnit) = new AnnotationsInjectTransformer (unit)
val autoInjectable = "Serializable"
val bindingModule = "bindingModule"
val constructorMethod = "<init>"
val bindingModuleType = Select(Select(Ident(newTermName("_root_")),newTermName("app")),newTermName("Module"))
class AnnotationsInjectTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
def preTransform(tree: Tree): Tree = {
import Flags._
tree match {
case cd @ ClassDef(modifiers, name, tparams, classBody)
if ! => {
log("AutoInjecting class %s".format(name))
val newParents = classBody.parents
val body = {
case item @ DefDef(modifiers, termname, tparams, vparamss, tpt, rhs)
if termname.toString != constructorMethod =>
val newMods = Modifiers(IMPLICIT | PARAM | PARAMACCESSOR)
val newImplicit = new ValDef(newMods, bindingModule, bindingModuleType, EmptyTree)
val newParams = vparamss ::: List(List(newImplicit))
val newTree = treeCopy.DefDef(item, modifiers, termname, tparams, newParams, tpt, rhs)
case t => t
val newImpVal = ValDef(Modifiers(IMPLICIT | PARAMACCESSOR), bindingModule, bindingModuleType, EmptyTree)
treeCopy.ClassDef(cd, modifiers, name, tparams, Template(newParents, classBody.self, newImpVal :: body))
case t => t
override def transform (tree:Tree):Tree = {
val t = preTransform(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment