I'm not completely sure about the use-case that you're trying to solve, but likely you want one of either propertyMissing or getProperty (or maybe invokeMethod).
The first will let you intercept property requests when they don't actually exist on the object:
class Person {
def name = "Ted"
def propertyMissing(String name) { "my $name" }
}
def p = new Person()
assert "my address" == p.address
assert "my email" == p.email
assert "Ted" == p.name // property isn't missing
The second will let you intercept all property requests, even for defined properties:
class Person {
def name = "Ted"
def getProperty(String name) { "my $name" }
}
def p = new Person()
assert "my address" == p.address
assert "my email" == p.email
assert "my name" == p.name
If you're working with an existing class, you can either subclass it with one of these methods, or else add these methods to the metaClass:
String.metaClass.getProperty = { String name ->
return "String's property $name"
}
assert "String's property foo" == "".foo