I'm trying to write a Scala compiler plugin that rewrites method calls from instantiating Set
to instantiations of LinkedHashSet
. Unfortunately I can't find any working example that does this already. The following code fails with no-symbol does not have an owner
:
object DemoErasureComponent extends PluginComponent with TypingTransformers with Transform {
val global: DemoPlugin.this.global.type = DemoPlugin.this.global
import global._
override val runsAfter = List("erasure")
val phaseName = "rewrite-sets"
def newTransformer(unit: CompilationUnit) = new SetTransformer(unit)
class SetTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
override def transform(tree: Tree): Tree = tree match {
case a@Apply(r@Select(rcvr@Select(predef, set), name), args) if name.toString == "Set" =>
localTyper.typed(treeCopy.Apply(tree, Ident(newTermName("LinkedHashSet")), args))
case t => super.transform(tree)
}
}
For the record, I've found these resources so far:
- Scalaxy compiler plugin: https://github.com/ochafik/Scalaxy
- Boxer compiler plugin example: https://github.com/retronym/boxer
- outdated compiler plugin inside the Scala compiler: http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/docs/examples/plugintemplate/src/plugintemplate/TemplateTransformComponent.scala