This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: New Groovy "use" method added by Sam
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Sam Pullara has added a cool new feature to Groovy. There is a new method use which allows you to introduce methods to existing class.
At the moment this works just for the duration of closure, but in the future you will be able to use it elsewhere.
This is pretty cool. I also would love to see:
Feature plugin/out: It would be great to use this mechanism to allow "features" of the language to be plugged in and out. It could enable backwards compatibility in the future, allowing changes to the language as experience shows us what to do (one worry wrt standardizing before a real impl is out there!)
Flush out AOP-isms: Mixin's are great, but I would love to see a PointCut language, and rich support
From Sam's Email:
excerpt from src/test/groovy/xml/dom/DOMTest.groovy:
xml = new StringReader("<html><head><title class='mytitle'>Test</title></head><body><p class='mystyle'>This is a test.</p></body></html>");
doc = DOMBuilder.parse(xml);
html = doc.documentElement;
use (groovy.xml.dom.DOMCategory) {
assert html.head.title.textContent == "Test";
assert html.body.p.textContent == "This is a test.";
assert html.find { it.tagName == "body" }.tagName == "body";
assert html.getElementsByTagName("*").findAll { if (it["@class"] != "") { return it } }.size() == 2;
}