Author: Jiangnan Baiyi
According to groovy's team leader -- Guillaume LaForge, mop (Meta Object Protocol) is his favorite groovy feature.
Mop is a simple mechanism to intercept and interpret attributes and methods. Intercept is familiar with AOP.
Groovy classes are inherited from policyobject. policyobject has get/setproperty () and invokemethod () functions. When a customer calls a non-existent property and method, these two functions are used for processing. In Ruby, this method is more appropriately named method_missing (). The Groovy class can add its own hook and behavior by reloading these two functions, which is much simpler than Java.ProxyAndDelegator.
More importantly, the mop function can actDomain language interpreterTo intercept some attributes and methods that are not defined in the class that exist in the domain language. This is in groovy.Magic root of XML embedded syntax.
Ibm dw has a special article: practically mini-ages and mops of GROOVY:
For example, the following XML
<Shop>
<Book name = "foo">
<Writer> Zhuang table Wei writer>
Book>
Shop>
It can be accessed using groovy.
Println node. Book. Writer. Text ()
Of course, the node class does not have a book, and the writer class is a domain language attribute. However, by reloading the getpropety (string name) function, the node class can access the book and writer attributes, convert to the access of the corresponding Dom node.
For implementation, see the public object getproperty (final string elementname) function of the org. codehaus. Groovy. sandbox. util. xmllist class.
Guillaume LaForge said, it's an aspect of groovy which makes the language very powerful, but rare those who really know and use it.