Groovy mop implements mini-language, such as the legendary XML embedded Syntax of java7.0

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.