".&"と".@"の、文字列の変数展開によるアクセス

".&"による定義済メソッドへのアクセス、および".@"による定義済プロパティへのアクセスは、文字列の変数展開と組み合わせると、より柔軟なアクセスが可能になります。

[ソース]
class MyClass implements GroovyInterceptable {
    def greeting = 'accessed greeting'
    def id = 'White: '

    Object getProperty( String property ) {
        try{
            return this.@id + // プロパティへの直接的なアクセス
                   'indirectly ' +
                   this.@"$property" // プロパティへの直接的で動的なアクセス
        } catch( e ) {
            return "no such property $property"
        }
    }

    def hello( Object[] args ) {
        "invoked hello with (${args.join(', ')})"
    }

    def id( ) {
        'Green: '
    }

    def invokeMethod( String name, Object args ) {
        try{
            return this.&id( ) + // 直接的なメソッド呼び出し
                   'indirectly ' +
                   this.&"$name"( args ) // 直接的で動的なメソッド呼び出し
        } catch( e ) {
            return "no such method $name"
        }
    }
}

def mine = new MyClass( )
println mine.greeting
println mine.farewell
println mine.hello( 1, 'b', 3 )
println mine.foo( 'Mark', 19 )

[実行結果]
White: indirectly accessed greeting
no such property farewell
Green: indirectly invoked hello with (1, b, 3)
no such method foo