1 May 2011 02:32
Re: [groovy-user] Proxy + methodMissing + Dynamic metaMethod :: Not Working
Maybe MetaClass proxies can't be change. I simplified the problem. And this is the result:
class Foo {}
Foo.metaClass.bar = {->
println "Foo#bar() called"
Foo.metaClass."baz" = {-> } // Modifying, comment this and it will work
}
def proxy = ProxyMetaClass.getInstance(Foo)
GroovySystem.metaClassRegistry.setMetaClass(Foo, proxy)
println new Foo().bar() // Works
println new Foo().bar() // Error !!
Also, there is a big warning in ProxyMetaClass javadoc. But I think it's only in respect with multithreaded systems.
WARNING: This implementation of ProxyMetaClass is NOT threadsafe and hence should only be used for as a per-instance MetaClass running in a single thread. Do not place this MetaClass in the MetaClassRegistry as it will result in unpredictable behaviour
Groovy experts, plz, help.
Paulo Poiati
blog.paulopoiati.com
On Sat, Apr 30, 2011 at 6:35 PM, Paulo Gabriel Poiati <paulogpoiati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
But that don't explain why when u comment the cache line it works. Really weird.On Sat, Apr 30, 2011 at 6:25 PM, Paulo Gabriel Poiati <paulogpoiati <at> gmail.com> wrote:Hello virtualeyes.For me, it seems like the proxy version of your Child MetaClass doesn't carry the static context methodMissing.When you override the Child MetaClass in the MetaClassRegistry you mess up with your setup. So, the second call (Child.bar()) fails because the method isn't defined and no methodMissing is found either.Well, that is my guess.[]'s
Paulo Poiati
blog.paulopoiati.comOn Sat, Apr 30, 2011 at 10:16 AM, virtualeyes <sit1way-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:Hey all.
Following this example:
http://groovy.codehaus.org/Using+methodMissing+and+propertyMissing
I am able to successfully create and cache methods on-the-fly, great, life
is good, nice technique.
However, dynamically adding missing methods via MCL does not appear to work
with proxy enabled objects. In the below example if I remove the proxy, or
do not MCL-add the missing method on-the-fly, everything works, albeit
without desired functionality.
***************
class Parent {
def foo() { println "foo method" }
def bar() { println "bar method"}
}
class Child {}
Child.mixin Parent // child now has non-static parent methods, but we're
going to call them from static context...
new Loader()
Child.foo() // invoke static methodMissing
Child.foo() // call now-cached static version of instance method foo
Child.bar() // fails, "No signature...static Child.bar()"
// Why does static.methodMissing (see Loader class below) vanish post-foo
method MCL creation??
class Loader {
Loader() {
Child.metaClass.static.methodMissing = {name,args->
Object method = Child.metaClass.getMetaMethod(name)
def cached = { Object[] vargs->
println "Cached method $name called"
method.invoke(Child)
}
Child.metaClass.static."$name" = cached // comment this line and
works, but no method caching!
return method.invoke(Child)
}
def proxy = ProxyMetaClass.getInstance(Child)
proxy.interceptor = new PogoInterceptor()
GroovySystem.metaClassRegistry.setMetaClass(Child,proxy) // comment
this line and works, but no proxy!
}
}
class PogoInterceptor implements PropertyAccessInterceptor {
boolean doInvoke() { true }
void beforeSet(Object clazz, String k, Object v) {}
Object beforeGet(Object clazz, String k) {}
Object beforeInvoke(Object clazz, String method, Object[] args) {}
Object afterInvoke(Object clazz, String method, Object[] args, Object
result) {}
}
****************--
View this message in context: http://groovy.329449.n5.nabble.com/Proxy-methodMissing-Dynamic-metaMethod-Not-Working-tp4361137p4361137.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
RSS Feed