Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Created August 16, 2010 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwmcadams/527992 to your computer and use it in GitHub Desktop.
Save bwmcadams/527992 to your computer and use it in GitHub Desktop.
// Test Method
@Stache("unitTest", Some(3600)) def foo(x: Int) = {
println("Multiplying X (%s) by 2".format(x))
x * 2
}
// Compiler steps
var block = tree.find( _.isInstanceOf[Block] ) orElse tree.find( _.isInstanceOf[Apply] ) getOrElse(throw new IllegalArgumentException("Cannot find method body for Stache annotated DefDef '%s'. Was looking for either a code block or a one liner. Is method abstract or something else boneheaded?".format(dDef.name)))
val base: Tree = treeCopy.Function(tree, Nil, block)
UnTyper.traverse(base)
val fn = Apply(
Apply(
TypeApply(
Select(
Ident(definitions.getModule("com.novus.stache.StacheManager")),
newTermName("apply")
),
List(TypeTree(sym.info.resultType.normalize))
),
callArgs),
List(base)
)
localTyper.typed { DEF(sym) === fn } // returns (end of block)
// At this point it's simply passed as a function parameter into the new method body.
// Generated compiler code
StacheManager.apply[Int]("unitTest", scala.Some.apply[Long](3600L), "<root>.com.novus.stache.plugin.test.TestMethods.foo", "_foo", x)((() => {
scala.this.Predef.println(scala.this.Predef.augmentString("Multiplying X (%s) by 2").format(x));
x.$times(2)
}))
// Compiler error is scala.tools.nsc.symtab.Types$TypeError: scala is not an enclosing class referring to scala.this.Predef...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment