Groovy: Dynamically addings methods with a specific signature.
Posted
by Reverend Gonzo
on Stack Overflow
See other posts from Stack Overflow
or by Reverend Gonzo
Published on 2010-03-31T18:26:12Z
Indexed on
2010/03/31
19:33 UTC
Read the original article
Hit count: 162
So, I need to dynamically create (or inject) methods into an object that have a specific return type and method signature, because a Java tool we're using will be finding this methods via Reflection and checks for void
type. Method names will be determined at runtime.
Using metaClass. = { ... } however adds a closure which doesn't show up as a regular method (even if it can be used as one) and also has a return type.
I can't modify the method finding code, and it it not Groovy-aware.
I can't use methodMissing() or invokeMethod() because the method needs to actually exist. If I could overload class.getMethods() I think it would be possible, but I can't figure out how.
Is there any way to do this in Groovy?
© Stack Overflow or respective owner