Groovy MOP implements the XML embedded syntax of Java7.0

Source: Internet
Author: User
According to Groovy's Team Leader -- Guillaume Laforge, MOP (Meta Object Protocol) is his favorite Groovy feature.
MOP can be used to easily intercept and interpret attributes and methods. intercept is familiar with AOP. In Groovy, by reloading the get/setProperty () and invokeMethod () functions of yyobject, you can add your own hooks and behavior to implement proxy and delegator much simpler than Java.
More importantly, MOP functions can act as domain language interpreters and intercept some attributes and methods that are not defined in the Class for interpretation, this is the magic root of XML embedded syntax in Groovy.
For example, the following xml <shop> <book name = "foo"> <writer> Zhuang table Wei </writer> </book> </shop>
You can use groovy to access 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.