groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ocs@ocs" <...@ocs.cz>
Subject Re: suggestion: ImplicitSafeNavigation annotation
Date Wed, 15 Aug 2018 10:52:45 GMT
Jochen,

> On 15 Aug 2018, at 10:13 AM, Jochen Theodorou <blackdrag@gmx.org> wrote:
> Am 14.08.2018 um 19:10 schrieb ocs@ocs:
>> Jochen,
>>> On 14 Aug 2018, at 6:34 PM, Jochen Theodorou <blackdrag@gmx.org <mailto:blackdrag@gmx.org>>
wrote:
> [...]
>>> are you saying x?.foo will NPE if x is null? Or that x?.getFoo() will NPE in
that case? Not sure how to read your comment.
>> Provided only (a) “the Null.metaclass; returning null from invokeMethod” is used
and no ASTT, then yes, it would NPE. Easiest thing on earth to try:
>> ===
>> 262 */tmp>* <q.groovy
>> class q {
>>     static main(args) {
>>         // ExpandoMetaClass.enableGlobally() // I thought this is needed; seems not
(though my application use it anyway)
>>         def mc=new OCSNMC(org.codehaus.groovy.runtime.NullObject)
>>         mc.initialize()
>>         org.codehaus.groovy.runtime.NullObject.metaClass=mc
>>         println "null.foo() is OK: ${null.foo()==null}"
>>         println "null.foo we won't see: ${null.foo==null}"
>>     }
>> }
>> class OCSNMC extends DelegatingMetaClass {
>>     OCSNMC(Class clazz){
>>         super(clazz)
>>     }
>>     Object invokeMethod(Object object, String methodName, Object[] arguments) {
>>         null
>>     }
>> }
> 
> null.foo will of course NPE, if you only take care of invokeMethod. have to do the same
treatment for get/setProperty, potentially also for get/setAttribute

That does not help; I do apologise again for sending a wrong example at first — check please
instead

> On 14 Aug 2018, at 11:17 PM, ocs@ocs <ocs@ocs.cz> wrote:
> 
> sorry for the example (I have written it in hurry); it should have looked rather like
this:
> 
> ===
> 266 /tmp> <q.groovy 
> class q {
>     static main(args) {
>         def mc=new OCSNMC(org.codehaus.groovy.runtime.NullObject)
>         mc.initialize()
>         org.codehaus.groovy.runtime.NullObject.metaClass=mc
> 
>         println "null.foo() is OK: ${null.foo()==null}"
>         println "null+null is OK: ${null+null==null}"
>         println "null[null] is OK (and no 3 needed, ha!): ${null[null]==null}"
>         println "null.foo though we won't see: ${null.foo==null}"
>     }
> }
> 
> class OCSNMC extends DelegatingMetaClass {
>     OCSNMC(Class clazz) { super(clazz) }
>     Object invokeMethod(Object object, String methodName, Object[] arguments) { null
}
>     Object getProperty(Class sender, Object receiver, String property, boolean isCallToSuper,
boolean fromInsideClass) {
>         println "getProperty called!" // actually never happens, dunno why
>         null
>     }
> }
> 267 /tmp> /usr/local/groovy-2.4.15/bin/groovy q
> null.foo() is OK: true
> null+null is OK: true
> null[null] is OK (and no 3 needed, ha!): true
> Caught: java.lang.NullPointerException: Cannot get property 'foo' on null object
> java.lang.NullPointerException: Cannot get property 'foo' on null object
> 	at q.main(q.groovy:10)
> 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 268 /tmp> 
> ===

Thanks and all the best,
OC

Mime
View raw message