I´m using Macros, and I would like to check in compilation time, if an instance class created contains all attribute passed in the constructor, or if one of them are null, and if that's the case fail in compilation time instead in runtime.
I´ve been reading a lot of blogs about it and I found none regarding this particular scenario, so maybe is not doable in compilation time.
If it's possible and someone know a blog, paper to read about how to do it with AST I would appreciate.
Here my builder class
actorBuilder = F2eActorBuilder
.withApplicationConf(new File(applicationContext))
.withAkkaContext( "akkaContext.xml")
.withApplicationContext("applicationContext.xml")
.build()
I want to check actorBuilder once is defined, and since is a DSL and some with* might not being invoked I want to check in compilation time if one of those attributes are set as null.
And here my Macro code
import scala.language.experimental.macros
def valid(action: F2eActorBuilder): F2eActorBuilder = macro checkActionImpl
def checkActionImpl(c: blackbox.Context)(action: c.Tree): c.Tree = {
import c.universe._
def isValidAction(s: F2eActorBuilder): Boolean = checkMessage(s)
action match {
case _tree@Literal(Constant(s: F2eActorBuilder)) if isValidAction(s) => _tree
case _tree@Literal(Constant(s: F2eActorBuilder)) => c.abort(c.enclosingPosition, getErrorMessage(s))
}
}
def checkMessage(action: F2eActorBuilder): Boolean = {
action.akkaContextPath != null
}
def getErrorMessage(action: F2eActorBuilder): String = {
s"Some mandatory attributes are marked as null"
}