groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Xiaoguang Wang (JIRA)" <j...@apache.org>
Subject [jira] [Created] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong
Date Thu, 11 Apr 2019 08:06:00 GMT
Xiaoguang Wang created GROOVY-9075:
--------------------------------------

             Summary: The exception message should be more clear when a GroovyObject's metaClass
is wrong
                 Key: GROOVY-9075
                 URL: https://issues.apache.org/jira/browse/GROOVY-9075
             Project: Groovy
          Issue Type: Bug
          Components: groovy-runtime
    Affects Versions: 2.5.6, 3.0.0-alpha-4
            Reporter: Xiaoguang Wang


If a GroovyObject's metaClass is overwritten by mistake, the accesses to the instance may
cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on debugging (and teaching
everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch in the runtime code and report this problem more clearly, it will
save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
    int x
}
//@CompileStatic
class C2 {
    int x
}

//@CompileStatic
class TestGroovy {
    static void main(String[] args) {
        def c1 = new C1()
        def c2 = new C2()
        c1.metaClass = c2.metaClass
        c1.x += 1  // crash here with unclear exception
    }
}

/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of
declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of
declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */

{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message