Skip to content

Instantly share code, notes, and snippets.

@psilospore
Created December 21, 2018 16:23
Show Gist options
  • Save psilospore/1aaafd202d5048a1c4cd3e8ee52b4fea to your computer and use it in GitHub Desktop.
Save psilospore/1aaafd202d5048a1c4cd3e8ee52b4fea to your computer and use it in GitHub Desktop.
Look up usages of scala method using macros
import scala.language.experimental.macros
//WIP does not function
final class lookerUpper extends scala.annotation.StaticAnnotation {
import lookerUpper._
/**
* @param annottees list of untyped annottees
* @return
*/
def macroTransform(annottees: Any*): Any = macro lookerUpper_impl
}
object lookerUpper {
import reflect.macros.whitebox
def lookerUpper_impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
import c.universe._
println(s"entering impl $annottees")
annottees.headOption.foreach {
case vodd: ValOrDefDefApi =>{
println("hi 1");c.warning(vodd.pos, s"f${vodd.name}")
}
case _ => c.warning(c.enclosingPosition, "This is unexpected")
}
c.warning(c.enclosingPosition, "Didn't hit anything :(")
//I don't want to do anything to the tree actually
// Block(annottees) //Deprecated
q"""
..$annottees
"""
}
}
@lookerUpper() def hi = "hi"
@lookerUpper() val hi = "hi"
//no warnings or anything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment