[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: [groovy-user] Metaclass does not work for properties?
From: OC <ocs () ocs ! cz>
Date: 2014-01-22 17:07:59
Message-ID: AD1F5A42-730C-4E54-9F55-523D1A3C8A67 () ocs ! cz
[Download RAW message or body]
Hello there,
On Jan 22, 2014, at 4:53 PM, OC <ocs@ocs.cz> wrote:
> > Actually that is almost what you could do today already, by changing the \
> > MetaClass of NullObject.
>
> Can I? Thanks, I haven't realized it. Ages ago I was adviced to use an ASTT to set \
> the safe attribute, which I am doing now, but it still leaves lots of problems \
> pretty darn hard to solve (like those operators etc). Thanks for pointing that out, \
> I'll try! It might prove ways better and more reliable than my current technique.
I'm trying this, and it seems it works excellently for methods, but does not for \
properties. What am I missing? Thanks a lot!
===
9 /tmp> <t.groovy
public class OCSNMC extends DelegatingMetaClass {
OCSNMC(Class clazz){
super(clazz)
}
Object invokeMethod(Object object, String methodName, Object[] arguments) {
if (arguments.size()==1 && arguments[0]==null && methodName=='is') return true
null
}
// trying all the found signatures...
Object getProperty(String property) {
println "1"
null
}
Object getProperty(Object object, String property) {
println "2"
null
}
Object getProperty(Class sender, Object receiver, String messageName, boolean \
useSuper, boolean fromInsideClass) { println "3"
null
}
}
def nmc=new OCSNMC(null.getClass())
nmc.initialize()
null.getClass().metaClass=nmc
assert null.bar()==null
assert null.bar==null
println "Okay!"
10 /tmp> groovy t
Caught: java.lang.NullPointerException: Cannot get property 'bar' on null object
java.lang.NullPointerException: Cannot get property 'bar' on null object
at t.run(t.groovy:29)
11 /tmp>
===
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic