AST Transformations
[ 214 ]
ClassHelper.make(contextClassName),
ArgumentListExpression.EMPTY_ARGUMENTS
),
null,
null
)
)
}
The first thing we need in the client class is a property to represent the context
object for the state machine. This property is a state context object, which will
always contain the current state of the machine. Next, we add a getter for the
current state of the machine:
def buildClientStateGetter() {
def callGetContextState = new ExpressionStatement(
new MethodCallExpression(
new MethodCallExpression(
new VariableExpression("context"),
new ConstantExpression("getState"),
ArgumentListExpression.EMPTY_ARGUMENTS
),
"toString",
ArgumentListExpression.EMPTY_ARGUMENTS
)
)
classNode.addMethod(new MethodNode(
"getState",
Modifier.PUBLIC,
null,
new Parameter[0],
null,
callGetContextState
)
)
}
The getState method returns the state of the context object as a string.
The code we are looking to generate for the body of the methods therefore is
context.getState().toString(). In the AST, the nodes needed for the method
needs to be in the following structure:
MethodNode getState()
|_ExpressionStatement
|_MethodCallExpression toString()
|_MethodCallExpression getState()
| |_VariableExpression context
http://www.ebook3000.com