Groovy 中的动态方法调用

Xel*_*ian 4 methods groovy dynamic

我想根据参数代码是否为空来调用方法1方法2

void add(def code, def index) {
    method(index).((code != null) ? "method1"(code) : "method2"())
}
Run Code Online (Sandbox Code Playgroud)

但什么也没发生?我的错在哪里?如果我写

method(index)."method1"(code)
Run Code Online (Sandbox Code Playgroud)

有效但不能使三元运算符起作用。