Rubee | 17 Jul 15:37

[groovy-user] creating a new method inside invokeMethod


I've seen examples where invokeMethod creates a new method so that, on the
next call, the performance hit is reduced. Here's an example from Venkat
Subramaniam's excellent "Programming Groovy." (See p. 223.)

class Manager
{
.......
def methodMissing(String name, args)
  {
    println "intercepting call to $name..."
    def delegateTo = null

    if(name.startsWith('simple')) { delegateTo = worker }
    if(name.startsWith('advanced')) { delegateTo = expert }

    if (delegateTo?.metaClass.respondsTo(delegateTo, name, args)) 
    { 
      Manager.metaClass."${name}" = { Object[] varArgs -> 
            return delegateTo.invokeMethod(name, *varArgs)
      }

      return delegateTo.invokeMethod(name, args)
    }

    throw new MissingMethodException(name, Manager.class, args)
  }
........
} //end class

(Continue reading)

Jochen Theodorou | 17 Jul 15:44

Re: [groovy-user] creating a new method inside invokeMethod

Rubee schrieb:
> I've seen examples where invokeMethod creates a new method so that, on the
> next call, the performance hit is reduced. Here's an example from Venkat
> Subramaniam's excellent "Programming Groovy." (See p. 223.)
> 
> class Manager
> {
> .......
> def methodMissing(String name, args)
>   {
>     println "intercepting call to $name..."
>     def delegateTo = null
>     
>     if(name.startsWith('simple')) { delegateTo = worker }
>     if(name.startsWith('advanced')) { delegateTo = expert }
>     
>     if (delegateTo?.metaClass.respondsTo(delegateTo, name, args)) 
>     { 
>       Manager.metaClass."${name}" = { Object[] varArgs -> 
>             return delegateTo.invokeMethod(name, *varArgs)
>       }
> 
>       return delegateTo.invokeMethod(name, args)
>     }
>     
>     throw new MissingMethodException(name, Manager.class, args)
>   }
> ........
> } //end class
> 
(Continue reading)

Graeme Rocher | 17 Jul 15:46

Re: [groovy-user] creating a new method inside invokeMethod

You shouldn't have to synchronize, Groovy uses concurrent
implementations of collections under the surface.

If you do get ConcurrentModificationExceptions it should be regarded
as a bug, but since we use this technique for GORM in Grails if this
were a problem we would know about it by now

Cheers

On Thu, Jul 17, 2008 at 2:38 PM, Rubee <charlestassoni@...> wrote:
>
> I've seen examples where invokeMethod creates a new method so that, on the
> next call, the performance hit is reduced. Here's an example from Venkat
> Subramaniam's excellent "Programming Groovy." (See p. 223.)
>
> class Manager
> {
> .......
> def methodMissing(String name, args)
>  {
>    println "intercepting call to $name..."
>    def delegateTo = null
>
>    if(name.startsWith('simple')) { delegateTo = worker }
>    if(name.startsWith('advanced')) { delegateTo = expert }
>
>    if (delegateTo?.metaClass.respondsTo(delegateTo, name, args))
>    {
>      Manager.metaClass."${name}" = { Object[] varArgs ->
>            return delegateTo.invokeMethod(name, *varArgs)
(Continue reading)

Rubee | 17 Jul 19:54

Re: [groovy-user] creating a new method inside invokeMethod


    From the comments above, I gather that Jochen (aka "blackdrag") thinks
that the most I should have to synchronize is this:

     synchronized(this) {
      	      Manager.metaClass."${name}" = { Object[] varArgs -> 
                        return delegateTo.invokeMethod(name, *varArgs) 
      	      }
      }

     and Graeme thinks I don't even need that.

     After Graeme's comment, I  realized I should probably search the Grails
source for 'synchronized', since that's a great real-world example.  I see a
few instances where Graeme or another Grails developer encloses method
creation inside a synchronized block.  (The cases I saw were for adding
static methods, which, of course, may be a different beast.)  So unless I
hear otherwise-- and please let me know if I can get away without this--I
think I'll go with the style shown above.

--

-- 
View this message in context: http://www.nabble.com/creating-a-new-method-inside-invokeMethod-tp18508436p18514253.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

(Continue reading)

Graeme Rocher | 17 Jul 21:09

Re: [groovy-user] creating a new method inside invokeMethod

You can do for safety, the reason I said it was a bug is we do have
one instance where that happens in GORM and that was as a result of a
bug in Groovy which I raised and was fixed

Cheers

On Thu, Jul 17, 2008 at 6:54 PM, Rubee <charlestassoni@...> wrote:
>
>    From the comments above, I gather that Jochen (aka "blackdrag") thinks
> that the most I should have to synchronize is this:
>
>     synchronized(this) {
>              Manager.metaClass."${name}" = { Object[] varArgs ->
>                        return delegateTo.invokeMethod(name, *varArgs)
>              }
>      }
>
>     and Graeme thinks I don't even need that.
>
>     After Graeme's comment, I  realized I should probably search the Grails
> source for 'synchronized', since that's a great real-world example.  I see a
> few instances where Graeme or another Grails developer encloses method
> creation inside a synchronized block.  (The cases I saw were for adding
> static methods, which, of course, may be a different beast.)  So unless I
> hear otherwise-- and please let me know if I can get away without this--I
> think I'll go with the style shown above.
>
>
> --
> View this message in context: http://www.nabble.com/creating-a-new-method-inside-invokeMethod-tp18508436p18514253.html
(Continue reading)

Rubee | 17 Jul 21:59

Re: [groovy-user] creating a new method inside invokeMethod


Thanks for the clarifications, and sorry I misunderstood Jochen's first post.

--

-- 
View this message in context: http://www.nabble.com/creating-a-new-method-inside-invokeMethod-tp18508436p18516652.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Jul 21:17

Re: [groovy-user] creating a new method inside invokeMethod

Rubee schrieb:
>     From the comments above, I gather that Jochen (aka "blackdrag") thinks
> that the most I should have to synchronize is this:
> 
>      synchronized(this) {
>       	      Manager.metaClass."${name}" = { Object[] varArgs -> 
>                         return delegateTo.invokeMethod(name, *varArgs) 
>       	      }
>       }
> 
>      and Graeme thinks I don't even need that.

no, you misunderstood... I agree with Graeme.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane