14.2 Method Synthesis Using ExpandoMetaClass
In Section 14.1, Method Synthesis Using methodMissing, we saw how to synthesize methods.
However, if we don’t have the privilege of editing the class source file or if the class is not a POGO,
that approach will not work. We can synthesize methods using the ExpandoMetaClass
in these cases.
We already saw how to interact with MetaClass in Section 12.2, Intercepting Methods Using MetaClass.
Instead of providing an interceptor for a domain method, we implement the
methodMissing
method on it. Let’s
take the Person class (and the boring jack) from Section 14.1, Method Synthesis Using methodMissing,
but instead ExpandoMetaClass, as shown here:
| InjectionAndSynthesisWithMOP/MethodSynthesisUsingEMC.groovy ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access