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] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong
Date Thu, 11 Apr 2019 08:08:00 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Xiaoguang Wang updated GROOVY-9075:
-----------------------------------
    Description: 
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 (or some other check) 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  //eg: org.springframework.beans.BeanUtils.copyProperties
        c1.x += 1  // crash here with unclear exception
    }
}

{code}
 
{code:java}
/*
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}

  was:
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 (or some other check) 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
    }
}

{code}
 
{code:java}
/*
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}


> 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: 3.0.0-alpha-4, 2.5.6
>            Reporter: Xiaoguang Wang
>            Priority: Major
>
> 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 (or some other check) 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  //eg: org.springframework.beans.BeanUtils.copyProperties
>         c1.x += 1  // crash here with unclear exception
>     }
> }
> {code}
>  
> {code:java}
> /*
> 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