groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Douglas Mendes <douglas.men...@gmail.com>
Subject Re: Override a method without knowing its parameters
Date Wed, 01 Mar 2017 16:51:25 GMT
Answering my own question:

It can be done if the metaClass is an ExpandoMetaClass. Like this:

expandoMetaClass.registerInstanceMethod(
    new TransformMetaMethod(originalMetaMethod) {
        @Override
        Object invoke(Object object, Object[] arguments) {
            println "Done! Method overriden!"
            super.invoke(object, arguments)
        }
    }
)

Anyway, in grails it didn't work (it kept calling the original method). Had
to use an AST.

Regards,
Douglas

Em qui, 23 de fev de 2017 às 16:12, Douglas Mendes <douglas.mendes@gmail.com>
escreveu:

Hi.

How can I properly override a method given a java.lang.reflect.Method and
its respective MetaClass?

I know that can be achieved by overriding the MetaClass's *invokeMethod *but
I can't do that (even if I could, would like to avoid).

Pseudocode:

*void doAnOverride(MetaClass metaClass, Method method) {*
*    metaClass."${method.name <http://method.name>}" = { args ->*
*        println "done"*
*    }*
*}*

Thanks,
Douglas

Mime
View raw message