Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 12

[ 335 ]

}

def blockStatement = new BlockStatement(
eventStatements as Statement [],
new VariableScope()
)

PageAssignmentTransformer pageAssignmentTransformer =
new PageAssignmentTransformer(model, sourceUnit)
blockStatement.visit(pageAssignmentTransformer)

def closureExpression = new ClosureExpression(
[
new Parameter(
ClassHelper.make(Object, false),
"event")
] as Parameter[],
blockStatement
)
closureExpression.variableScope = new VariableScope()

def closurePropertyNode = new PropertyNode(
"${event}",
Modifier.PUBLIC,
ClassHelper.DYNAMIC_TYPE,
ClassHelper.make("${page}Page"),
closureExpression,
null,
null
)

stateClassNode[1].addProperty(closurePropertyNode)
}

When a page is assigned in the DSL, we need to transform this assignment from a
constant string assignment to a constructor call for the corresponding page class:


// And assignment like this in the DSL
page = "Welcome"
// Becomes this in the pattern
page = new WelcomePage(session)
Free download pdf