10.6 Calling Groovy Dynamic Methods from Java
In Groovy we can create methods at runtime, as we’ll see in Part 3, MOPping Groovy. We can’t directly call these methods from Java, because at compile time these methods don’t exist in the bytecode. They come to life at runtime, but if we’re calling them from Java we’d write the calls at compile time (or use reflection). To invoke the dynamic methods, we have to get past the Java compiler so the runtime can do the dispatching. That sounds complicated, but we can rely on Groovy!
Every Groovy object implements the GroovyObject interface, which has a special method named
invokeMethod
. This method accepts the name of the method to invoke and the arguments to pass. We can use the
invokeMethod ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access