groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From OCsite <...@ocs.cz>
Subject Re: Calling original method from extension class
Date Fri, 17 Jul 2020 15:57:07 GMT
Alberto,

far as I understand properly, at the very least you should be able to do that programmatically,
like this (do not forget ExpandoMetaClass.enableGlobally()):

    def original=UnicastRemoteObject.metaClass.getMetaMethod("toString", new Class[])
    UnicastRemoteObject.metaClass.toString={ ->
        try { delegate.getName() }
        catch (MissingMethodException e) { original.invoke(delegate) }
    }

There might possibly be an easier/more flexible solution, which I don't see offhand.

All the best,
OC

> On 17 Jul 2020, at 16:18, Alberto <alberto.ing@gmail.com> wrote:
> 
> Hi all,
> 
> I'm trying to extend a class so that it has a different toString, but only in specific
cases.
> 
> I don't know for sure if this is possible, but what I'd like to do is:
> 
>     static String toString(UnicastRemoteObject self) {
>         try {
>             return self.getName();
>         } catch (MissingMethodException e) {
>             return // return the to string from Unicast.
>         }
>     }
> 
> Is it possible at all to call the original toString?
> 
> Regards,
> -- 
> Alberto Ingenito


Mime
View raw message