Power Groovy DSL Features
[ 170 ]
given:
def fred = new Customer(id:1001,firstName:"Fred",
surname:"Flintstone",
street:"1 Rock Road",city:"Bedrock")
def barney = new Customer(id:1002,firstName:"Barney",
surname:"Rubble",
street:"2 Rock Road",city:"Bedrock")
def customerList = [ fred, barney]
when:
for (customer in customerList)
customer.prettyPrint()
then:
"""city: Bedrock
firstName: Fred
id: 1001
street: 1 Rock Road
surname: Flintstone
city: Bedrock
firstName: Barney
id: 1002
street: 2 Rock Road
surname: Rubble""" == output()
MetaClass
In addition to the regular Java Class object that we saw earlier when looking at
reflection, each Groovy object also has an associated MetaClass object. All Groovy
classes secretly implement the groovy.lang.GroovyObject interface, which exposes
a getMetaClass() method for each object:
public interface GroovyObject {
/**
* Invokes the given method.
*/
Object invokeMethod(String name, Object args);
/**
* Retrieves a property value.
*/
Object getProperty(String propertyName);
http://www.ebook3000.com